SQL To Text Box

Simple enough, I'm trying to take information out of a Column in an SQL Database where the Members_ID is equal to the integer the users inputs to a Textbox.

  • SendFromID.Text is the textbox containing the Members_ID

  • Current_Points is the textbox I want to hold/show the SQL Column info

  • U_G_Studio is the SQL Column holding the data

Only 1 row can match the Members_ID as it's a Primary Key.

This is as far as I've managed to get with my code; after this I just get lost. If you could provide an example in code I'd really appreciate it.

stcon = "CONNECTION STRING;"
        con = New SqlConnection(stcon)

        con.Open()

        Dim cmd As New SqlCommand("SELECT U_G_Studio FROM PersonsA WHERE Members_ID ='" & SendFromID.Text & "'", con)
        cmd.CommandType = CommandType.Text

{EDIT 1}

Just want to clarify, I need to DOWNLOAD the data from the SQL Column into the texbox.

Answers


Try something like this. Using automatically takes care of disposing resources. Also parameterize your query to avoid sql injection attacks.

stcon = "CONNECTION STRING;"
Dim strUGStudio As String

Dim sql As String = _
"SELECT U_G_Studio FROM PersonsA WHERE Members_ID = @MemberId"

Using conn As New SqlConnection(stcon)
    Dim cmd As New SqlCommand(sql, conn)
    cmd.Parameters.AddWithValue("@MemberId", SendFromID.Text)
    Try
        conn.Open()
        strUGStudio = Convert.ToString(cmd.ExecuteScalar())
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try 
End Using 

Return strUGStudio 

Use this:

Dim cmd As New SqlCommand("SELECT U_G_Studio FROM PersonsA WHERE Members_ID = @id")
cmd.Parameters.AddWithValue("@id", SendFromID.Text)

You need to then do ExecuteScalar(), assign that to a variable, and display it in the text box.


If the Members Id is integer then you should try

Dim cmd As New SqlCommand("SELECT U_G_Studio FROM PersonsA WHERE Members_ID =" &  SendFromID.Text , con)

I ended up with this code:

stcon = "CONNECTION STRING"
        con = New SqlConnection(stcon)

        da = New SqlDataAdapter("SELECT U_G_Studio FROM PersonsA WHERE Members_ID =" & SendFromID.Text, con)
        Dim dt = New DataTable

        da.Fill(dt)

            CurrentPoints.Text = dt.Rows(0)("U_G_Studio").ToString()

Need Your Help

Broken php array indexing

php arrays json

There must be something obvious I'm doing wrong here, but I've spent hours trying to figure it out and it makes no sense.

Execute command stored in variable in fish shell

fish

I would like to run the following command: