Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Conversion failed when converting the varchar value 'hello ' to data type int.

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Invoice_Genraton
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        SqlDataAdapter da;
         DataSet ds;
         SqlConnection con;
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            con = new SqlConnection("Data Source=localhost;Initial Catalog=allcare;Integrated Security=True;Pooling=False");
            da = new SqlDataAdapter("insert into [prudhvi] values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','"
                                        + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" +
                                        textBox9.Text + "','" + textBox10.Text + "','" +  "')", con);
            ds = new DataSet();
            da.Fill(ds);
            Form2 f2 = new Form2();
            this.Hide();
            f2.label12.Text = textBox1.Text.ToString();
            f2.label13.Text = textBox2.Text.ToString();
            f2.label14.Text = textBox3.Text.ToString();
            f2.label15.Text = textBox4.Text.ToString();
            f2.label16.Text = textBox5.Text.ToString();
            f2.label17.Text = textBox6.Text.ToString();
            f2.label18.Text = textBox7.Text.ToString();
            f2.label19.Text = textBox8.Text.ToString();
            f2.label20.Text = textBox9.Text.ToString();
            f2.label21.Text = textBox10.Text.ToString();
            
            int n1 = int.Parse(textBox9.Text);
            int n2 = int.Parse(textBox10.Text);
            int total = n1 + n2;
            f2.label22.Text = total.ToString();
            f2.Show();


        }
    }
}
Posted
Updated 4-May-17 23:48pm
v2

Don't do it like that!
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
When you convert that to parameterized queries, you validate and convert all your values in your C# code, and pass error messages back to your user when they have mistyped - you then pass the validated and converted values directly to SQL:
C#
int iVal;
if (!int.TryParse(txtThisShouldBeANumber.Text, out iVal))
   {
   ... Report input problem to user ...
   return;
   }
using (da = new SqlDataAdapter("INSERT INTO MyTable (MyColumnName) VALUES (@NUM)", com))
   {
   da.InsertCommand.Parameters.AddWithValue("@NUM", iVal);
   ...
   }
It's more friendly for your users, easier to read, easier to maintain, and a damn site safer that passing control of your DB to the user! And as another advantage, your problem will disappear at the same time...

BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
Share this answer
 
Never build an SQL query by concatenating with user inputs, it is named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability.
SQL injection - Wikipedia[^]
SQL Injection[^]
 
Share this answer
 
Actually your exception is giving an answer of your question.


You are trying to insert varchar value to int field of a database

Either you should alter datatype of a column to varchar or you can insert int values only to that particular column

For better practice write column names also in Insert statement respective of values
e.g.

SQL
INSERT INTO TABLE_NAME (COLUMN1,COLUMN2...) VALUES (@VAL1,@VAL2)


Let me know if you have any query or concern for same.
 
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