将网格数据保存到数据库表中. [英] Saving Grid data into database table.

查看:98
本文介绍了将网格数据保存到数据库表中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网格中的某些列当前正在从数据库表中填充.
它还包含一些文本框和DDL,供用户输入值.
现在,当用户单击保存"按钮时,我要保存网格中的所有数据,该数据已预先填充,并由用户输入到单独的数据库表中.
我正在考虑使用DataTable,但不知道该怎么做.
我的代码在VB.NET中.

非常感谢您的帮助.

[从评论中添加]

这是我的代码,在插入"命令中,我还需要传递我的pk值,该值将来自页面上的标签控件.

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 'extract the TextBox values 

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) 'get the values from the TextBoxes 'then add it to the collections with a comma "," as the delimited values 

sc.Add(box1.Text + "," + box2.Text & "," + box3.Text & "," + box4.Text & "," + box5.Text & "," + box6.Text & "," + box7.Text & "," + ddl.SelectedValue & "," + box8.Text) 
rowIndex += 1 
Next 'Call the method for executing inserts InsertRecords(sc) 
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() 'Display a popup which indicates that the record was successfully inserted 
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

推荐答案

这真的是一个常见的问题.您也可以在Google上搜索CP.

将动态文本框值从GridView保存到数据库 [
It''s really a frequent question here. You can search CP also google.

Save Dynamic TextBox Values from GridView to Database[^]

OP写道:

但是我有一个问题.在我的插入查询中,我想插入一个与我的网格列无关的附加字段(PKey值).因为当我运行insert命令时会显示错误,所以主键不能为空.在同一个Web窗体上,我有一个具有该主键值的标签,现在如何使用上面的代码插入该值.

But i have one issue. In my insert query, i want to insert an additional field(PKey Value) which is not related to my grid columns. Since when i run insert command it shows me error, that primary key can''t be null. on same webform i have one label which have that primary key value, now how can i insert this value using code above.



在保存数据之前,您需要检查PrimaryKey字段值是否为null.因此,在调试时请检查主键列值的值.

让我们知道

顺便说一句,然后在您的问题中添加您的代码以快速响应.



You need to check if the PrimaryKey field value is null before saving the data. So check the value of the primary key column value while debugging.

Let us know

BTW Here after include your code in your question for quick response.


'this code will loop to get the value then it sends to 2 procedure to insert into the database
Private Sub save__details()
        Dim S As Integer
        For S = 0 To datagridgridview1.RowCount - 1
            If CStr(datagridgridview1.Item(0, S).Value()) <> ""  Then
                'payment details
                updateRecord("insert into table1 (code)" & _
                  " values('" & CStr(datagridgridview1.Item(0, S).Value()) & "' )")
            End If
        Next
'loop all the record until end using next.
    End Sub

'this procedure will be used to insert to the databases as u loop
 Function updateRecord(ByVal query As String) As Integer
        Try
            Dim rowsEffected As Integer = 0
            Dim connection As New SqlConnection("YOUR CONNECTION STRING  ")
            Dim cmd As New sqlcommand(query, connection)
            ''InputBox("", query, query)
            connection.Open()
            rowsEffected = cmd.ExecuteNonQuery()
            connection.Close()
            Return rowsEffected
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Function


这篇关于将网格数据保存到数据库表中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆