Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi evryone..
i want to get the text box value to list box .
And also i want this textbox value enter into the dabasex table called Area and @ th esame time insert this value to List box. here is the code wich i have use.



SqlConnection conn = new SqlConnection(ConnectionString);
                String S = "Insert into Area values ('" + textBox1.Text.ToString().Trim() + "')";
                SqlCommand command = new SqlCommand(S);
                SqlDataAdapter da = new SqlDataAdapter(S,ConnectionString);
              // DataSet dt = new DataSet();
                DataTable dt = new DataTable();
                da.Fill(dt);
                  
               textBox1.Clear();
                listBox1.Items.Add(dt.Rows[0].ItemArray[0].ToString());


but here DataTable is not filling. Can any1 tell me what is the problem here.
thank you everyone
Posted

1 solution

The problems here are;

  1. INSERT statements do not return data
  2. Taking a value direct from a text box opens your code to injection attack, learn about parameters and how to use them.
  3. You don't need a SqlDataAdapter or DataTable for this
    SqlCommand command = new SqlCommand(sqlQuery, conn);
    conn.Open();
    command.ExecuteNonQuery();
    conn.Close();
    
    //After Inserting value into database add the value to the list box, THEN clear the text box
    listBox1.Items.Add(textBox1.Text.Trim());
    textBox1.Clear();
    
 
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