PDA

View Full Version : insert row in database by datagrid??



vickyfastian
18 May 2006, 12:28 AM
Any one tells me how i insert multiple rows showing on datagrid in ASP WebPage into database table....?

thanks in advance.

DanInManchester
23 May 2006, 12:52 PM
There are numerous ways but assuming you are using the standard edit command button this is some code I've used....


Private Sub dgFiles_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgFiles.UpdateCommand

' unset the editing row as we are done editing.
dgFiles.EditItemIndex = -1

Dim txtBox As TextBox
' get the primary key
Dim intFileID As Long = Convert.ToInt32(dgFiles.DataKeys(e.Item.ItemIndex))

' get the textbox field values
Dim strDisplayText As String = CType(e.Item.FindControl("txtFiletext2"), TextBox).Text
Dim strFilename As String = CType(e.Item.FindControl("txtFilename2"), TextBox).Text

' load the product
Dim productFile As ProductFile = ProductController.LoadProductFile(intFileID)
' set the new values
productFile.Name = strDisplayText
productFile.Filename = strFilename
' save the product
ProductController.SaveProductFile(productFile)
' rebind the grid.
BindFiles()
End Sub

The exanmple uses an edit template with two text boxes.

by this stage you have already set the row to edit and the user has edited the values. The code above is what happens when the user hits save/update.

You'll notice there is no 'database stuff' in there but thats simply because it's a tiered design. you can just as easily put the database code there instead of the business controller.