[英]Display data from database to textbox
我试图显示从数据库到文本框的数据,即我在文本框中写的内容。 这是我得到的最后一个代码。 这里的数据显示在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
我不知道该怎么做。 请帮我
如果要填充文本框,请尝试类似...
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
下面的代码应将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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.