|
Visual Studio c# for windows (not WPF)
|
|
|
|
|
Okay, so I'm thinking my problem here probably involves completely misunderstanding the use of a DataGridView, but here goes:
I'm currently working on learning how to use SQL with C#/.NET using VS2010. I created a DataGridView that's linked to my data table. Adding rows to the DataGridView while the program was running did not actually save the changes to the database, so I created a separate piece using SqlConnection & SqlCommand. This successfully added the rows to the database, but did not update the DataGridView.
So I suppose there are two questions here: How do I save the changes to a DataGridView to the bound data table? and How do I have the DataGridView reload table data?
|
|
|
|
|
While this most definitely doesn't answer your question, here goes:
Don't use DataGridViews to enter/introduce/edit data. Use dedicated windows/forms.
You're life gets a whole lot easier.
The reasons are plenty: more control(like data validation), easier to debug, the app looks nicer...
I only use DataGridViews for displaying data(if that).
All the best,
Dan
|
|
|
|
|
Good to know. Given that I'm at this point just trying to figure out how to use SQL in C#, it's good to know which pieces to avoid. So yes, that answer was useful, even if it didn't actually answer my specific question.
|
|
|
|
|
The updating is usually handled by a DataAdapter.Update -- but as has been said, you won't have a very impressive app.
|
|
|
|
|
I have been struggling HARD to find a solution to this. In order to get some long-term platform compatibility (PC simulator, embedded target, etc.) I am implementing my main code in C, and a GUI emulator in C# as a DLL.
I am able to make calls into the C# DLL from my native code by exposing the DLL functions as COM objects (I think...). Data are passed in and out of the library functions just fine.
I would like to have callbacks made back to the native code when events are triggered in the C# code. You can imagine that when a button is clicked on my fancy C# GUI, my native code gets called. I cannot seem to get this callback mechanism working.
Can anyone please give an example, syntax, etc. for assigning a native callback to a managed C# event (or something like that)?
There are TONS of examples of using managed code to call un-managed DLLs, but very little going to the other way.
Thanks in advance.
-Stuart
|
|
|
|
|
Stuart, you need to expose your event as described in this[^] example. This creates the COM hookup for the event handler that you can reference through your C code.
|
|
|
|
|
Nice one - I think that is going to come in handy to me some day too.
Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
|
|
|
|
|
Peter, this is helpful, but at least half of the challenge is on the native (un-managed) side. I think now I have a syntactic problem with registering my native C function with the managed event.
Any more tips?
Thanks!
|
|
|
|
|
That side is going to require you to hook up to the COM event in your C code. You might want to read this[^] article to see how C++ handles it (it should be easy enough to apply this to C).
|
|
|
|
|
"Calling" the managed functions isn't really a problem anymore. The real problem is passing in a reference to the unamanged event handler function into the managed event. Sorry to keep pushing on this, but does sound like you know what you're doing!
Thanks again.
|
|
|
|
|
Have you assigned the IConnectionPoint interface[^]? As you've implemented the event interface in your C#, the CLR Interop automatically applies the juice that hooks this together.
|
|
|
|
|
Hi all I have a problem i am importing data from an excell spreadsheet to a sql 2008 database ,and i can do that in c# 3.5 getting all fields in the spreadsheet the same as the ones in the db..
I want to select now a few fields and insert them or rather import them to the database not all fields are the same here heres my code and i am getting this error
" The connection was not closed. The connection's current state is open."
i want only these 2 fields(DOC_TPL_CGY_TP_DESC ,USR ) from excell now to dbs the other two i will insert namually please help
private void btnDocumentCat_Click(object sender, EventArgs e)
{
string oconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\2010Projects\\Project1\\DocumentCatalogue\\bin\\Debug\\MyExcelFile.xls;Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
OleDbConnection con = new OleDbConnection(oconn);
string filepath = @"C:\\2010Projects\\Project1\\DocumentCatalogue\\bin\\Debug\\MyExcelFile.xls";
string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filepath + ";" + "Extended Properties=Excel 8.0;";
using (OleDbConnection cn = new OleDbConnection(ConnectionString))
{
cn.Open();
DataTable dbSchema = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dbSchema == null || dbSchema.Rows.Count < 1)
{
throw new Exception("Error: Could not determine the name of the first worksheet.");
}
string WorkSheetName = dbSchema.Rows[0]["TABLE_NAME"].ToString();
OleDbCommand ocmd = new OleDbCommand("SELECT * FROM [" + WorkSheetName + "]", cn);
OleDbCommand OCM = new OleDbCommand("SELECT DOCUMENT NAME ,USER FROM [" + WorkSheetName + "]", con);
DataTable dt = new DataTable(WorkSheetName);
OleDbDataReader odr = OCM.ExecuteReader();
//OleDbDataReader URD = OCM.ExecuteReader();
string DOC_TPL_CGY_TP_DESC = string.Empty;
string DOC_TPL_CGY_TP_CODE = string.Empty;
int DOC_TPL_CGY_TP_ID = 0;
string USR = string.Empty;
while (odr.Read())
{
// DOC_TPL_CGY_TP_CODE = valid(odr, 0);
DOC_TPL_CGY_TP_DESC = valid(odr, 0);
USR = valid(odr, 1);
Insertdataintosql2(DOC_TPL_CGY_TP_CODE,DOC_TPL_CGY_TP_DESC,DOC_TPL_CGY_TP_ID, USR);
}
}
}
private void Insertdataintosql2(string DOC_TPL_CGY_TP_CODE, string DOC_TPL_CGY_TP_DESC, int DOC_TPL_CGY_TP_ID, string USR)
{
throw new NotImplementedException();
}
sipho
|
|
|
|
|
That code sure is hard to read without PRE tags.
Which line is giving the Exception?
Why use @ and \\ ?
What are the DataTable dt and OleDbCommand ocmd used for?
Is the connection to the destination database open?
|
|
|
|
|
oh yes i saw my connection was wrong...thanks but yes am still struggling i wanna select a few colomns from the excel sheet to the dbs but i aint sure how to do that if its not all columns.
|
|
|
|
|
I just don't see what would give you that error.
|
|
|
|
|
Why take all the pains to do it in C#, when you can do it in TSQL directly.
select * from OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=C:\myexcel.xls', 'select * from [Sheet1$]')
|
|
|
|
|
OleDbCommand OCM = new OleDbCommand("SELECT DOCUMENT_NAME ,USER FROM [" + WorkSheetName + "]", cn);
i am using the above way because i only want thse two coloumns form my excel and import them to a dbs table but i still am getting an arror..please help
error "No value given for one or more required parameters."
|
|
|
|
|
Did you name the columns in the spreadsheet? By default, the values in the first row are treated as column names.
|
|
|
|
|
yes my spreadsheet coloumns are the ones in this line of code
OleDbCommand OCM = new OleDbCommand("SELECT [DOCUMENT_NAME] ,[USER] FROM [" + WorkSheetName + "]", cn);
OleDbDataReader odr = OCM.ExecuteReader();
when i build my app it can not go tru this statement gives me this error
"No value given for one or more required parameters." because what i am trying to do is get documentname and user name from excell then store it in a table but now i can get these ..
Hope i did answer you question
|
|
|
|
|
Ensure that the WorkSheetName variable ends with a dollar ($) symbol.
|
|
|
|
|
Hi all
I'm having trouble with my app and i need some help.
I have an app that needs to paste some numbers from a listbox into another App.
Using
SendKeys.Send("^v");
works great while working on a local computer, the problem is that i'm working on a remote desktop and sometimes when sending the "^v" it only sends the "v"
Can anyone help me figure this out ?
Thanks in advance.
|
|
|
|
|
|
I don't think that's the problem , because it works without the {} , but for example when i have 50 numbers in the listbox, pasting 45-47 number is OK, while it makes 3-5 random mistakes, sending only the "v".
To emulate "Control" you don't need {} because than it will send the "^" character only...
|
|
|
|
|