SQL - Fill

Posted on 2006-03-29
Last Modified: 2008-01-09
I need to retreive data from my SQL database , table name is CustomerAddress. Why it is not working?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If Me.txtTNum.Text <> "" Then


SqlDataAdapter1.SelectCommand.CommandText = "select * from results Where " & _

"transno = " & Me.txtTNum.Text



End If

End Sub

Private Sub Display(ByVal DataSet As DataSet)

Dim dataTable As DataTable = DataSet.Tables(0)

If dataTable.Rows.Count <> 0 Then

Dim recordNumber As Integer = Convert.ToInt32(dataTable.Rows(0)(0))

Me.txtAddress.Text = CStr(dataTable.Rows(0)(1))

Me.txtCity.Text = CStr(dataTable.Rows(0)(2))

Me.txtState.Text = CStr(dataTable.Rows(0)(3))

End If

End Sub

End Class
Question by:VBdotnet2005
    LVL 52

    Expert Comment

    by:Carl Tawn
    Are you getting any exceptions ? Have you checked your query to make sure it is returning any results ?
    LVL 9

    Expert Comment

    I would guess that the Data adapter hasn't been set up correctly with the correct connection string, opening the connection to the database etc.

    If you provide a little more info maybe I could be more specific, but that would be my best guess now.


    Author Comment

    Ok. do you know any similair links to anything like above sample on how to retreive data and fill in appropriate textboxes?
    LVL 7

    Accepted Solution

    If this is MSSQL, take out the spaces around "=" in your select command - it usually works anyway but occasionally cause problems.  If the SQL data type is integer, then cast the value to INT like this

    SqlDataAdapter1.SelectCommand.CommandText = "select * from results Where " & _
    "transno=" & CInt(Me.txtTNum.Text)

    If the SQL data type is NOT some numeric type, then you need single quotes around your value in your select command like this

    SqlDataAdapter1.SelectCommand.CommandText = "select * from results Where " & _
    "transno='" & Me.txtTNum.Text & "'"

    The variable object type passed in the SELECT statement must match the SQL data type, or you will get either no results or an invalid casting error on the SQL side (which will sometimes show no error on the client side, just returns empty dataset)

    My other suggestion is that using "dataTable" as a name for DataTable object is poor practice.  This can sometimes cause the interpreter to try and execute generic operations on object type instead of your object.  Same goes for your DataSet object.  

    Also, there's really no need to instantiate a dataTable in your Sub Display() - the items from the dataset are accessible directly with the syntax dsDataSet.Tables(0).Rows(i).Items(x) where i is the zero-indexed row number and x is the zero-indexed column number in that row.

    Me.txtAddress.Text = (dsDataSet.Tables(0).Rows(0).Items(1)).ToString

    If your dataadapter connection string is hosed, then none of this will help, but if that's not the problem, try these suggestions.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now