I have a form (F1) where user will provide their respective credentials username and password . After a sucessfull login , the controls moves to the Client Form (F2) and display welcome username in a label on it.
Client Form contains:
- labels and textboxes(name, address, function , ...)
- Button Insert
- DataGridView bind to DB (name, address, function ,.., UserId)
After filling textboxes, I want to add a Client a show it added by user who's connected.
Ex: if I logged with Username Rose after that add a client, in my datagridView , show me my row of insert added by Rose.
Here is my code of Insert:
- private void button1_Click(object sender, EventArgs e)
- {
- if ( comboBox2.SelectedValue != null && textBox1.Text != string.Empty && textBox2.Text != string.Empty && textBox4.Text != string.Empty)
- {
- string cmdStr = "Insert into Client (idUser,name,address,function,telephone,commentaire)values (@idUser,@name,@,address,@function,@telephone,@commentaire)";
- SqlConnection con = new SqlConnection("Data Source=User-PC\\SQLEXPRESS;Initial Catalog=timar;Integrated Security=True");
- SqlCommand cmd = new SqlCommand(cmdStr, con);
- con.Open();
- //The problem in this code how Can I get the id of username,Error cannot convert string Rose to int.
- cmd.Parameters.AddWithValue("@idUser",label.Text);
- cmd.Parameters.AddWithValue("@name", (comboBox2.SelectedValue));
- cmd.Parameters.AddWithValue("@,address", textBox1.Text);
- cmd.Parameters.AddWithValue("@function", textBox2.Text);
- cmd.Parameters.AddWithValue("@telephone", textBox4.Text);
- cmd.Parameters.AddWithValue("@commentaire",txtArchive.Text);
- int LA = cmd.ExecuteNonQuery();
- con.Close();
- MessageBox.Show("Le Client a été ajouter avec succés !","Saisie Rendez-vous", MessageBoxButtons.OK, MessageBoxIcon.Information);
- DisplayData();
- ClearData();
- }
- else
- {
- MessageBox.Show("Vérifier que tous les champs sont remplis !","Erreur",MessageBoxButtons.OK,MessageBoxIcon.Information);
- }
- }
I am not able to figure out how to do this, i am very new to c# and trying to learn.
Thanks in Advance .
ketan borsdiyaPosted Jul 8, 2016, 7:41 AM
Yosra ChourouPosted Jul 8, 2016, 7:36 AM
ketan borsdiyaPosted Jul 8, 2016, 7:19 AM
Yosra ChourouPosted Jul 8, 2016, 6:50 AM
The problem isn't incmd.Parameters.AddWithValue("@name", (comboBox2.Text)); but in cmd.Parameters.AddWithValue("@idUser",label.Text); it show me an error Cannot convert string 'Rose'to int
ketan borsdiyaPosted Jul 8, 2016, 6:36 AM
Satya Prakash MishraPosted Jul 8, 2016, 6:21 AM