0

I would like to add a record in my database.

There is my code below :

private void buttonAdd_Click(object sender, EventArgs e)
{
    con.Open();

    SqlDataAdapter sda = new SqlDataAdapter("Insert into RDV (idUser,idClient,objet,objectif,DateRdv,commentaire)values('"+comboBox1.Text+"','"+comboBox2.Text+ "','" + textBox1.Text + "','" + textBox2.Text + "','" + dateTimePicker1.Text.ToString() + "','" + textBox4.Text + "')", con);

    sda.SelectCommand.ExecuteNonQuery();
    con.Close();

    MessageBox.Show("Le RDV a été ajouté avec succés !");
}

but an error happens:

Conversion failed when converting the varchar value 'John' to data type int.

How should I convert it? Please help me.

Thanks in advance.

3
  • Could you show the datatype of each column in table RDV ? Commented May 9, 2016 at 11:22
  • remove single quotes from '"+comboBox1.Text+"','"+comboBox2.Text+ "','" and It should be comboBox1.SelectedValue Commented May 9, 2016 at 11:22
  • @PhamX.Bach CREATE TABLE [dbo].[RDV] ( [idRdv] INT NOT NULL, [objet] NVARCHAR (50) NULL, [objectif] NVARCHAR (50) NULL, [DateRdv] DATETIME NULL, [commentaire] NVARCHAR (50) NULL, [archive] NVARCHAR (50) NULL, [idClient] INT NULL, [idUser] INT NULL, [idResultat] INT NULL, CONSTRAINT [PK_RDV] PRIMARY KEY CLUSTERED ([idRdv] ASC), FOREIGN KEY ([idClient]) REFERENCES [dbo].[Client] ([idClient]), FOREIGN KEY ([idUser]) REFERENCES [dbo].[User] ([idUser]), Commented May 9, 2016 at 11:32

3 Answers 3

0

For your information about your table RDV, the problem is 2 fields: idUser and idClient. I think you should:

  1. Check if your comboBox1 and comboBox2 (namming it well please) should have value of idUser and idClient in int type (for asp combobox, it has value and text. Text often for display to users and value is hidden, can be used for getting id like this).

  2. Get the right ids by using comboBox1.SelectedValue and comboBox2.SelectedValue :

    SqlDataAdapter sda = new SqlDataAdapter("Insert into RDV(idUser,idClient,objet,objectif,DateRdv,commentaire)values("+comboBox1.SelectedValue+","+comboBox2.SelectedValue+ ",'" + textBox1.Text + "','" + textBox2.Text + "','" + dateTimePicker1.Text.ToString() + "','" + textBox4.Text + "')", con);

Just delete the ' so it will be values(1, 4, 'the obj',...) instead of values('1', '4', 'the obj',...)

Sign up to request clarification or add additional context in comments.

2 Comments

The error now was: ** Incorrect Syntaxe to 'John,Lys,'** (john is username, lys name of client)
First you must bind idUser and idClient to your comboboxes value not text. Then you get the value in int type like 1, 4,..., not string like john or lys. Second, you should delete all the single quote ' around the value of combobox like my code in 2.
0

I assume that you're trying to insert 'John' into the idUser field, which is actually an int.

You're only getting the text from combobox1, whereas you should most likely be getting the value from the selection.

something like (int)combobox1.SelectedValue will get the correct value from the ComboBox.

--UPDATE--

Lets for instance say that you're binding your data to the ComboBox as follows:

combobox1.DataSource = //whatever your data source is

Then you would set the DisplayMember and ValueMember as follows

combobox1.DisplayMember = "username"; //assuming this is the column name
combobox1.ValueMember = "idUser";

So now that the combobox has the ValueMember attribute set you can then get it as follows

int userId = (int)combobox1.SelectedValue;

7 Comments

when I do (int)combobox1.Text ,it underligne it and show me an error Cannot convert sting to int
That's what should happen. It should be (int)combobox1.Value, to get value, not the text.
when i wrote (int)combobox1 then . in the list doesn't exist Value ("Combobox doesn't contain the definition Value")
Sorry my bad. combobox1.SelectedValue
Another Error **Specified cast is not valid **
|
0

First of all, you just check insert statement with direct value into Sqlserver Management Studio that is there any issue. In case you get, change the value, so you have the raw data to insert. Then try to same data via application.

Step 1 Try to check insert statement in SSMS with the actual value (Also give single quote only to character datatype) like

Insert into RDV (idUser,idClient,objet,objectif,DateRdv,commentaire)

values
( youractualIntValue, youractualIntValue,'youractualStringValue','youractualStringValue','youractualDateTimeValue','youractualStringValue')

Step2 - now use the same key with variable name.

The message shows, that your column is not character datatype i.e. int datatype as per error.

//Logic:- Break the code to identify the issue. first individual take variable and pass. Also just debug the variables, one by one and you definately get the error.

int idUser =  Convert.ToInt32( YourControlvalue); //yourcontrolvalue means  combobox or textbox value;
int idClient = Convert.ToInt32( YourControlvalue);
string objet = Convert.ToString( YourControlvalue);
....

Insert into RDV 
(idUser,idClient,objet,objectif,DateRdv,commentaire)

values
('" + idUser  + "','" + idClient + "','" + objet + "','" + objectif + "','" + DateRdv + "','" + commentaire + "')", con);

Sql inline query with parameters. Parameter is not read when the query is executed

6 Comments

Oh my god !, there is agin an Error the input string format is incorrect in the first line int idUser = Convert.ToInt32(comboBox1.SelectedValue);
are you check, insert statement with all value in SSMS? then try code
No,it's not my first insert, I have some records in SSMS
I execute my INSERT in SSMS and show me the error **Failed converting the varchar value idUser in int data type **
what is your statement?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.