Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 29, 2012 09:20 AM by Chen Yu - MSFT
Member
6 Points
28 Posts
May 23, 2012 09:26 PM|LINK
Hello , i need help i can´t save data to database , the next code show error on "cmd.Parameters.AddWithValue("@iva", checkbox1.checked);"
// fields in sql database // Cod : nvarchar // iva : bit
try { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Micon5"].ToString()); con.Open(); sqlCommand cmd = new SqlCommand("update table1 set iva=@iva where cod=@cod ", con); cmd.Parameters.AddWithValue("@codigo", txtcod.Text); cmd.Parameters.AddWithValue("@iva", checkbox1.checked); cmd.ExecuteNonQuery(); }
catch (System.Data.SqlClient.SqlException ex) { string msg = "Fetch Error:"; msg += ex.Message; }
--------------------
thanks
Contributor
2522 Points
376 Posts
May 23, 2012 09:46 PM|LINK
Try changing the name of the parameter:
cmd.Parameters.AddWithValue("@cod", txtcod.Text);
If not working, post the error message appears.
Hope this helps.
May 23, 2012 09:53 PM|LINK
oops, sorry, it was bad copied , the correct its like you wrote.
but,the error is in the line :
cmd.Parameters.AddWithValue("@iva", Checkbox1.Checked );
May 23, 2012 10:12 PM|LINK
this is the error message in "ex.Message" System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near 'iva'. en System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) en System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) en System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() en System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) en System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) en System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) en System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) en System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) en System.Data.SqlClient.SqlCommand.ExecuteNonQuery() en S1_ramos.btnUpdate_Click(Object sender, EventArgs e) en d:\Empresas\WebSites\AppEnlaceWeb\S1\ramos.aspx.cs:línea 123
May 23, 2012 11:00 PM|LINK
Thanks , i solved already the problem. I re-install Visual Studio, i did not know what happened, but now is working fine.
Thanks
May 23, 2012 11:06 PM|LINK
This is the complete code, and it is working fine :
protected void btnUpdate_Click(object sender, EventArgs e) { lblresult.Text = ""; try { // iva is a Bit Fields in database
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Micon5"].ToString()); con.Open(); SqlCommand cmd = new SqlCommand("update ramos set codigo=@codigo, ramo=@ramo, iva=@iva where ramo=@antramo", con); cmd.Parameters.AddWithValue("@codigo", txtcodigo.Text); cmd.Parameters.AddWithValue("@ramo", txtramo.Text); cmd.Parameters.AddWithValue("@iva", Checkbox1.Checked); cmd.Parameters.AddWithValue("@antramo", lblhidden1.Text); cmd.ExecuteNonQuery(); lblresult.Text = " Updated!"; } catch (Exception ex) { lblresult.Text = ex.ToString(); //lblresult.Text = "Error updating!!!"; } finally { con.Close(); } }
----------------------------
Thanks againg
All-Star
21600 Points
2493 Posts
Microsoft
May 29, 2012 09:20 AM|LINK
Hi joelcor,
Thanks for sharing your solution. It will help others if they meet similar issues.
Thanks.
joelcor
Member
6 Points
28 Posts
errror saving checkbox to database
May 23, 2012 09:26 PM|LINK
Hello , i need help i can´t save data to database , the next code show error on "cmd.Parameters.AddWithValue("@iva", checkbox1.checked);"
// fields in sql database
// Cod : nvarchar
// iva : bit
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Micon5"].ToString());
con.Open();
sqlCommand cmd = new SqlCommand("update table1 set iva=@iva where cod=@cod ", con);
cmd.Parameters.AddWithValue("@codigo", txtcod.Text);
cmd.Parameters.AddWithValue("@iva", checkbox1.checked);
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
}
--------------------
thanks
spapim
Contributor
2522 Points
376 Posts
Re: errror saving checkbox to database
May 23, 2012 09:46 PM|LINK
Try changing the name of the parameter:
cmd.Parameters.AddWithValue("@cod", txtcod.Text);If not working, post the error message appears.
Hope this helps.
www.imobiliariasemsuzano.com.br
joelcor
Member
6 Points
28 Posts
Re: errror saving checkbox to database
May 23, 2012 09:53 PM|LINK
oops, sorry, it was bad copied , the correct its like you wrote.
cmd.Parameters.AddWithValue("@cod", txtcod.Text);
but,the error is in the line :
cmd.Parameters.AddWithValue("@iva", Checkbox1.Checked );
joelcor
Member
6 Points
28 Posts
Re: errror saving checkbox to database
May 23, 2012 10:12 PM|LINK
this is the error message in "ex.Message"
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near 'iva'. en System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) en System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) en System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() en System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) en System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) en System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) en System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) en System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) en System.Data.SqlClient.SqlCommand.ExecuteNonQuery() en S1_ramos.btnUpdate_Click(Object sender, EventArgs e) en d:\Empresas\WebSites\AppEnlaceWeb\S1\ramos.aspx.cs:línea 123
joelcor
Member
6 Points
28 Posts
Re: errror saving checkbox to database
May 23, 2012 11:00 PM|LINK
Thanks , i solved already the problem.
I re-install Visual Studio, i did not know what happened, but now is working fine.
Thanks
joelcor
Member
6 Points
28 Posts
Re: errror saving checkbox to database
May 23, 2012 11:06 PM|LINK
This is the complete code, and it is working fine :
protected void btnUpdate_Click(object sender, EventArgs e)
{
lblresult.Text = "";
try
{
// iva is a Bit Fields in database
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Micon5"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("update ramos set codigo=@codigo, ramo=@ramo, iva=@iva where ramo=@antramo", con);
cmd.Parameters.AddWithValue("@codigo", txtcodigo.Text);
cmd.Parameters.AddWithValue("@ramo", txtramo.Text);
cmd.Parameters.AddWithValue("@iva", Checkbox1.Checked);
cmd.Parameters.AddWithValue("@antramo", lblhidden1.Text);
cmd.ExecuteNonQuery();
lblresult.Text = " Updated!";
}
catch (Exception ex)
{
lblresult.Text = ex.ToString();
//lblresult.Text = "Error updating!!!";
}
finally
{
con.Close();
}
}
----------------------------
Thanks againg
Chen Yu - MS...
All-Star
21600 Points
2493 Posts
Microsoft
Re: errror saving checkbox to database
May 29, 2012 09:20 AM|LINK
Hi joelcor,
Thanks for sharing your solution. It will help others if they meet similar issues.
Thanks.
Feedback to us
Develop and promote your apps in Windows Store