![]() |
| ||
| Updating SQL database, Please help Can someone please help with the following problem I am trying to update a sql database through a command builder but I keep getting an sql exception (Additional information: System error.). I dont know where I am going wrong because I thought the command builder should carry out the update automatically. the table on the sql database is called 'MovieTable1' and the dataset is called 'MovSet1'. please see the code I am using below....any help would be appreciated because this is now giving me a headache. using System; |
| ||
| Re: Updating SQL database, Please help Whats the actual error you get? (And do you have read/write permissions on the database) |
| ||
| Re: Updating SQL database, Please help Quote:
Well when I press the update button which has the update method I then get the following exception: An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll Additional information: System error. How do I check if I have read/write permissions on the database?, because I assumed I have since I can read the data when I use the fill method. thanks |
| ||
| Re: Updating SQL database, Please help Looks like you're not generating the update command. It isn't actually created until you call SqlCommandBuilder.GetUpdateCommand(). You'll need something like this: MovAdapt.UpdateCommand = cb.GetUpdateCommand This article has a good explanation of how it works. |
| ||
| Re: Updating SQL database, Please help Quote:
An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll Additional information: Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information. Thanks for any further help. |
| ||
| Re: Updating SQL database, Please help hmp |
| ||
| Re: Updating SQL database, Please help Quote:
SELECTstatement return a primary key or unique index. If your table has either of those, you may be able to get away with simply adding the appropriate column(s) to the SELECTstatement. If not, you'll have to write the update command yourself. Here's what it might look like: using(SqlConnection connection = new SqlConnection("<connection string>")) |
| ||
| Re: Updating SQL database, Please help Thanks again but couldnt I just enter dynamic data direct through the datagrid and then just update the table(In other words actually click on the datagrid and then type data in the list and then click on update). The added data will be movie titles, director etc so that I could just add to the list when I want to update. regards |
| ||
| Re: Updating SQL database, Please help Quote:
I wish I had a better answer for you, but this is how it works. There are other ways to do what you want within ADO.NET, but they require more code and more manual management of the database. I believe that what I'm describing here is one of the simpler methods, and I think the most decoupled from the database itself. Longer answer: DataGridis there to provide a user interface. It can display and manage data based on a variety of underlying data-holding classes. We're looking at a DataSetor DataTableas the backing object for your DataGrid. Both of these are in-memory representations, i.e., they are a copy of your data, and they don't represent a direct connection to the database. SqlDataAdapteris probably the easiest way manage the link between these data objects and your database. At a minimum, you need to write the SELECTstatement, and if you aren't able to use SqlCommandBuilderyou'll also need to write appropriate INSERT, UPDATE, and DELETEstatements for full database operation. Once that's done, though, the adapter's Updatemethod takes care of all of the details. If the .NET Framework 3.5 is available to you, there is also LINQ. It provides a language-integrated query facility that is much more intuitive than ADO.NET mechanisms and takes considerably fewer lines of code to do the same things. Using LINQ, however, is a whole different discussion--I only mention it in case you aren't familiar with it. |
| All times are GMT -4. The time now is 5:16 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC