SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Updating information in a datagrid by using a button


Updating information in a datagrid by using a button

Author
Message
Meatloaf
Meatloaf
SSC Veteran
SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)

Group: General Forum Members
Points: 202 Visits: 421
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!
Meatloaf
Meatloaf
SSC Veteran
SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)

Group: General Forum Members
Points: 202 Visits: 421
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();
}
timwell
timwell
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1373 Visits: 3072
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?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search