none
how can i add a new row to db by textbox RRS feed

  • Question

  •  

    I creat two textboxes  and a button on my page. I want to add rows to my table  "new" in the database by textbox and the button. Following are some codes:

    'SqlConnection1, SqlDataAdapter1 and DataSet are set automatically
      SqlDataAdapter1.SelectCommand = New SqlClient.SqlCommand("Select * from news", SqlConnection1)
      Dim cb As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(SqlDataAdapter1)
      SqlConnection1.Open()
      SqlDataAdapter1.Fill(DataSet11, "news")

      Dim anyRow As DataRow = DataSet11.Tables("news").NewRow
      anyRow("content") = TextBox1.Text
      anyRow("writingdate") ="2008-7-19" 
      DataSet11.Tables("news").Rows.Add(anyRow)

      SqlDataAdapter1.Update(DataSet11, "news")
      SqlDataAdapter1.Fill(DataSet11, "news")
      SqlConnection1.Close()
      End Sub

    Result: the line " anyRow("content")= TextBox1.Text " does not work.

               the line " anyRow("writingdate") = "2008-7-19" runs well and data adds to the db successfully

    I need help!

    Thanks in advance!

    Saturday, July 19, 2008 8:26 AM

All replies

  •  

    oh,the question soluted, I forgot to add IsPostBack to the Page-Load event.
    Saturday, July 19, 2008 10:22 AM
  • Your code could be much simpler and more efficient, since you do not need to use SqlDataAdapter to add new row. You could use SqlCommand and execute INSERT SQL statement directly. It would be less code and will work much faster, especially taking into account that your current code select all the rows from the database before inserting new row.

     

    Saturday, July 19, 2008 11:05 AM
    Moderator