Updating SQL database, Please help

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 6
Reputation: markyjj is an unknown quantity at this point 
Solved Threads: 0
markyjj markyjj is offline Offline
Newbie Poster

Updating SQL database, Please help

 
0
  #1
Sep 6th, 2008
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.



  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Data.Common;
  8. using System.Data.SqlClient;
  9.  
  10. namespace MovieDiary
  11. {
  12. /// <summary>
  13. /// Summary description for Form1.
  14. /// </summary>
  15. public class Form1 : System.Windows.Forms.Form
  16. {
  17. private System.Windows.Forms.DataGrid dataGrid1;
  18. private System.Windows.Forms.Button button1;
  19. /// <summary>
  20. /// Required designer variable.
  21. /// </summary>
  22. private System.ComponentModel.Container components = null;
  23.  
  24. public static SqlConnection Mcon = new SqlConnection("Server=SHERMAN2;Database =MDatabase; data source=\"SH" +
  25. "ERMAN2\\SQLEXPRESS\"");
  26.  
  27.  
  28. public static SqlDataAdapter MovAdapt = new SqlDataAdapter("SELECT * FROM MovTable1", Mcon);
  29. SqlCommandBuilder cb = new SqlCommandBuilder(MovAdapt);
  30. DataTableMapping myNewMapping =
  31. new DataTableMapping("MovMap","MovTable1");
  32. public static DataSet Movset1 = new DataSet();
  33.  
  34. public Form1()
  35. {
  36. //
  37. // Required for Windows Form Designer support
  38. //
  39. InitializeComponent();
  40.  
  41. //
  42. // TODO: Add any constructor code after InitializeComponent call
  43. //
  44. }
  45.  
  46. /// <summary>
  47. /// Clean up any resources being used.
  48. /// </summary>
  49. protected override void Dispose( bool disposing )
  50. {
  51. if( disposing )
  52. {
  53. if (components != null)
  54. {
  55. components.Dispose();
  56. }
  57. }
  58. base.Dispose( disposing );
  59. }
  60.  
  61. #region Windows Form Designer generated code
  62. /// <summary>
  63. /// Required method for Designer support - do not modify
  64. /// the contents of this method with the code editor.
  65. /// </summary>
  66. private void InitializeComponent()
  67. {
  68. this.dataGrid1 = new System.Windows.Forms.DataGrid();
  69. this.button1 = new System.Windows.Forms.Button();
  70. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
  71. this.SuspendLayout();
  72. //
  73. // dataGrid1
  74. //
  75. this.dataGrid1.DataMember = "";
  76. this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  77. this.dataGrid1.Location = new System.Drawing.Point(0, 0);
  78. this.dataGrid1.Name = "dataGrid1";
  79. this.dataGrid1.Size = new System.Drawing.Size(448, 128);
  80. this.dataGrid1.TabIndex = 0;
  81. //
  82. // button1
  83. //
  84. this.button1.Location = new System.Drawing.Point(104, 168);
  85. this.button1.Name = "button1";
  86. this.button1.Size = new System.Drawing.Size(128, 23);
  87. this.button1.TabIndex = 1;
  88. this.button1.Text = "Update And Save";
  89. this.button1.Click += new System.EventHandler(this.button1_Click);
  90. //
  91. // Form1
  92. //
  93. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  94. this.ClientSize = new System.Drawing.Size(472, 266);
  95. this.Controls.Add(this.button1);
  96. this.Controls.Add(this.dataGrid1);
  97. this.Name = "Form1";
  98. this.Text = "Form1";
  99. this.Load += new System.EventHandler(this.Form1_Load);
  100. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
  101. this.ResumeLayout(false);
  102.  
  103. }
  104. #endregion
  105.  
  106. /// <summary>
  107. /// The main entry point for the application.
  108. /// </summary>
  109. [STAThread]
  110. static void Main()
  111. {
  112. Application.Run(new Form1());
  113. }
  114.  
  115. public void Form1_Load(object sender, System.EventArgs e)
  116. {
  117.  
  118. MovAdapt.Fill(Movset1,"MovTable1");
  119. dataGrid1.DataSource = Movset1;
  120. Show();
  121.  
  122.  
  123. }
  124.  
  125. public void button1_Click(object sender, System.EventArgs e)
  126. {
  127.  
  128.  
  129. MovAdapt.Update(Movset1, "MovTable1");
  130. Movset1.AcceptChanges();
  131.  
  132.  
  133. }
  134. }
  135. }
Last edited by cscgal; Sep 6th, 2008 at 2:35 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Updating SQL database, Please help

 
0
  #2
Sep 6th, 2008
Whats the actual error you get? (And do you have read/write permissions on the database)
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: markyjj is an unknown quantity at this point 
Solved Threads: 0
markyjj markyjj is offline Offline
Newbie Poster

Re: Updating SQL database, Please help

 
0
  #3
Sep 6th, 2008
Originally Posted by LizR View Post
Whats the actual error you get? (And do you have read/write permissions on the database)

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
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 80
Reputation: gusano79 is on a distinguished road 
Solved Threads: 5
gusano79 gusano79 is offline Offline
Junior Poster in Training

Re: Updating SQL database, Please help

 
0
  #4
Sep 7th, 2008
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:

  1. MovAdapt.UpdateCommand = cb.GetUpdateCommand

This article has a good explanation of how it works.
--smg
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: markyjj is an unknown quantity at this point 
Solved Threads: 0
markyjj markyjj is offline Offline
Newbie Poster

Re: Updating SQL database, Please help

 
0
  #5
Sep 8th, 2008
Originally Posted by gusano79 View Post
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:

  1. MovAdapt.UpdateCommand = cb.GetUpdateCommand

This article has a good explanation of how it works.
I have tried using the following code: MovAdapt.UpdateCommand = cb.GetUpdateCommand but now I get the following exception:

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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 3
Reputation: nevoj is an unknown quantity at this point 
Solved Threads: 0
nevoj's Avatar
nevoj nevoj is offline Offline
Newbie Poster

Re: Updating SQL database, Please help

 
0
  #6
Sep 9th, 2008
hmp
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 80
Reputation: gusano79 is on a distinguished road 
Solved Threads: 5
gusano79 gusano79 is offline Offline
Junior Poster in Training

Re: Updating SQL database, Please help

 
0
  #7
Sep 10th, 2008
Originally Posted by markyjj View Post
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.
Ah, yes. The command builder also requires that your SELECT statement 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 SELECT statement.

If not, you'll have to write the update command yourself. Here's what it might look like:

  1. using(SqlConnection connection = new SqlConnection("<connection string>"))
  2. {
  3. SqlDataAdapter adapter = new SqlDataAdapter(
  4. "SELECT SomeField, SomeOtherField FROM Table"
  5. );
  6.  
  7. adapter.UpdateCommand = new SqlCommand(
  8. "UPDATE Table SET SomeField = @SomeField WHERE TheKey = @TheKey"
  9. );
  10.  
  11. adapter.UpdateCommand.Parameters.Add(
  12. "@TheKey", SqlDbType.Int).SourceColumn = "TheKey";
  13.  
  14. adapter.UpdateCommand.Parameters.Add(
  15. "@SomeField", SqlDbType.Binary).SourceColumn = "SomeField";
  16.  
  17. DataTable table = new DataTable();
  18.  
  19. connection.Open();
  20.  
  21. adapter.Fill(table);
  22.  
  23. foreach(DataRow row in table.Rows)
  24. {
  25. row["SomeField"] = (int)row["SomeOtherField"] == 42;
  26. }
  27.  
  28. adapter.Update(table);
  29. }
--smg
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: markyjj is an unknown quantity at this point 
Solved Threads: 0
markyjj markyjj is offline Offline
Newbie Poster

Re: Updating SQL database, Please help

 
0
  #8
Sep 10th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 80
Reputation: gusano79 is on a distinguished road 
Solved Threads: 5
gusano79 gusano79 is offline Offline
Junior Poster in Training

Re: Updating SQL database, Please help

 
0
  #9
Sep 10th, 2008
Originally Posted by markyjj View Post
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.
Short answer is no. How will your application know what SQL to send to your database to insert, update, or delete records?

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: DataGrid is 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 DataSet or DataTable as 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. SqlDataAdapter is probably the easiest way manage the link between these data objects and your database. At a minimum, you need to write the SELECT statement, and if you aren't able to use SqlCommandBuilder you'll also need to write appropriate INSERT , UPDATE , and DELETE statements for full database operation. Once that's done, though, the adapter's Update method 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.
--smg
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC