Insert New Record

Discussion in 'ASP .Net' started by RN1, Aug 4, 2008.

  1. RN1

    RN1 Guest

    This is how I am trying to insert a new record in a database table &
    then display all the records (including the newly inserted record) in
    a DataGrid:

    <script runat="server">
    Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
    Dim dSet As DataSet
    Dim dTable As DataTable
    Dim sqlCmd As SqlCommand
    Dim sqlConn As SqlConnection
    Dim sqlDapter As SqlDataAdapter

    sqlConn = New SqlConnection(".....")
    sqlDapter = New SqlDataAdapter
    sqlCmd = New SqlCommand("SELECT * FROM TestMarks", sqlConn)
    sqlDapter.SelectCommand = sqlCmd

    dSet = New DataSet
    sqlDapter.Fill(dSet, "Marks")

    dTable = New DataTable
    dTable = dSet.Tables("Marks")

    sqlCmd = New SqlCommand("INSERT INTO TestMarks (UserID,
    Subject, Marks) VALUES (@UserID, @Subject, @Marks)", sqlConn)
    With sqlCmd
    .Parameters.Add("@UserID", SqlDbType.VarChar, 50).Value =
    "bobby"
    .Parameters.Add("@Subject", SqlDbType.VarChar, 50).Value =
    "Physics"
    .Parameters.Add("@Marks", SqlDbType.Int).Value = 85
    End With

    sqlDapter.InsertCommand = sqlCmd
    sqlDapter.Update(dSet.Tables("Marks"))

    dgMarks.DataSource = dSet.Tables("Marks").DefaultView
    dgMarks.DataBind()
    End Sub
    </script>

    <form runat="server">
    <asp:DataGrid ID="dgMarks" runat="server"/>
    </form>

    But the above code doesn't insert the new record in the DB table.
    What's wrong with the above code? Why isn't the new record getting
    inserted in the DB table?

    Please note that I am aware that there are alternate ways to insert
    records; so please do not suggest alternate ways. All I did like to
    know is what's wrong with the above code.

    Thanks,

    Ron
    RN1, Aug 4, 2008
    #1
    1. Advertising

  2. RN1

    Munna Guest

    Munna, Aug 5, 2008
    #2
    1. Advertising

  3. RN1

    RN1 Guest

    On Aug 5, 11:37 am, Munna <> wrote:
    > Hi
    >
    > before update the datatable just a execure non query
    >
    > sqlDapter.InsertCommand.ExecuteNonQuery();
    >
    > try it...
    >
    > Best of luck
    >
    > -------
    > Munna
    >
    > www.munna.shatkotha.com/blogwww.munna.shatkotha.comwww.shatkotha.com


    Thanks Munna for the solution. You are right......ExecuteNonQuery did
    the trick but the DataGrid doesn't display the newly inserted record.
    Why so?

    Ron
    RN1, Aug 5, 2008
    #3
  4. RN1

    Munna Guest

    Munna, Aug 5, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. loreille
    Replies:
    1
    Views:
    4,466
    Ben Strackany
    Oct 28, 2004
  2. loreille
    Replies:
    0
    Views:
    451
    loreille
    Oct 28, 2004
  3. Frits van Soldt

    How to insert a new record in detailsview when there are no record

    Frits van Soldt, Nov 16, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    733
    Frits van Soldt
    Nov 17, 2005
  4. Guest
    Replies:
    1
    Views:
    253
    Alex G
    Jan 9, 2004
  5. Maximus
    Replies:
    2
    Views:
    192
    Bob Barrows [MVP]
    Apr 12, 2007
Loading...

Share This Page