我试图将数据从数据库显示到文本框,这是我在文本框中写的内容。 这是我的最后一个代码。这里的数据显示在datagrid中。而不是datagrid如何将数据获取到文本框。
Public Sub SelectItem(ByVal ItemCode As String)
Try
sql.OpenDbConnection()
Dim strSQL As String = "SELECT ItemCode 'Item Code',ItemName 'Item Name' " & _
" FROM tblItemMaster where ItemCode= @ItemCode"
Dim cmd As New SqlCommand(strSQL, sql.SqlConn)
Dim ds As New DataSet
cmd.Parameters.AddWithValue("ItemCode", ItemCode)
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds, "tblItemMaster")
dgvPurchaseOrder.DataSource = ds.Tables("tblItemMaster")
sql.SqlConn.Close()
Catch ex As SqlException
MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
End Try
End Sub
我不知道该怎么做。请帮帮我
答案 0 :(得分:1)
如果你想填充文本框,请尝试类似......
Public Sub SelectItem(ByVal ItemCode As String)
Try
sql.OpenDbConnection()
Dim strSQL As String = "SELECT ItemCode [Item Code],ItemName [Item Name] FROM tblItemMaster where ItemCode= @ItemCode"
Dim cmd As New SqlCommand(strSQL, sql.SqlConn)
cmd.Parameters.AddWithValue("ItemCode", ItemCode)
Dim myReader As sqlDataReader
myReader = cmd.ExecuteReader()
If myReader.HasRows Then
myReader.Read()
txtItemCode.Text = myReader.GetValue(0).ToString()
txtItemName.Text = myReader.GetValue(1).ToString()
Else
MessageBox.Show("No data found", "No Data")
End If
myReader.Close()
sql.SqlConn.Close()
Catch ex As SqlException
MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
End Try
End Sub
答案 1 :(得分:0)
以下代码应将ItenName
,Qty
和Price
返回到名为TxtName
,txtQty
和txtPrice
的文本框中。如果您想要检索单个值,则根本不需要DataSet
。
Public Sub SelectItem(ByVal ItemCode As String)
Try
sql.OpenDbConnection()
Dim strSQL As String = "SELECT ItemName, Qty, Price " & _
"FROM tblItemMaster WHERE ItemCode = @ItemCode"
Dim cmd As New SqlCommand(strSQL, sql.SqlConn)
cmd.Parameters.AddWithValue("@ItemCode", ItemCode)
Dim reader As SqlDataReader
reader = cmd.ExecuteReader()
If reader.HasRows Then
reader.Read()
txtItemName.Text = reader.GetValue(0)
txtQty.Text = reader.GetValue(1).ToString()
txtPrice.Text = reader.GetValue(2).ToString()
reader.Close()
End If
Catch ex As SqlException
MsgBox(ex.Message)
End Try
End Sub