| | |
Help cant save data to sql database in c#
![]() |
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace CarsRUS { public partial class Cars : Form { private SqlConnection conn; private SqlDataAdapter daAddresses; private DataSet dsAddresses; private DataGrid dgAddresses; private const string tableName = "CarRUS"; public class Customer { public Customer(string CustomerID, string Forename, string Surname, string Address, string PostCode) { this.customerID = CustomerID; this.forename = Forename; this.surname = Surname; this.address = Address; this.postcode = PostCode; } public string customerID; public string forename; public string surname; public string address; public string postcode; } //button for new customer private void button1_Click(object sender, EventArgs e) { Customer c = new Customer(textBox1.Text, textBox2.Text, textBox5.Text, textBox6.Text, textBox9.Text); customerQueue.Enqueue(c); fillListBox(customerQueue); textBox1.Text = "";//text box 1 will show the value that the user types in the customer id text box textBox2.Text = "";//text box 2 will show the value that the user types in the forename text box textBox5.Text = "";//text box 5 will show the value that the user types in the surname text box textBox6.Text = "";//text box 6 will show the value that the user types in the address text box textBox9.Text = "";//text box 9 will show the value that the user types in the postcode text box textBox10.Text = "";//text box 10 will show the value that the user types in the telno text box } private void fillListBox(Queue<Customer> customers)//fill the listbox of the customer details { IEnumerator<Customer> myEnumerator = customerQueue.GetEnumerator(); listBox1.Items.Add("----Start of Customer Booking----"); //display the message above to show the start of the queue while (myEnumerator.MoveNext()) { listBox1.Items.Add(myEnumerator.Current.customerID + " " + myEnumerator.Current.forename + " " + myEnumerator.Current.surname + " " + myEnumerator.Current.address + " " + myEnumerator.Current.postcode); }//list the details of the customer by customer id and then forename followed by the surname and then the address and then the postcode } private void Cars_Load(object sender, EventArgs e) { } //Button to quit program private void button3_Click(object sender, EventArgs e) { this.Close(); } Queue<Customer> customerQueue = new Queue<Customer>(); public Cars() { InitializeComponent(); // fill dataset Initdata(); // set up datagrid dgAddresses = new DataGrid(); dgAddresses.Location = new Point(5, 5); dgAddresses.Size = new Size(500, 500); dgAddresses.DataSource = null; dgAddresses.DataMember = null; // create update button Button btnUpdate = new Button(); btnUpdate.Text = "Update"; btnUpdate.Location = new Point(600, 150); btnUpdate.Click += new EventHandler(btnUpdateClicked); // make sure controls appear on form Controls.AddRange(new Control[] { dgAddresses, btnUpdate }); } // set up ADO.NET objects public void Initdata() { try { conn = new SqlConnection( "Data Source=.\\SQLEXPRESS;AttachDbFilename=G:\\CarRUS.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"); } catch (System.Exception ex) { MessageBox.Show("Exception" + ex.ToString()); } // 1. instantiate a new DataSet dsAddresses = new DataSet(); // 2. init SqlDataAdapter with select command and connection daAddresses = new SqlDataAdapter("select CustomerID, Forename, Surname, Address, PostCod, TelNo from CarRUS", conn); // 3. fill in insert, update, and delete commands SqlCommandBuilder cmdBldr = new SqlCommandBuilder(daAddresses); // 4. fill the dataset //daAddresses.Fill(dsAddresses, tableName); } // Update button was clicked public void btnUpdateClicked(object sender, EventArgs e) { // write changes back to DataBase daAddresses.Update(dsAddresses, tableName); } private void button2_Click(object sender, EventArgs e) { } private void GridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } } }//end
Last edited by Nick Evan; Nov 26th, 2009 at 5:32 pm.
•
•
Join Date: Jul 2009
Posts: 977
Reputation:
Solved Threads: 224
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
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
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:
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)
.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)
![]() |
Similar Threads
- Upload txt data to MS SQL Database (Using C# and ASP.NET) (C#)
- coding to save data in sql table (C#)
- SQL database stores data twice (ASP.NET)
- exporting existing excel sheet data into sql database (VB.NET)
- retrieve data from SQL database into a combobox (Visual Basic 4 / 5 / 6)
- Save Image in SQL Database Image field (ASP.NET)
- How to receive data from SQL database by ODBC class (C++)
- save data to access database (VB.NET)
- Unable to insert data into SQL Database (ASP)
Other Threads in the C# Forum
- Previous Thread: Folder`s name in User`s start menu
- Next Thread: SQL Database
Views: 796 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C#
.net 2d access algorithm application array asp.net automation bitmap box button c# check checkbox class code color connectionproblem control conversion csharp custom data database datagrid datagridview dataset datetime degrees display dll drawing enum event excel file flash form format forms function gcd gdi+ graphics image index input instance interface internet key list listbox login math mdi mysql networking notepad numbers operator oracle picturebox print problem procedure programming property protection radians reflection remote remoting resource richtextbox saving search sendkeys server socket sql statistics stream string table tcp text textbox threading time timer update validation vc++ visual webbrowser windows winforms wpf xml






