Sunday, March 25, 2012

Conversion of int data type error?!

Hi,

I keep getting the error:

System.Data.SqlClient.SqlException: Conversion failed when converting the varchar value '@.qty' to data type int.

When I initiate the insert and update.

I tried adding a: Convert.ToInt32(TextBox1.Text), but it didn't work..

Could someone help?

My 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);
}
}

Thanks so much if someone can!

Jon

Hi,

I think the problem lies in your Command Text. Try this:


command.CommandText = "UPDATE Items SET Quantityavailable = Quantityavailable - " + @.qty +" WHERE productID=@.productID";

Hope this helps.

|||

Hi,

I tried it but it gave me a different error message saying qty doesnt exist.

But actually the update seems to work - I think the problem lies with the insert commands..

Thanks,

Jon

|||

In your original post, you need to remove the single quotes around @.qty.

|||

sswanner1:

In your original post, you need to remove the single quotes around @.qty.

Hi,

I put them in when I got a syntax error 'near WHERE'..

If I take them away the error comes back..

|||

command.Parameters.Add("@.qty", TextBox1.Text); //need to convert into integer like Convert.ToInt32(TextBox1.Text)

TextBox.Text is string rather than integer. You need to valify and convert it to integer.


|||

Hi,

You mean just change the update parameter to the same as the insert parameter (@.qty)?

If so, I have done that but it still gives the same error...

Thanks,

Jon

|||

int qty = 0;
TextBox TextBox1 = (TextBox)FormView1.FindControl("TextBox1");

if(TextBox1 != null)

{

qty = int.parse(TextBox1.Text);

}

catch{}

command.CommandText = "UPDATE Items SET Quantityavailable = Quantityavailable -@.qty WHERE productID=@.productID";
command.Parameters.Add("@.qty",qty);
command.Parameters.Add("@.productID", labid.Text);
command.ExecuteNonQuery();

Following my code, and do it for both methods. And, you need to do same for @.productId.|||

Hi,

che3358:

if(TextBox1 != null)

{

qty = int.parse(TextBox1.Text);

}

catch{}

Gives me a squiggly before catch{}, saying 'try' is expected? then when I type try it gives more syntax errors?

Thanks,

Jon

|||

You will have to convert TextBox1.text to int . Try using ,

int qty = int.parse(TextBox1.text);

command.Parameters.Add("@.qty",SqlDbType.Int);

command..Parameters["@.qty"].Value = qty ;

|||

My fault. It should be

if(TextBox1 != null)

{

try

{

qty = int.parse(TextBox1.Text);

}

catch{}

}



|||

Hi again,

Now it gives the error:

CS0117: 'int' does not contain a definition for 'parse'

Line 40: qty = int.parse(TextBox1.Text);
 
??
Thanks again!
Jon 

|||

int.Parse. Sorry.

|||

Hi,

New error:

CS0103: The name 'int32' does not exist in the current context

Line 40: qty = int32.parse(TextBox1.Text);
 
Cheers
Jon 

|||

it needs to be exactly as :

qty = int.Parse(TextBox1.Text);

Tim

No comments:

Post a Comment