944,131 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1622
  • C# RSS
Nov 26th, 2009
0

Help cant save data to sql database in c#

Expand Post »
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10.  
  11. namespace CarsRUS
  12. {
  13. public partial class Cars : Form
  14. {
  15. private SqlConnection conn;
  16. private SqlDataAdapter daAddresses;
  17. private DataSet dsAddresses;
  18. private DataGrid dgAddresses;
  19. private const string tableName = "CarRUS";
  20. public class Customer
  21. {
  22. public Customer(string CustomerID, string Forename, string Surname, string Address, string PostCode)
  23. {
  24. this.customerID = CustomerID;
  25. this.forename = Forename;
  26. this.surname = Surname;
  27. this.address = Address;
  28. this.postcode = PostCode;
  29. }
  30. public string customerID;
  31. public string forename;
  32. public string surname;
  33. public string address;
  34. public string postcode;
  35. }
  36.  
  37. //button for new customer
  38. private void button1_Click(object sender, EventArgs e)
  39. {
  40. Customer c = new Customer(textBox1.Text, textBox2.Text, textBox5.Text, textBox6.Text, textBox9.Text);
  41. customerQueue.Enqueue(c);
  42. fillListBox(customerQueue);
  43. textBox1.Text = "";//text box 1 will show the value that the user types in the customer id text box
  44. textBox2.Text = "";//text box 2 will show the value that the user types in the forename text box
  45. textBox5.Text = "";//text box 5 will show the value that the user types in the surname text box
  46. textBox6.Text = "";//text box 6 will show the value that the user types in the address text box
  47. textBox9.Text = "";//text box 9 will show the value that the user types in the postcode text box
  48. textBox10.Text = "";//text box 10 will show the value that the user types in the telno text box
  49. }
  50.  
  51. private void fillListBox(Queue<Customer> customers)//fill the listbox of the customer details
  52. {
  53. IEnumerator<Customer> myEnumerator = customerQueue.GetEnumerator();
  54. listBox1.Items.Add("----Start of Customer Booking----");
  55. //display the message above to show the start of the queue
  56. while (myEnumerator.MoveNext())
  57. {
  58. listBox1.Items.Add(myEnumerator.Current.customerID + " " + myEnumerator.Current.forename + " " + myEnumerator.Current.surname + " " + myEnumerator.Current.address + " " + myEnumerator.Current.postcode);
  59. }//list the details of the customer by customer id and then forename followed by the surname and then the address and then the postcode
  60. }
  61.  
  62. private void Cars_Load(object sender, EventArgs e)
  63. {
  64. }
  65. //Button to quit program
  66. private void button3_Click(object sender, EventArgs e)
  67. {
  68. this.Close();
  69. }
  70. Queue<Customer> customerQueue = new Queue<Customer>();
  71. public Cars()
  72. {
  73. InitializeComponent();
  74. // fill dataset
  75. Initdata();
  76. // set up datagrid
  77. dgAddresses = new DataGrid();
  78. dgAddresses.Location = new Point(5, 5);
  79. dgAddresses.Size = new Size(500, 500);
  80. dgAddresses.DataSource = null;
  81. dgAddresses.DataMember = null;
  82. // create update button
  83. Button btnUpdate = new Button();
  84. btnUpdate.Text = "Update";
  85. btnUpdate.Location = new Point(600, 150);
  86. btnUpdate.Click += new EventHandler(btnUpdateClicked);
  87. // make sure controls appear on form
  88. Controls.AddRange(new Control[] { dgAddresses, btnUpdate });
  89. }
  90. // set up ADO.NET objects
  91. public void Initdata()
  92. {
  93. try
  94. {
  95. conn = new SqlConnection(
  96. "Data Source=.\\SQLEXPRESS;AttachDbFilename=G:\\CarRUS.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
  97. }
  98. catch (System.Exception ex)
  99. {
  100. MessageBox.Show("Exception" + ex.ToString());
  101. }
  102. // 1. instantiate a new DataSet
  103. dsAddresses = new DataSet();
  104. // 2. init SqlDataAdapter with select command and connection
  105. daAddresses = new SqlDataAdapter("select CustomerID, Forename, Surname, Address, PostCod, TelNo from CarRUS", conn);
  106. // 3. fill in insert, update, and delete commands
  107. SqlCommandBuilder cmdBldr = new SqlCommandBuilder(daAddresses);
  108. // 4. fill the dataset
  109. //daAddresses.Fill(dsAddresses, tableName);
  110. }
  111. // Update button was clicked
  112. public void btnUpdateClicked(object sender, EventArgs e)
  113. {
  114. // write changes back to DataBase
  115. daAddresses.Update(dsAddresses, tableName);
  116. }
  117. private void button2_Click(object sender, EventArgs e)
  118. {
  119. }
  120.  
  121. private void GridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  122. {
  123.  
  124. }
  125. }
  126. }//end
Last edited by Nick Evan; Nov 26th, 2009 at 6:32 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wiselka is offline Offline
1 posts
since Nov 2009
Nov 26th, 2009
0
Re: Help cant save data to sql database in c#
First, change this post to NOT be a snippet because that is not what a SNIPPET is for!

Where it asks for "What are you posting?", just use the default: Forum Thread
Last edited by DdoubleD; Nov 26th, 2009 at 4:05 pm.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Nov 26th, 2009
0
Re: Help cant save data to sql database in c#
Click to Expand / Collapse  Quote originally posted by DdoubleD ...
First, change this post to NOT be a snippet because that is not what a SNIPPET is for!

Where it asks for "What are you posting?", just use the default: Forum Thread
I've gone ahead and changed. I couldn't find a question anywhere in there though.
Last edited by Nick Evan; Nov 26th, 2009 at 6:33 pm.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 26th, 2009
0
Re: Help cant save data to sql database in c#
I don't see where you are setting up the update command... Look at this link: http://msdn.microsoft.com/en-us/libr...er.update.aspx
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Nov 27th, 2009
0
Re: Help cant save data to sql database in c#
You should not be querying an SQL server in the constructor of a class for best-practices reasons and it appears you are not calling .Dispose() on IDisposable instances in your class. Most, if not every, control you will encounter in .NET uses a late or delayed binding mechanism so the first place you should really consider populating a local data store (dataset, datatable) is in the form's load event.

In your cars class you are exposting public fields -- this is also not recommended for best practices. You should use public properties. Microsoft added "auto properties" for this reason:
public string SomeProp { get; set; }
Lastly why are you creating all of this in code? You can use the designer to create these classes for you and generate the related code.

Now as nothing above related to your actual question -- DdoubleD answered your question. You need to specify Select, Update, Delete, and Insert queries for your DataSet if you intend to do those operations (typically called CRUD)
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Folder`s name in User`s start menu
Next Thread in C# Forum Timeline: SQL Database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC