Database updation through datagridview

N

Neeraj

hi
I am using datagridview and sql Express as a datasource i populate
data but not able to update through datagridview
i am using such code

dataAdpter = new System.Data.SqlClient.SqlDataAdapter("select MGenID,
General, status from General where status = 1", connect);
dataset = new DataSet();
dataAdpter.Fill(dataset, nodeValue);
grdMastervalue.DataSource = dataset.Tables[0];

data is showing proporly

for update the data i write code on button click event

private void btnEdit_Click(object sender, EventArgs e)
{
this.Validate();
this.grdMastervalue.EndEdit();
this.dataAdpter.Update(this.dataset, nodeValue);


}

then last line generate exception

An unhandled exception of type 'System.InvalidOperationException'
occurred in System.Data.dll

Additional information: Update requires a valid UpdateCommand when
passed DataRow collection with modified rows.

Please tell where i am wrong

thanks
Neeraj Kumar
 
G

Guest

I don't know if I am helping but htis code below works for me. I am saving to
two tables below:

this.Validate();
this.scriptBindingSource.EndEdit();
this.scriptIDBindingSource.EndEdit();

try
{
// Update the Script tables.
scriptIDTableAdapter.Update(fixed1DataSet.ScriptID);
scriptTableAdapter.Update(fixed1DataSet.Script);

}

catch (System.Exception ex)
{
MessageBox.Show("Operation failed: " + ex.ToString),
Application.ProductName + " - Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
 
B

bob

Hi Neeraj,
You need to create an update command and attach it to the dataadapter,

Some working code follows
Seeing you are not using stored procedures. The Update and insert
strings become SQL text with parameter holders.
e.g.(From memory)
Update myTable set myColumn = ? where myOtherColumn = ?
Ignore the factory use in the following code.
You can see making of:
The command
The command string
The parameters and their binding to the dataset columns
The attachment of the command to the dataadapter
hth
Bob
public bool WriteBatchStatus(dsBatchAssignable ds)
{
string strSQL;
long j=0;
DbCommand cmdConn = df.CreateCommand();
DbDataAdapter dUpdate = df.CreateDataAdapter();
DbConnection Con = df.CreateConnection();

try
{
strSQL = "proc_WriteBatchStatus";
Con.ConnectionString = mstrConnectionString;
cmdConn.Connection = Con;
DbParameter pBId = df.CreateParameter();
DbParameter pSId = df.CreateParameter();
pBId.ParameterName = "@Batch_id";
pSId.ParameterName = "@Supplier_id";
pBId.DbType = DbType.Int32;
pSId.DbType = DbType.Int32;

pBId.SourceColumn = "id";
pSId.SourceColumn = "supplier_id";

cmdConn.Parameters.Add(pBId);
cmdConn.Parameters.Add(pSId);


cmdConn.CommandText = strSQL;

cmdConn.CommandType = CommandType.StoredProcedure;
dUpdate.UpdateCommand = cmdConn;

cmdConn.Connection.Open();
dUpdate.Update(ds.Tables["batchassignable"]);

cmdConn.Connection.Close();
cmdConn.Dispose();
return true;
}
 
N

Neeraj

Hi Neeraj,
You need to create an update command and attach it to the dataadapter,

Some working code follows
Seeing you are not using stored procedures. The Update and insert
strings become SQL text with parameter holders.
e.g.(From memory)
Update myTable set myColumn = ? where myOtherColumn = ?
Ignore the factory use in the following code.
You can see making of:
The command
The command string
The parameters and their binding to the dataset columns
The attachment of the command to the dataadapter
hth
Bob
public bool WriteBatchStatus(dsBatchAssignable ds)
{
string strSQL;
long j=0;
DbCommand cmdConn = df.CreateCommand();
DbDataAdapter dUpdate = df.CreateDataAdapter();
DbConnection Con = df.CreateConnection();

try
{
strSQL = "proc_WriteBatchStatus";
Con.ConnectionString = mstrConnectionString;
cmdConn.Connection = Con;
DbParameter pBId = df.CreateParameter();
DbParameter pSId = df.CreateParameter();
pBId.ParameterName = "@Batch_id";
pSId.ParameterName = "@Supplier_id";
pBId.DbType = DbType.Int32;
pSId.DbType = DbType.Int32;

pBId.SourceColumn = "id";
pSId.SourceColumn = "supplier_id";

cmdConn.Parameters.Add(pBId);
cmdConn.Parameters.Add(pSId);

cmdConn.CommandText = strSQL;

cmdConn.CommandType = CommandType.StoredProcedure;
dUpdate.UpdateCommand = cmdConn;

cmdConn.Connection.Open();
dUpdate.Update(ds.Tables["batchassignable"]);

cmdConn.Connection.Close();
cmdConn.Dispose();
return true;
}
hi
I am using datagridview and sql Express as a datasource i populate
data but not able to update through datagridview
i am using such code
dataAdpter = new System.Data.SqlClient.SqlDataAdapter("select MGenID,
General, status from General where status = 1", connect);
dataset = new DataSet();
dataAdpter.Fill(dataset, nodeValue);
grdMastervalue.DataSource = dataset.Tables[0];
data is showing proporly
for update the data i write code on button click event
private void btnEdit_Click(object sender, EventArgs e)
{
this.Validate();
this.grdMastervalue.EndEdit();
this.dataAdpter.Update(this.dataset, nodeValue);

then last line generate exception
An unhandled exception of type 'System.InvalidOperationException'
occurred in System.Data.dll
Additional information: Update requires a valid UpdateCommand when
passed DataRow collection with modified rows.
Please tell where i am wrong
thanks
Neeraj Kumar- Hide quoted text -

- Show quoted text -

Thanks Bob
I got it
 
Top