Hi,
I have an error message:
Exception Details:System.Data.SqlClient.SqlException: Conversion failed when converting the varchar value '@.qty' to data type int.
It doesnt say which line of code it is reffering to however..
Could someone help me?
The parameter for @.qty is Textbox1.Text - maybe I could make the text box int only or something?
My code is:
private bool ExecuteUpdate(int quantity)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True";
con.Open();
SqlCommand command = new SqlCommand();
command.Connection = con;
TextBox TextBox1 = (TextBox)FormView1.FindControl("TextBox1");
Label labname = (Label)FormView1.FindControl("Label3");
Label labid = (Label)FormView1.FindControl("Label13");
command.CommandText = "UPDATE Items SET Quantityavailable = Quantityavailable - @.qty WHERE productID=@.productID";
command.Parameters.Add("@.qty", TextBox1.Text);
command.Parameters.Add("@.productID", labid.Text);
command.ExecuteNonQuery();
con.Close();
return true;
}
private bool ExecuteInsert(String quantity)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True";
con.Open();
SqlCommand command = new SqlCommand();
command.Connection = con;
TextBox TextBox1 = (TextBox)FormView1.FindControl("TextBox1");
Label labname = (Label)FormView1.FindControl("Label3");
Label labid = (Label)FormView1.FindControl("Label13");
command.CommandText = "INSERT INTO Transactions (Usersname,Itemid,itemname,Date,Qty) VALUES ('@.User','@.productID','@.Itemsname',@.date,'@.qty')";
command.Parameters.Add("@.User", System.Web.HttpContext.Current.User.Identity.Name);
command.Parameters.Add("@.Itemsname", labname.Text);
command.Parameters.Add("@.productID", labid.Text);
command.Parameters.Add("@.qty", TextBox1.Text);
command.Parameters.Add("@.date", DateTime.Now.ToString());
command.ExecuteNonQuery();
con.Close();
return true;
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox TextBox1 = FormView1.FindControl("TextBox1") as TextBox;
ExecuteUpdate(Int32.Parse(TextBox1.Text) );
}
protected void Button2_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Update")
{
TextBox TextBox1 = FormView1.FindControl("TextBox1") as TextBox;
ExecuteInsert(TextBox1.Text);
}
}
Thanks to anyone who can help!!!
Jon
jbear123:
Exception Details:System.Data.SqlClient.SqlException: Conversion failed when converting the varchar value '@.qty' to data type int.
Well, the error clearly says that the server is expecting int type value for the parameter called @.qty and you are providing either nothing or some value which is not int for @.qty.
First, make sure that the value in your textbox TextBox1 in of type int.
Second, for better confirmation, pass the cast(ed) value to the @.qty parameter, like Convert.ToInt32(TextBox1.text). This way you'll immediately get notified on the line where you try to convert it to int if the textbox is holding non int values.
jbear123:
command.CommandText = "INSERT INTO Transactions (Usersname,Itemid,itemname,Date,Qty) VALUES ('@.User','@.productID','@.Itemsname',@.date,'@.qty')";
I don't know whether this line is being executed or not, but anyway you'll get an error if at all this line gets executed. The problem is in the insert query you've written. You don't need to add ' (single quotation) around the parameters. It is required only if you're passing the values directly in the query.
Hope this will help.
|||Hi,
dhimant:
First, make sure that the value in your textbox TextBox1 in of type int.
Just make sure the value typed in is int? Or do i have to edit the settings to make the textbox int only?
The value typed in is always int..
dhimant:
Second, for better confirmation, pass the cast(ed) value to the @.qty parameter, like Convert.ToInt32(TextBox1.text). This way you'll immediately get notified on the line where you try to convert it to int if the textbox is holding non int values.
Where should I put the Convert.ToInt32(TextBox1.text) code?
dhimant:
I don't know whether this line is being executed or not, but anyway you'll get an error if at all this line gets executed. The problem is in the insert query you've written. You don't need to add ' (single quotation) around the parameters. It is required only if you're passing the values directly in the query.
Thanks! Done!
Thanks alot,
Jon
|||jbear123:
Where should I put the Convert.ToInt32(TextBox1.text) co
Just where you're setting the value for the command parameter value. Instead of just TetBox1.Text write Convert.ToInt32(TextBox1.Text).
Hi,
I had tried that it gives me the same error:
System.Data.SqlClient.SqlException: Conversion failed when converting the varchar value '@.qty' to data type int.
..?
Cheers,
Jon
||| Now, can you re-post the all modified code again ? I don't see any problem if you've made all the correction. One more thing, Why are you using both button2_command and click events and what are you trying to do with them ?
Hi,
Code:
private bool ExecuteUpdate(int quantity)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True";
con.Open();
SqlCommand command = new SqlCommand();
command.Connection = con;
TextBox TextBox1 = (TextBox)FormView1.FindControl("TextBox1");
Label labname = (Label)FormView1.FindControl("Label3");
Label labid = (Label)FormView1.FindControl("Label13");
command.CommandText = "UPDATE Items SET Quantityavailable = Quantityavailable - '@.qty' WHERE productID=@.productID";
command.Parameters.Add("@.qty", TextBox1.Text);
command.Parameters.Add("@.productID", labid.Text);
command.ExecuteNonQuery();
con.Close();
return true;
}
private bool ExecuteInsert(String quantity)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True";
con.Open();
SqlCommand command = new SqlCommand();
command.Connection = con;
TextBox TextBox1 = (TextBox)FormView1.FindControl("TextBox1");
Label labname = (Label)FormView1.FindControl("Label3");
Label labid = (Label)FormView1.FindControl("Label13");
command.CommandText = "INSERT INTO Transactions (Usersname,Itemid,itemname,Date,Qty) VALUES (@.User,@.productID,@.Itemsname,@.date,@.qty)";
command.Parameters.Add("@.User", System.Web.HttpContext.Current.User.Identity.Name);
command.Parameters.Add("@.Itemsname", labname.Text);
command.Parameters.Add("@.productID", labid.Text);
command.Parameters.Add("@.qty", Convert.ToInt32(TextBox1.Text));
command.Parameters.Add("@.date", DateTime.Now.ToString());
command.ExecuteNonQuery();
con.Close();
return true;
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox TextBox1 = FormView1.FindControl("TextBox1") as TextBox;
ExecuteUpdate(Int32.Parse(TextBox1.Text) );
}
protected void Button2_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Update")
{
TextBox TextBox1 = FormView1.FindControl("TextBox1") as TextBox;
ExecuteInsert(TextBox1.Text);
}
}
}
dhimant:
One more thing, Why are you using both button2_command and click events and what are you trying to do with them ?
I used them to initiate update and insert, someone suggested that I didnt put them both in button click? Should I?
Thanks again!
Jon
No comments:
Post a Comment