插入后如何刷新数据网格 [英] How to refresh data grid after insert

查看:129
本文介绍了插入后如何刷新数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专业人员,

我正在尝试在同一页面上显示特定表的内容.所以,这是我的问题,

我有几个文本框和组合框,我在其中填充了一些数据,然后单击保存".

我可以将数据插入到表中(在数据库中),但不能动态刷新数据.

每当发生任何插入时,都需要更新同一页面的数据网格.

我的代码正确插入了第一个条目,但是当我寻找下一个条目时,它是从表中复制以前的记录,然后再次将其填充到数据集中.因此,我得到重复的数据.如何更正此代码?我对绑定没有信心.

我尝试过的事情:

私有Sub btnSave_Click(发送者为对象,e作为EventArgs)处理btnSave.Click
如果MsgBox(发送的所有输入的详细信息都是正确的.",vbYesNo,保存交易")= vbYes然后
试试
con.Open()
''Dim str,标记为String
''str = CreateTransID()

使用cmd作为新的SqlClient.SqlCommand("INSERT INTO Sender_Details(Sender_ID,Sender_Address,RegionID,Trans_ID,Rec_Token_No,Quantity)VALUES(@ d1,@ d2,@ d3,@ d4,@ d5,@ d6)",con)
''& txtSenderID.Text&'',``"&txtAddr.Text.Trim&'',``& cbRegID.Text&"'',''& NewTransactionID .Text&'',``"& txtSChallanNo.Text.Trim&","& txtSQuantity.Text.Trim&"),con)
cmd.Parameters.Add(新SqlClient.SqlParameter("@ d1",SqlDbType.NVarChar)).Value = txtSenderID.Text
cmd.Parameters.Add(新SqlClient.SqlParameter("@ d2",SqlDbType.NVarChar)).Value = txtAddr.Text.Trim
cmd.Parameters.Add(新SqlClient.SqlParameter("@ d3",SqlDbType.NVarChar)).Value = cbRegID.Text
cmd.Parameters.Add(新SqlClient.SqlParameter("@ d4",SqlDbType.NVarChar)).Value = NewTransactionID.Text
cmd.Parameters.Add(新SqlClient.SqlParameter("@ d5",SqlDbType.NVarChar)).Value = txtSChallanNo.Text.Trim
cmd.Parameters.Add(新SqlClient.SqlParameter("@ d6",SqlDbType.BigInt)).Value = txtSQuantity.Text.Trim
''cmd.Parameters.Add("@ d7",SqlDbType.NVarChar).Value =备注
''cmd.Parameters.Add("@ d8",SqlDbType.NVarChar).Value = txtSenderID.Text

Dim i As Integer

我= cmd.ExecuteNonQuery

如果我> 0然后
致电btnClear_Click(sender,e)
NewTransactionID.Text = CreateTransID()
txtSenderID.Text = RandomString()

Me.DG1.DataSource =没什么
使用cmd1作为新的SqlClient.SqlCommand("SELECT * FROM Sender_Details",con)
sda =新的SqlDataAdapter(cmd1)

sda.Fill(dt)
Me.DG1.DataSource = dt
最终使用
txtAddr.Focus()
如果结束
最终使用
con.Close()
异常捕获
MsgBox(错误")
结束尝试
如果结束
结束Sub

Hello professionals,

I am trying to display the content of a particular table on the same page. So, here is my issue,

I have few text boxes and combo boxes, I feed some data in it and then click on save.

I am able to insert the data in to the table (in database) But I am not able to refresh the data dynamically.

The datagrid of same page needs to be updated whenever any insert occurs.

My code is inserting first entry correctly however when I go for the next entry, it is copying previous records from the table and filling it again in the dataset. Thus I am getting duplicated data. How to correct this code? I am not confident with binding.

What I have tried:

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
If MsgBox("All entered details of send is correct.", vbYesNo, "Save Transaction") = vbYes Then
Try
con.Open()
''Dim str, remarks As String
''str = CreateTransID()

Using cmd As New SqlClient.SqlCommand("INSERT INTO Sender_Details(Sender_ID, Sender_Address, RegionID, Trans_ID, Rec_Token_No, Quantity) VALUES(@d1, @d2, @d3, @d4, @d5, @d6)", con)
''" & txtSenderID.Text & "'', ''" & txtAddr.Text.Trim & "'', ''" & cbRegID.Text & "'', ''" & NewTransactionID.Text & "'', ''" & txtSChallanNo.Text.Trim & "'', ''" & txtSQuantity.Text.Trim & "'')", con)
cmd.Parameters.Add(New SqlClient.SqlParameter("@d1", SqlDbType.NVarChar)).Value = txtSenderID.Text
cmd.Parameters.Add(New SqlClient.SqlParameter("@d2", SqlDbType.NVarChar)).Value = txtAddr.Text.Trim
cmd.Parameters.Add(New SqlClient.SqlParameter("@d3", SqlDbType.NVarChar)).Value = cbRegID.Text
cmd.Parameters.Add(New SqlClient.SqlParameter("@d4", SqlDbType.NVarChar)).Value = NewTransactionID.Text
cmd.Parameters.Add(New SqlClient.SqlParameter("@d5", SqlDbType.NVarChar)).Value = txtSChallanNo.Text.Trim
cmd.Parameters.Add(New SqlClient.SqlParameter("@d6", SqlDbType.BigInt)).Value = txtSQuantity.Text.Trim
''cmd.Parameters.Add("@d7", SqlDbType.NVarChar).Value = remarks
''cmd.Parameters.Add("@d8", SqlDbType.NVarChar).Value = txtSenderID.Text

Dim i As Integer

i = cmd.ExecuteNonQuery

If i > 0 Then
Call btnClear_Click(sender, e)
NewTransactionID.Text = CreateTransID()
txtSenderID.Text = RandomString()

Me.DG1.DataSource = Nothing
Using cmd1 As New SqlClient.SqlCommand("SELECT * FROM Sender_Details", con)
sda = New SqlDataAdapter(cmd1)

sda.Fill(dt)
Me.DG1.DataSource = dt
End Using
txtAddr.Focus()
End If
End Using
con.Close()
Catch ex As Exception
MsgBox("error")
End Try
End If
End Sub

推荐答案

如注释中所述,问题是在调用之间重用了单个全局DataTable来绑定网格. Fill方法从SQL查询加载所有记录,并将它们附加到DataTable.大多数记录已被加载到DataTable中,因此最终会出现重复记录.

通过在每次绑定网格时使用新的DataTable,可以解决此问题:
As discussed in the comments, the problem is that a single global DataTable was reused between calls to bind the grid. The Fill method loads all of the records from the SQL query, and appends them to the DataTable. Most of the records have already been loaded into the DataTable, so you end up with duplicates.

By using a new DataTable each time the grid is bound, the problem is resolved:
Dim sda As New SqlDataAdapter(cmd1)
Dim dt As New DataTable()
sda.Fill(dt)
Me.DG1.DataSource = dt


这篇关于插入后如何刷新数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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