Some columns on my grid is currently populating from a database table.
It also contains some textbox and DDL for user to input values.
Now, when user click on save button, i want to save all data from grid, which is prepopluating and which is entered by user into a separate database table.
I am thinking of using DataTable, but don't know how to do this.
My code is in VB.NET.
Any help is really appreciated.
[Added from Comment]
Here's my code In Insert command, i need to pass my pk value also, which will come from a label control on page.
Public Sub btnsave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsave.Click If True Then Dim rowIndex As Integer = 0 Dim sc As New StringCollection() If ViewState("CurrentTable") IsNot Nothing Then Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable) If dtCurrentTable.Rows.Count > 0 Then For i As Integer = 1 To dtCurrentTable.Rows.Count
Dim box1 As TextBox = DirectCast(GD_Prod.Items(rowIndex).Cells(4).FindControl("TxtSTUPort"), TextBox)
Dim box2 As TextBox = DirectCast(GD_Prod.Items(rowIndex).Cells(5).FindControl("TxtAAPort"), TextBox)
Dim box3 As TextBox = DirectCast(GD_Prod.Items(rowIndex).Cells(7).FindControl("TxtFPort"), TextBox)
Dim box4 As TextBox = DirectCast(GD_Prod.Items(rowIndex).Cells(8).FindControl("TxtQoFUsed"), TextBox)
Dim box5 As TextBox = DirectCast(GD_Prod.Items(rowIndex).Cells(9).FindControl("TxtPPort"), TextBox)
Dim box6 As TextBox = DirectCast(GD_Prod.Items(rowIndex).Cells(10).FindControl("TxtBFPort"), TextBox)
Dim box7 As TextBox = DirectCast(GD_Prod.Items(rowIndex).Cells(11).FindControl("TxtLOPort"), TextBox)
Dim ddl As DropDownList = DirectCast(GD_Prod.Items(rowIndex).Cells(12).FindControl("ddlLOCode"), DropDownList)
Dim box8 As TextBox = DirectCast(GD_Prod.Items(rowIndex).Cells(13).FindControl("TxtTSPort"), TextBox)
sc.Add(box1.Text + "," + box2.Text & "," + box3.Text & "," + box4.Text & "," + box5.Text & "," + box6.Text & "," + box7.Text & "," + ddl.SelectedValue & "," + box8.Text)
rowIndex += 1
Next
End If
End If
End If
End Sub
Private Sub InsertRecords(ByVal sc As StringCollection)
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("FOOD_AND_NUTRITIONConnectionString").ConnectionString)
Dim sb As New StringBuilder(String.Empty)
Dim splitItems As String() = Nothing
For Each item As String In sc Const sqlStatement As String = "INSERT INTO FNProdRecDetails (PL_Student_Portions_Planned, PL_Adults_ALC__Portions_Planned, PL_Factor_Portions, DOC_Quantity_Of_Food_Used, DOC_Portions_Prepared, DOC_Portions_Brought_Forward, DOC_Portions_Leftover,DOC_Leftover_Code, DOC_Total_Portions_Served) VALUES"
If item.Contains(",") Then
splitItems = item.Split(",".ToCharArray())
sb.AppendFormat("{0}('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}'); ", sqlStatement, splitItems(0), splitItems(1), splitItems(2), splitItems(3), splitItems(4), splitItems(5), splitItems(6), splitItems(7), splitItems(8))
End If
Next
Try
conn.Open()
Dim cmd As New SqlCommand(sb.ToString(), conn)
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Script", "alert('Records Successfuly Saved!');", True)
Catch ex As System.Data.SqlClient.SqlException
Dim msg As String = "Insert Error:"
msg += ex.Message
Throw New Exception(msg)
Finally
conn.Close()
End Try
End Sub