PROBLEM IN UPDATED database using grid view

Posted by Sachin19 under ASP.NET on 12/3/2010 | Points: 10 | Views : 1278 | Status : [Member] | Replies : 1
private void UpdateorAddNewRecord(string CustomerID, string CompanyName,string ContactName,string ContactTitle, string Address, string Country, bool isUpdate)
{
SqlConnection Connection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True");
string sqlStatement = string.Empty;

if(!isUpdate)
{
sqlStatement="insert into sachin"+"(CustomerID,CompanyName,ContactName,ContactTitle,Address,Country)"+"VALUES(@CustomerID,@CompanyName,@ContactName,@ContactTitle,@Address,@Country)";
}
else
{
sqlStatement = "UPDATE sachin" +
"SET CompanyName = @CompanyName,ContactName=@ContactName," +
"ContactTitle = @ContactTitle,Address= @Address,Country= @Country" +
" WHERE CustomerID = @CustomerID,";




}
try
{

Connection.Open();
SqlCommand cmd=new SqlCommand(sqlStatement,Connection);
cmd.Parameters.AddWithValue("@CustomerID",CustomerID);
cmd.Parameters.AddWithValue("@CompanyName",CompanyName);
cmd.Parameters.AddWithValue("@ContactName",ContactName);
cmd.Parameters.AddWithValue("@ContactTitle",ContactTitle);
cmd.Parameters.AddWithValue("Address",Address);
cmd.Parameters.AddWithValue("Country",Country);
cmd.CommandType=CommandType.Text;
cmd.ExecuteNonQuery();

}
catch (System.Data.SqlClient.SqlException ex){

string msg = "insert/update error";
msg+=ex.Message;
throw new Exception(msg);

}
finally {
Connection.Close();
}
}









WHEN I M RUNNING THE AAPPLICATION with gridview and usig store procedureSHOWING ERROR IN UPDATED




Responses

Posted by: T.saravanan on: 12/3/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi Sachin19,

I think error is in you SqlStatement..Check that String...


sqlStatement = "UPDATE sachin" +
"SET CompanyName = @CompanyName,ContactName=@ContactName," +
"ContactTitle = @ContactTitle,Address= @Address,Country= @Country" +
" WHERE CustomerID = @CustomerID, ";


Remove the comma in last of the string...


sqlStatement = "UPDATE sachin" +
"SET CompanyName = @CompanyName,ContactName=@ContactName," +
"ContactTitle = @ContactTitle,Address= @Address,Country= @Country" +
" WHERE CustomerID = @CustomerID";


Check it...



Thanks,
T.Saravanan

Sachin19, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response