Home All Groups Group Topic Archive Search About

A Simple SQL Result displayed into a TextBox

Author
10 Dec 2006 10:27 PM
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!!))))))

Author
11 Dec 2006 3:34 AM
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.
------------------------------------------------
<Aser***@Cableone.net> wrote in message
Show quoteHide quote
news:1165789675.524476.267550@n67g2000cwd.googlegroups.com...
> 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!!))))))
>
Author
18 Dec 2006 2:50 AM
Ginx
Thank you so much!!

I really Appreciate it!


RobinS wrote:
Show quoteHide quote
> 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.
> ------------------------------------------------
> <Aser***@Cableone.net> wrote in message
> news:1165789675.524476.267550@n67g2000cwd.googlegroups.com...
> > 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!!))))))
> >