Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Updating information in a datagrid by using a button Expand / Collapse
Author
Message
Posted Friday, April 26, 2013 9:18 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 1:06 PM
Points: 69, Visits: 170
Hi,

Another C# question here. What I am trying to do is update information within a datagrid that could contain multiple records and multiple edits when clicking an Update button.

When I run my code I get no errors, but also no action happens either. Can you help me understand what I am missing here?

SqlConnection myConnection = new SqlConnection(Test_Utility.Properties.Settings.Default.ConnectionString);
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("Select * from Assignment", myConnection);
mySqlDataAdapter.UpdateCommand = new SqlCommand("Update Assignment SET Active='0' WHERE ID='99999'", myConnection);
SqlCommandBuilder cb = new SqlCommandBuilder(mySqlDataAdapter);
cb.GetUpdateCommand();

DataSet ds = new DataSet();
mySqlDataAdapter.Fill(ds);
dataGridView1.DataSource = ds;
mySqlDataAdapter.Update(ds);

Thanks in advance!
Post #1447058
Posted Tuesday, April 30, 2013 8:03 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 1:06 PM
Points: 69, Visits: 170
Hi,

I am still trying to figure this one out. What I want to do is update all records in a datagrid when clicking an update button. I have been able to get it to update the current row, but what do I need to do from here to get it to update all records?

Here is my latest code:

SqlConnection updConnection = new SqlConnection(Test_Utility.Properties.Settings.Default.ConnectionString);
string updSqlStmt = "UPDATE Assignment SET Active= '1', StatusType='9999', EndDt =@EndDt WHERE ID=@ID";


try
{
updConnection.Open();
SqlCommand updCmd = new SqlCommand(updSqlStmt, updConnection);
updCmd.Parameters.AddWithValue("@ID", dataGridView1.CurrentRow.Cells["ID"].Value);
updCmd.Parameters.AddWithValue("@EndDt", dataGridView1.CurrentRow.Cells["EndDt"].Value);
updCmd.CommandType = CommandType.Text;
updCmd.ExecuteNonQuery();

txtProgress.Text = "Update completed.";
AssignmentTableAdapter.FillID(ds_Assignment.tAssignment, Convert.ToInt32(txtID.Text));
}
finally
{
updConnection.Close();
updConnection.Dispose();
}
Post #1448046
Posted Wednesday, May 1, 2013 8:53 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 3:15 PM
Points: 561, Visits: 1,466
Hello,
I have implemented a form with a grid that can be used to edit data in a variety of different tables. Then the update button calls a generic method that receives the dataset that was bound to the grid. It gets the table name from the dataset and uses a SQLCommandBuilder object to create the update (and all the other commands) from a simple select of all fields.

Here is the code:

        public void UpdateDataGridList(DataSet gridDataSet)
{
// File the updates from the grid view form...
SqlConnection connection = new SqlConnection(ConnectString);
SqlDataAdapter tempDataAdapter;
SqlCommandBuilder bldr; // needs to build to get update and other cmds.

string tableName = gridDataSet.Tables[0].TableName;
tempDataAdapter = new SqlDataAdapter("Select * from " + tableName, connection);
bldr = new SqlCommandBuilder(tempDataAdapter);
try
{
tempDataAdapter.Update(gridDataSet.Tables[tableName]);
}
catch (DBConcurrencyException ex)
{
System.Windows.Forms.MessageBox.Show("Someone else updated this data.");
}

}


This updates any changes I made anywhere in the data set.

Of course you need to replace ConnectString with yours and you may need to close the connection.

Does that help?
Post #1448450
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse