1 Replies - 1967 Views - Last Post: 26 February 2009 - 10:06 AM Rate Topic: -----

#1 getajob1   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 03-February 09

Updating SQL Server DB from DataGridView

Post icon  Posted 26 February 2009 - 05:59 AM


I am trying to update a SQL Server database from a datagridview. I have looked at examples but still don't quite have it.
one example said to put the endedit in a cellchanged method, where is it if this is true? Is the data updated from cells are changed
or when a user hits a Update button. Where do I put what code? The following code fills and displays the data but doesn't update it.

 public void LoadSql2()
		{
			//  string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS2\MSSQL\DATA\ReportServer$SQLEXPRESS2TempDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; 
			//string connectionString = @"server = ABM\SQLEXPRESS;integrated security = true;User Instance=True;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS2\MSSQL\DATA\AdventureWorksDW_Data.mdf";
			string sqlSel = @"Select * From DimScenario";

			SqlConnection conndb = new SqlConnection(connectionString); //make a OleDbConnection object					  
		   // DataSet ds = new DataSet();								 //make a DataSet object

			try
			{
				SqlDataAdapter myAdapter = new SqlDataAdapter(sqlSel, conndb); //Using the SqlDataAdapter execute the query										
		   //	 myAdapter.Fill(ds, "Test");									//Fill the DataSet from the query, dsname can be anything			
		   //	 gridColors.DataSource = ds.Tables["Test"];
				SqlCommandBuilder myCommand = new SqlCommandBuilder(myAdapter);
				DataTable myTable = new DataTable();

				myAdapter.Fill(myTable);
				bSource.DataSource = myTable;
				gridColors.DataSource = myTable;
				bSource.EndEdit();
				myAdapter.Update(myTable);

			
			  //  myAdapter.Dispose();

			}
			catch (Exception e)
			{
				MessageBox.Show("Error in connecting! " + e.ToString(), "Error");
			}
			finally
			{
			   
				conndb.Close();	 
				conndb.Dispose();
			}
		}



Is This A Good Question/Topic? 0
  • +

Replies To: Updating SQL Server DB from DataGridView

#2 getajob1   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 03-February 09

Re: Updating SQL Server DB from DataGridView

Posted 26 February 2009 - 10:06 AM


I would appreciate some help. Thanks

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1