NAJEEB
NAJEEB

Reputation: 251

Display data from database to textbox

I trying to show the data from database to textbox , that what I write in textbox. this is my last code I got. here the data showing in to datagrid. instead of datagrid how to get the data to textboxes.

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

I have no idea how to do that. please help me

Upvotes: 0

Views: 238

Answers (2)

Fred
Fred

Reputation: 5808

If you want to populate textboxes try something like...

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

Upvotes: 1

ɐsɹǝʌ ǝɔıʌ
ɐsɹǝʌ ǝɔıʌ

Reputation: 4512

The below code should return the ItenName, Qty and Price into text boxes named TxtName, txtQty and txtPrice. You don't need a DataSet at all if you want to retrieve a single value.

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

Upvotes: 0

Related Questions