Im trying to use a datagridview to view and edit the tables data, i was using a command builder but i cannot update to multiple tables using it (because im using a view). The error i was recieving was - "Dynamic SQL generation is not supported against multiple base tables.". Was wondering if anyone could explain to me how to approach this thanks
Loading
Luke BakerPosted Apr 5, 2012, 1:26 PM
[CODE]
private SqlConnection connect;
private SqlDataAdapter da;
private DataTable dt;
string conStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\NTO.mdf;Integrated Security=True;User Instance=True";
public void Fill(string command, string update, string table)
{
connect = new SqlConnection(conStr);
connect.Open();
da = new SqlDataAdapter();
SqlCommand sqlCommand = new SqlCommand(command, connect);
da.SelectCommand = sqlCommand;
sqlCommand = new SqlCommand(update, connect);
da.UpdateCommand = sqlCommand;
dt = new DataTable(table);
da.Fill(dt);
}
public void Update()
{
if (da != null) da.Update(dt);
}
public void CloseConnection()
{
if (da != null && connect != null)
{
da.Dispose();
da = null;
connect.Close();
connect = null;
}
}
[/CODE]
I am calling this functions in this order
[CODE]
if (txtNew.Text == txtConfirmNew.Text)
{
select = "Select * from staff";
update = @"UPDATE staff SET password = '" + txtNew.Text + "'" +
"where staff_No = '" + Global.staffID + "'";
dc.Fill(select, update, "staff");
}
[/CODE]
[CODE]
private void button2_Click(object sender, EventArgs e)
{
dc.Update();
MessageBox.Show("updated");
}
[/CODE]
[CODE]
private void btnMenu_Click(object sender, EventArgs e)
{
MainMenu menu = new MainMenu();
this.Hide();
menu.Show();
dc.CloseConnection();
}
[/CODE]
VulpesPosted Apr 5, 2012, 1:03 PM
if (da != null && connect != null)
{
}
Luke BakerPosted Apr 5, 2012, 11:45 AM
VulpesPosted Apr 5, 2012, 11:22 AM
Luke BakerPosted Apr 5, 2012, 7:31 AM
[code]
public void update(string command,string update, string table)
{
SqlConnection connect = new SqlConnection(conStr);
connect.Open();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand sqlCommand = new SqlCommand(command,connect);
da.SelectCommand = sqlCommand;
sqlCommand = new SqlCommand(update, connect);
da.UpdateCommand = sqlCommand;
DataTable dt = new DataTable(table);
da.Fill(dt);
da.Update(dt);
connect.Close();
}
[/code]
and for some reason no update to the table is taking place
VulpesPosted Apr 3, 2012, 4:26 PM
When you call the SqlDataAdapter's Update() method, these commands will then be executed for each row in the table that has been inserted, updated or changed.
There's an example here on MSDN that should give you some idea of what to do:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.insertcommand.aspx