Click here to Skip to main content
15,915,053 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created database and created table,after that I have created store procedure.
and the code in c#.net .During compilation I am not getting an error but at run time I am getting error "conversion failed when converting the varchar value '@id' data type int"
code in c#.net:-
C#
private void button1_Click(object sender, EventArgs e)
       {
           SqlConnection cn = new SqlConnection("server=DESKTOP-7QODVGC\\SQLEXPRESS;uid=sa;database=employee;password=cdac");
           cn.Open();
           SqlCommand cmd = new SqlCommand("spinsert_3", cn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.AddWithValue("Id",textBox1.Text);
           cmd.Parameters.AddWithValue("Name", textBox2.Text);
           try
           {
               cmd.ExecuteNonQuery();
               cn.Close();
               MessageBox.Show("Created");

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }


What I have tried:

created store procedure,written the code in c#.net
Posted
Comments
ZurdoDev 12-Feb-16 6:57am    
It sounds like that says you're trying to pass the string value "@id" as the ID. What is in textbox1?
Richard Deeming 12-Feb-16 9:38am    
It sounds like the error is being thrown from within the stored procedure. Update your question and add the definition of the stored procedure.
aurj 14-Feb-16 10:19am    
thanks Richard, error was there withtin the store procedure

Whatever is in textBox1.Text can't be converted to an int. You should always validate inputs from the user before using them, and you should also use the "@" when adding the parameter.

C#
int id = 0;
if (!int.TryParse(textBox1.Text, out id))
{
    // data isn't valid, do something to handle this
    return;
}

cmd.Parameters.AddWithValue("@Id", id);
 
Share this answer
 
Ok, so your not open to sql injection directly, but I would never advise using a textbox.text directly. You should handle errors first. Take this example:


C#
private void button1_Click(object sender, EventArgs e)
       {
           int id;
           if(!int.TryParse(textBox1.Text,out id) || id <= 0){
               //Alert the user.  if all is ok then you now have id as an int 
           }
           string name = textBox2.Text.trim();
           if(string.isNullOrEmpty(name)){          
              //Alert the user.  if all is ok then you have a clean 'name'
           }

           SqlConnection cn = new SqlConnection("server=DESKTOP-7QODVGC\\SQLEXPRESS;uid=sa;database=employee;password=cdac");
           cn.Open();
           SqlCommand cmd = new SqlCommand("spinsert_3", cn);
           cmd.CommandType = CommandType.StoredProcedure;
           
           //Also you can express the parameter as a type like this
           cmd.Parameters.Add("Id",SqlDbType.Int){Value=id};
           cmd.Parameters.Add("Name",SqlDbType.VarChar){Value=name};
           try
           {
               cmd.ExecuteNonQuery();
               cn.Close();
               MessageBox.Show("Created");
 
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }


That will hopefully at least catch the issue

let me know ^_^
Andy
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900