locked
vb.net select information and bind to text boxes RRS feed

  • Question

  • User1997074450 posted
    Private Sub Search(StaffID As String)
            Dim conn As New MySql.Data.MySqlClient.MySqlConnection
            Dim strConnString As String
    
            strConnString = ConfigurationManager.ConnectionStrings("conString").ConnectionString
    
    
            Try
    
                conn.ConnectionString = strConnString
                conn.Open()
    
                Dim cmdtText As String = "SELECT StaffID, StaffFName, StaffLName, StaffCity FROM Staff WHERE StaffID = 1"
    
                Dim ds As New DataSet
                Dim cmde As New MySql.Data.MySqlClient.MySqlCommand(cmdtText, conn)
                Dim da As New MySql.Data.MySqlClient.MySqlDataAdapter(cmde)
                cmde.CommandType = CommandType.Text
                cmde.CommandText = cmdtText
                cmde.Connection = conn
                cmde.ExecuteNonQuery()
    
                txtID.Text = 1
                txtFName.Text = "Vinay"
    
    
    
    
    
                conn.Close()
    
            Catch ex As MySql.Data.MySqlClient.MySqlException
                messagebox.Text = ("Load Course Details:" & ex.Message)
            End Try
        End Sub



    Saturday, September 12, 2020 8:42 PM

Answers

  • User-939850651 posted

    Hi vj78,

    If you want to bind the queried data to a TextBox, you need to fill the data into a DataSet or DataTable, and then get them based on the column name.

    Here is a simple example based on the code you provided:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            Search(1)
        End Sub
    
        Private Sub Search(StaffID As String)
            Dim conn As New SqlConnection
            Dim strConnString As String
    
            strConnString = ConfigurationManager.ConnectionStrings("conStr").ToString()
    
            Try
    
                conn.ConnectionString = strConnString
                conn.Open()
    
                Dim cmdtText As String = "SELECT StaffID, StaffFName, StaffLName, StaffCity FROM Staff WHERE StaffID = 1"
    
                Dim ds As New DataSet
                Dim cmde As New SqlCommand(cmdtText, conn)
                Dim da As New SqlDataAdapter(cmde)
                da.Fill(ds) 'Fill select data to DataSet or DataTable
    
                cmde.CommandType = CommandType.Text
                cmde.CommandText = cmdtText
                cmde.Connection = conn
                cmde.ExecuteNonQuery()
    
                txtID.Text = ds.Tables(0).Rows(0).Field(Of Integer)("StaffID").ToString() 'Column name => StaffID
                txtFName.Text = ds.Tables(0).Rows(0).Field(Of String)("StaffFName").ToString() 'Column name => StaffFName
    
                conn.Close()
    
            Catch ex As SqlException
                messagebox.Text = ("Load Course Details:" & ex.Message)
            End Try
        End Sub
    <form id="form1" runat="server">
            <div>
                <asp:TextBox runat="server" ID="txtID"></asp:TextBox>
                <br /><br />
                <asp:TextBox runat="server" ID="txtFName"></asp:TextBox>
                <br /><br />
                <asp:Label runat="server" ID="messagebox"></asp:Label>
            </div>
        </form>

    Result:

    Hope this can help you.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 14, 2020 2:11 AM