adding a row to datagrid

son

Regular
Joined
Feb 21, 2004
Messages
67
hi all hope all is well .. i am using asp.net and vb.net

i have a webform which has a datagrid and textboxes on my form . i have added a command button above the datagrid which is called add... i have managed to make the add work , meaning that it allows the user to add a record, and that the datagrid is hidden when the user clicks add what i am finding difficulty in is how to add the values the user has just added in the textboxes to show in my datagrid and hide the textboxes .. my code for the add command button is below.



Visual Basic:
    Private Sub btnadd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnadd.Click
        Dim conn As SqlConnection
        conn = New SqlConnection
        conn.ConnectionString = myconfigurationsettings
        conn.Open()


        Dim SqlSPParam1 As String = "@customerid"
        Dim SqlSPParam2 As String = "@companyname"
        Dim SqlSPParam3 As String = "@contactname"
        Dim SqlSPParam4 As String = "@contacttitle"
        Dim SqlSPParam5 As String = "@address"
        Dim SqlSPParam6 As String = "@city"
        Dim SqlSPParam7 As String = "@region"
        Dim SqlSPParam8 As String = "@postalcode"
        Dim SqlSPParam9 As String = "@country"
        Dim SqlSPParam10 As String = "@phone"
        Dim SqlSPParam11 As String = "@fax"

        Dim objSQLCmd As SqlCommand
        'Create the command object
        objSQLCmd = New SqlCommand

        With objSQLCmd
            .Connection = conn  ' the SQL connection
            .CommandType = CommandType.StoredProcedure
            .CommandText = "sp_customers" 'Stored Procedure to use

            'The name parameter passed into the stored procedure
            .Parameters.Add(SqlSPParam1, txtId.Text)
            .Parameters.Add(SqlSPParam2, txtCompanyName.Text)

            .Parameters.Add(SqlSPParam3, txtContactName.Text)
            .Parameters.Add(SqlSPParam4, txtContactTitle.Text)
            .Parameters.Add(SqlSPParam5, txtAddress.Text)
            .Parameters.Add(SqlSPParam6, txtCity.Text)
            .Parameters.Add(SqlSPParam7, txtRegion.Text)
            .Parameters.Add(SqlSPParam8, txtPostalCode.Text)
            .Parameters.Add(SqlSPParam9, txtCountry.Text)
            .Parameters.Add(SqlSPParam10, txtPhone.Text)
            .Parameters.Add(SqlSPParam11, txtFax.Text)
        End With
        objSQLCmd.ExecuteNonQuery()
        objSQLCmd.Parameters.Clear()
        objSQLCmd.Connection.Close()


        DataGrid1.Visible = True
        Panel2.Visible = False    ' is the textboxes
end sub
 
Sounds like the datagrid is not being refreshed after you click add. If you are using an ASP.NET script on only 1 page to accomplish all of your tasks you may need rebind you datagrid. Another approach would be to use different pages based on function. The first page would provide a view of the current data. An AddItem button could then launch a seperate page which sole function is to add a record. After clicked Submit or Add your first page would be presented again. When the first page is loaded again it will automatically grab a refreshed version of the data.
 
Back
Top