当问这样一个问题做了一个查询No值你会更好地显示你的SQL语句以及显示流程的代码,例如在你的例子中,你在程序/函数/事件之外有部分代码,而你应该在它们的容器中显示所述代码,例如过程/函数/事件。
下面我使用Microsoft NorthWind数据库的产品表。一个查询用于获取所有数据,另一个查询通过主键获取数据(一条记录),最后一个查询获得您似乎不需要的产品列表,但这里是完整的,因为我不希望用户输入一个无效的ID或名称,这可以称赞自动完成组合框。我已经评论了代码的每个部分,以便您可以跟随。
图片 数据集(的.xsd) https://1drv.ms/i/s!AtGAgKKpqdWjiGseXZ9FLOr1mMvZ 形式 https://1drv.ms/i/s!AtGAgKKpqdWjiGzfO22-jdRb-eAs
表单代码
Public Class Form1
Private Sub ProductsBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
Handles ProductsBindingNavigatorSaveItem.Click
Me.Validate()
Me.ProductsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DemoDataSet)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' disable constraints as the query to get product name and id violate relational
' rules in the database setup in Microsoft NorthWind database
Me.DemoDataSet.EnforceConstraints = False
' FillByProductList is a subset of data e.g. id and name of product
Me.ProductsTableAdapter.FillByProductList(Me.DemoDataSet.Products)
' create a list containing id and name of products for combo box
Dim ProdList As List(Of Product) =
Me.DemoDataSet.Products _
.AsEnumerable _
.Select(Function(row) New Product With
{
.Id = row.Field(Of Integer)("ProductId"),
.Name = row.Field(Of String)("ProductName")}) _
.ToList
' insert a select all
ProdList.Insert(0, New Product With {.Id = 0, .Name = "All"})
cboProducts.DataSource = ProdList
cboProducts.DisplayMember = "Name"
cboProducts.ValueMember = "id"
' clear so that we can set Constraints back on
Me.DemoDataSet.Products.Clear()
Me.DemoDataSet.EnforceConstraints = True
End Sub
Private Sub cmdGet_Click(sender As Object, e As EventArgs) Handles cmdGet.Click
Dim primaryKey As Integer = CInt(cboProducts.SelectedValue)
If primaryKey = 0 Then
Me.ProductsTableAdapter.Fill(Me.DemoDataSet.Products)
Else
Me.ProductsTableAdapter.FillByPrimaryKey(Me.DemoDataSet.Products, primaryKey)
End If
End Sub
''' <summary>
''' Demo to get current DataGridView primary key for use in a data operation
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub cmdCurrentRowKey_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ProductsBindingSource.Current IsNot Nothing Then
Dim primaryKey As Integer = CInt(CType(ProductsBindingSource.Current, DataRowView).Item("ProductId"))
MessageBox.Show($"Current row primary key is {primaryKey}")
End If
End Sub
End Class
''' <summary>
''' Class for ComboBox.
''' Recommend this class be in it's own physical
''' file, placed here for demo purposes only.
''' </summary>
Public Class Product
Public Property Id As Integer
Public Property Name As String
End Class
所以,你的代码可能看起来正确的(除了获得来自DataGridView的键值,而不是其他的数据源),但错误消息确实告诉你,主要关键是在网络条件中设置,所以为什么不发布您的查询或简单地回顾我发布和模仿在您的编辑版本q题目了。
当问这里的问题具体而不是缺乏含义,这表明你不认真学习如何纠正手头的问题。
这里的人都非常熟悉这些问题,我的猜测不会回答你如何解释你的问题。所以花时间编辑您的帖子。
首先,不要发布你的代码的图像。将它作为文本复制并粘贴到您的问题中。第二。发布与您的问题相关的代码,失败的调用不会告诉我们任何内容,您应该发布被调用的代码并可能描述数据库表的结构 – Steve