A Simple SQL Result displayed into a TextBox

A

Asergea

Hey all,

Please help I'm very new and want to learn,

I have a SQL Database with three columns, "ID" (Primary Key), "FName"
and "LName"

All I want to do to learn, is to type the ID Number in Textbox1, Click
"Submit" and the FName apprears in
Textbox2 and Lname Appears in Textbox3.

I'm Doing this in Visual Basic..

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Con1 As New SqlDataSource()
Con1.ConnectionString =
ConfigurationManager.ConnectionStrings("Test").ToString()
Con1.SelectCommandType = SqlDataSourceCommandType.Text
Con1.SelectCommand = "SELECT FName , LName FROM Agent WHERE ID
= 1 VALUES (@Textbox1, @Textbox2, @Textbox3)"

Con1.SelectParameters.Add("ID", Textbox1.Text)
Con1.SelectParameters.Add("FName", Textbox2.Text)
Con1.SelectParameters.Add("LName", Textbox3.Text)


End Sub = ((((((IT DOESNT" DISPLAY ANYTHING!!))))))
 
R

RobinS

You don't need the parameters to read from the database.
The VALUES clause is only used for Insert/Update.
Change Con1.SelectCommand to "SELECT FName, LName FROM Agent
WHERE ID = 1 "

Also, I haven't used SQLDataSource. Here's an example of
a way to retrieve information from SQLServer:

Dim ds As DataSet
'open the connection
Using cnn As New SqlConnection(My.Settings.ProductConnectionString)
cnn.Open()

'define the command
Dim cmd As New SqlCommand
cmd.Connection = cnn
cmd.CommandText = "SELECT * FROM Product"
'define the data adapter and fill the data table
Dim da As New SqlDataAdapter(cmd)
ds = New DataSet
da.Fill(ds, "Product")
End Using

'Read through the dataset and display the data.
For Each dr As DataRow In ds.Tables("Product").Rows
Dim ProductID As Integer = CType(dr.Item("ProductID"), Integer)
Dim ProductName As String = dr.Item("ProductName").ToString
Dim ProductNumber As String = dr.Item("ProductNumber").ToString
Dim Description As String = dr.Item("Description").ToString
Console.WriteLine(String.Format("ProductID {0}, " & _
"ProductName {1}, {2}ProductNumber {3}, " & _
"Description {4}", ProductID, ProductName, _
ControlChars.CrLf, ProductNumber, Description))
Next

HTH,
Robin S.
 
G

Ginx

Thank you so much!!

I really Appreciate it!

You don't need the parameters to read from the database.
The VALUES clause is only used for Insert/Update.
Change Con1.SelectCommand to "SELECT FName, LName FROM Agent
WHERE ID = 1 "

Also, I haven't used SQLDataSource. Here's an example of
a way to retrieve information from SQLServer:

Dim ds As DataSet
'open the connection
Using cnn As New SqlConnection(My.Settings.ProductConnectionString)
cnn.Open()

'define the command
Dim cmd As New SqlCommand
cmd.Connection = cnn
cmd.CommandText = "SELECT * FROM Product"
'define the data adapter and fill the data table
Dim da As New SqlDataAdapter(cmd)
ds = New DataSet
da.Fill(ds, "Product")
End Using

'Read through the dataset and display the data.
For Each dr As DataRow In ds.Tables("Product").Rows
Dim ProductID As Integer = CType(dr.Item("ProductID"), Integer)
Dim ProductName As String = dr.Item("ProductName").ToString
Dim ProductNumber As String = dr.Item("ProductNumber").ToString
Dim Description As String = dr.Item("Description").ToString
Console.WriteLine(String.Format("ProductID {0}, " & _
"ProductName {1}, {2}ProductNumber {3}, " & _
"Description {4}", ProductID, ProductName, _
ControlChars.CrLf, ProductNumber, Description))
Next

HTH,
Robin S.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top