c# How to insert textbox value and save it to sql database?

37,464

I have solved this question by connecting properly to the Workers database. YeaY!!

Here's the right code for this question:

private void btnSave_Click(object sender, EventArgs e)
{
    #region SaveButton
    System.Data.SqlClient.SqlDataAdapter da;
    string sql = "SELECT * From tblWorkers";
    da = new System.Data.SqlClient.SqlDataAdapter(sql, con);

    System.Data.SqlClient.SqlCommandBuilder cb;
    cb = new System.Data.SqlClient.SqlCommandBuilder (da);

    //add to Dataset a new row
    DataRow dRow = ds1.Tables["Workers"].NewRow();

    //add data to the new row that has just been created
    //refer to first_Name
    dRow[1] = textBox1.Text;
    dRow[2] = textBox2.Text;
    dRow[3] = textBox3.Text;

    //add command
    //add to table worker a new row that declared by row variable name dRow
    ds1.Tables["Workers"].Rows.Add(dRow);

    MaxRows = MaxRows + 1; //to enable last row is still last row
    inc = MaxRows - 1;

    //call data adapter da to update and save data into database sql server
    da.Update(ds1, "Workers");              

    MessageBox.Show("Entry Added!");
    con.Close();
    #endregion 
Share:
37,464
Com.Man.Do.Girl
Author by

Com.Man.Do.Girl

Noobs

Updated on July 17, 2022

Comments

  • Com.Man.Do.Girl
    Com.Man.Do.Girl almost 2 years


    How to insert textbox value and save it to sql database? I need some help here regarding to the question above. When I clicked button save, it should update the input textbox to the sql database Workers. Could you guys make some coding sample to achieve this? Because what I do is not working at all. This is the coding :

    private void btnSave_Click(object sender, EventArgs e) {
    #region SaveButton
                // System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter ();
    
                //System.Data.SqlClient.SqlCommandBuilder cb;
                //cb = new System.Data.SqlClient.SqlCommandBuilder (da);
    
                //add to Dataset a new row
                DataRow dRow = ds1.Tables["Workers"].NewRow();
    
                //add data to the new row just have been created
                //refer to first_Name
                dRow[1] = textBox1.Text;
                dRow[2] = textBox2.Text;
                dRow[3] = textBox3.Text;
    
                //add command
                //add to table worker a new row that declared by row variable name dRow
                ds1.Tables["Workers"].Rows.Add(dRow);
    
                MaxRows = MaxRows + 1; //to enable last row is still last row
                inc = MaxRows - 1;
    
                //call data adapter da to update and save data into database sql server
                //da.Update(ds1, "Workers");
    
                MessageBox.Show("Entry Added!");
    #endregion
                con.ConnectionString = "Data Source=.\\SQLEXPRESS; AttachDbFilename =D:\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
    
    
                string strSQL = "INSERT INTO Workers (first_Name, last_Name, job_Title )" + " VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "', " + " '" + textBox3.Text + "') ";
    
    
    
    
    
                con.Close();  
            }
    
    • Daniel Hilgarth
      Daniel Hilgarth about 13 years
      You are doing nothing. No wonder, it isn't working ;-)
    • kͩeͣmͮpͥ ͩ
      kͩeͣmͮpͥ ͩ about 13 years
      ...and this is how sql-injection attacks start.
  • Com.Man.Do.Girl
    Com.Man.Do.Girl about 13 years
    Button save should have to save it to database, update it permanently. Any suggestion on how to do this since using insert is not working?
  • Com.Man.Do.Girl
    Com.Man.Do.Girl about 13 years
    da.Update(ds1, "Workers");<<---Object reference not set to an instance of an object.
  • Com.Man.Do.Girl
    Com.Man.Do.Girl about 13 years
    @justinlabenne Any suggestion on how to use sql command for this button?
  • Kenneth Jakobsen
    Kenneth Jakobsen about 13 years
    Well for starters, you'll want to decide whether you want to use the dataadapter or just write your sql yourself.