Reputation: 25
I am trying to create Update function for my windows applications for some reason it's not working and come up with this error "System.Data.SqlClient.SqlException: 'Incorrect syntax near '('.'" Any suggestions?
private void Update_Click(object sender, EventArgs e)
{
if (txt_BranchID.Text != "" && txt_BranchName.Text != "" && txt_BranchAddress.Text != "" && txt_Telephone.Text != "")
{
cmd = new SqlCommand("Update BInfo(BranchID, BranchName, BranchAddress, Telephone) values(@BranchID, @BranchName, @BranchAddress, @Telephone)", con);
con.Open();
cmd.Parameters.AddWithValue("@BranchName", txt_BranchName.Text);
cmd.Parameters.AddWithValue("@BranchAddress", txt_BranchAddress.Text);
cmd.Parameters.AddWithValue("@Telephone", txt_Telephone.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Record updated Successfully");
con.Close();
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Update");
}
Upvotes: 0
Views: 44
Reputation: 25
Here is an Answer for my question.
private void Update_Click(object sender, EventArgs e) {
if (txt_BranchID.Text != "" && txt_BranchName.Text != "" && txt_BranchAddress.Text != "" && txt_Telephone.Text != "")
{
cmd = new SqlCommand("update BInfo set BranchID=@BranchID,Branchname=@BranchName, BranchAddress=@BranchAddress, Telephone=@Telephone where BranchID=@BranchId", con);
con.Open();
cmd.Parameters.AddWithValue("@BranchID", txt_BranchID.Text);
cmd.Parameters.AddWithValue("@BranchName", txt_BranchName.Text);
cmd.Parameters.AddWithValue("@BranchAddress", txt_BranchAddress.Text);
cmd.Parameters.AddWithValue("@Telephone", txt_Telephone.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Record updated Successfully");
con.Close();
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Update");
}
}
Upvotes: 0
Reputation: 62488
the syntax for update query is different than Insert query. It should look like:
update BInfo
SET
BranchName = @BranchName,
BranchAddress = @BranchAddress,
Telephone = @Telephone
where BranchID = @BranchID
and there should also be a where
clause specifying which Branch information we are going to update so at last line put where
in string form in the code it would be like:
cmd = new SqlCommand(@"Update BInfo
SET
BranchName = @BranchName,
BranchAddress = @BranchAddress,
Telephone = @Telephone
where BranchID = @BranchID",con);
Also @BranchId
is missing in your command parameters, it would be needed there:
cmd.Parameters.AddWithValue("@BranchID", "branchId here");
Upvotes: 1