Click here to Skip to main content
15,797,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
This is my Insert Button Code:

C#
private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                cmd.Parameters.Clear();
      cmd.CommandText = "Ins_Upd_Students";//Ins_Upd_Students is my stored procedure
                cmd.Parameters.AddWithValue("@Sno", textBox1.Text);
                cmd.Parameters.AddWithValue("@Sname", textBox2.Text);
                cmd.Parameters.AddWithValue("@Class", textBox3.Text);
                cmd.Parameters.AddWithValue("@Fees", textBox4.Text);
                con.Open();
                int affectedRecord=cmd.ExecuteNonQuery();
                MessageBox.Show("Executed");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }


I declared all my table column as varchar(50) type.
At the time of insertion it shows the error as cannot convert data type nvarchar into int.
Can any one tel me why I'm getting that error and how to rectify that error.
Thank you so much
Posted
Updated 17-Aug-11 0:35am
v2

Convert to integer.
C#
cmd.Parameters.AddWithValue("@Sno", Convert.ToInt32(textBox1.Text));


or use
VB
cmd.Parameters.Add("@Sno", SqlDbType.Int);
cmd.Parameters("@Sno").Value = sno;
 
Share this answer
 
v2
Comments
usha nelavalli 17-Aug-11 6:03am    
I changed code as cmd.Parameters.AddWithValue("@Sno", Convert.ToInt32(textBox1.Text));
but it gives the error as input string was not in correct format.
after that i changed as
cmd.Parameters.AddWithValue("@Sno", Convert.ToInt32(textBox1.Text.ToString()));
after this also i'm getting the error as input string was not in correct fromat.
usha nelavalli 17-Aug-11 6:04am    
can you help me to over come this error
You would have mentioned INT for some fields in Stored Procedure. Please change all to varchar in Stored Procedure too.
 
Share this answer
 
The problem is inside the stored procedure, You're most likely using a varchar parameter but you're trying to put the value into an int column. Check that all the parameters of the stored procedure are exactly the same datatype as the columns in your table.
 
Share this answer
 
If Sno, or any of the other parameters, is not a text parameter - you need to change AddWithValue("@Sno", textBox1.Text); into something like AddWithValue("@Sno", int.Parse(textBox1.Text)); if Sno is an integer.

Best regards
Espen Harlinn
 
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