Yosra Chourou

Yosra Chourou

  • NA
  • 8
  • 699

Insert records by user who's connected Winforms

Jul 8 2016 5:51 AM
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)

Now , I want to insert a Client
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: 
  1. private void button1_Click(object sender, EventArgs e)  
  2.       {  
  3.           if (  comboBox2.SelectedValue != null && textBox1.Text != string.Empty && textBox2.Text != string.Empty && textBox4.Text != string.Empty)  
  4.           {  
  5.               string cmdStr = "Insert into Client  (idUser,name,address,function,telephone,commentaire)values (@idUser,@name,@,address,@function,@telephone,@commentaire)";  
  6.               SqlConnection con = new SqlConnection("Data Source=User-PC\\SQLEXPRESS;Initial Catalog=timar;Integrated Security=True");  
  7.               SqlCommand cmd = new SqlCommand(cmdStr, con);  
  8.               con.Open();  
  9.               
  10.              //The problem in this code how Can I get the id of username,Error cannot convert string Rose to int.  
  11.               cmd.Parameters.AddWithValue("@idUser",label.Text);  
  12.               cmd.Parameters.AddWithValue("@name", (comboBox2.SelectedValue));  
  13.               cmd.Parameters.AddWithValue("@,address", textBox1.Text);  
  14.               cmd.Parameters.AddWithValue("@function", textBox2.Text);  
  15.               cmd.Parameters.AddWithValue("@telephone", textBox4.Text);  
  16.               cmd.Parameters.AddWithValue("@commentaire",txtArchive.Text);  
  17.                 
  18.   
  19.               int LA = cmd.ExecuteNonQuery();  
  20.               con.Close();  
  21.               MessageBox.Show("Le Client a été ajouter avec succés !","Saisie Rendez-vous", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  22.               DisplayData();  
  23.               ClearData();  
  24.           }  
  25.           else  
  26.           {  
  27.               MessageBox.Show("Vérifier que tous les champs sont remplis !","Erreur",MessageBoxButtons.OK,MessageBoxIcon.Information);  
  28.           }  
  29.       }  
 
I am not able to figure out how to do this, i am very new to c# and trying to learn.
Thanks in Advance .

Answers (6)