没有给定一个或多个参数的值

时间:2017-10-07 07:06:16

标签: sql vb.net

Public Sub fillproductinfo(ByVal Productid As String)
    ProductsTA.FillByProductID(ProductDataset.Products, Productid)
End Sub

If DataGridView1.SelectedRows.Count = 0 Then
    Exit Sub
End If


Dim productid = DataGridView1.SelectedRows(0).Cells(0).Value
EditProduct.fillproductinfo(productid)


Dim EditProductwindow As New EditProduct
If EditProduct.ShowDialog = Windows.Forms.DialogResult.OK Then
    ProductsTableAdapter1.Fill(MyDataset.Products)
End If

在我的数据库中实际的列名是Product ID,当我使用SQL语句进行查询时,我将此条件设置为" where ProductID = ?"

1 个答案:

答案 0 :(得分:0)

当提出类似这样的问题时,最好显示您的SQL语句以及显示流程的代码,例如在你的例子中,你有程序/功能/事件之外的部分代码,而你应该在他们的容器中显示所述代码,例如步骤/功能/事件。

下面我正在使用Microsoft NorthWind数据库中的Product表。一个查询用于获取所有数据,另一个用于获取主键的数据(一个记录),最后一个用于获取您似乎不需要的产品列表,但此处是完整的,因为我不希望用户输入无效的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而不是数据源获取键值)但错误消息确实告诉你主键是在where条件中设置的,所以为什么不发布你的查询或者只是查看我发布的内容,并在您的问题的编辑版本中模仿它。

最重要的是,在这里提问时要具体而不是缺乏信心,这表明你并不认真学习如何纠正手头的问题。

这里的人都非常精通这些问题,而我的猜测也不会回应你如何表达你的问题。所以花时间编辑你的帖子。