8
Answers

Error updating table in data grid view in c#

mohit gupta

mohit gupta

10y
3.6k
1
(1)Update requires a valid UpdateCommand when passed DataRow collection with modified rows.OR
(2) Dynamic SQL generation is not supported against multiple base tables.
I using join query and updating.but error is display above.
my code is.....
public partial class Form5 : Form
{
string con = "Data Source=ITSW2;Initial Catalog=Export;Integrated Security=True";

private SqlConnection connection;
private SqlCommand command;
private SqlDataAdapter adapter;
private SqlCommandBuilder builder;
private DataSet ds;

private DataTable userTable;


public Form5()
{
InitializeComponent();
}

private void btnload_Click(object sender, EventArgs e)
{



string query = "select A.partno, A.BoxNo,A.partdesc,A.partypartno,A.boxqty,A.stdwt,A.netwt,A.grosswt from BoxPacking A join Packing B on A.DespLotId=B.DespLotId where B.PackingId=1525"
connection = new SqlConnection(con);
connection.Open();
command = new SqlCommand(query, connection);
adapter = new SqlDataAdapter(command);
//builder= new SqlCommandBuilder(adapter);
ds = new DataSet();
adapter.Fill(ds, "NewTable");


userTable = ds.Tables["NewTable"];


connection.Close();
dataGridView1.DataSource = userTable;
dataGridView1.ReadOnly = true;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

edit.Enabled = true;
btnupdate.Enabled = true;


}



private void btnupdate_Click(object sender, EventArgs e)
{
try
{
connection.Open();
adapter.Update(userTable);
dataGridView1.ReadOnly = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
btnupdate.Enabled = true;
}
finally
{
edit.Enabled = true;
btnload.Enabled = true;
connection.Close();
}
}

private void edit_Click(object sender, EventArgs e)
{
try
{
dataGridView1.ReadOnly = false;
edit.Enabled = false;
btnupdate.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}


}
}
}

Answers (8)