View Single Post
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