Help cant save data to sql database in c#

Reply

Join Date: Nov 2009
Posts: 1
Reputation: wiselka is an unknown quantity at this point 
Solved Threads: 0
wiselka wiselka is offline Offline
Newbie Poster

Help cant save data to sql database in c#

 
0
  #1
Nov 26th, 2009
  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 5:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 977
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 224
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #2
Nov 26th, 2009
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 3:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3,315
Reputation: Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute 
Solved Threads: 337
Moderator
Featured Poster
Nick Evan's Avatar
Nick Evan Nick Evan is online now Online
Cenosillicaphobiac
 
0
  #3
Nov 26th, 2009
Originally Posted by DdoubleD View Post
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 5:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 977
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 224
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #4
Nov 26th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,793
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 698
Sponsor
Featured Poster
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #5
Nov 27th, 2009
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)
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

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




Views: 796 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC