Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to display a specific record in a form

Status
Not open for further replies.

TennesseeFox

Programmer
Jul 24, 2004
14
US
I have a sql database that im using vb.net 2003 as a front end. I dont know how to do this but i want to know the syntax to query a database for a particualr record.

I guess it would be the on load of the form. have that related to another form where the record number(primarykey) is inputed. so formA askes for which record and formB displays that record with all the info from that record. And is there a way to say if there is no record for that number create a new one with that number??? like I ask for record 43, the query finds there is no 43, so in formB a new record 43 is created.

Thanks for the help

Tenn
 
Hi

If the record is only going to be displayed in form b and not updated then I'd use SqlDataReader:
Code:
    Dim mySelectQuery As String = "SELECT Stuff FROM Table WHERE PrimaryKey = @PK"
    Dim myConnection As New SqlConnection(myConnString)
    Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
    myCommand.Parameters.Add("@PK",strStoredValue)
    myConnection.Open()
    Dim myReader As SqlDataReader
    myReader = myCommand.ExecuteReader()
    ' Always call Read before accessing data.
    While myReader.Read()
        ...........Add data to form
    End While

where strStoredValue is the primary key brought from other form and obviously SQL is amended to select correct data.

Hope this makes sense. If you are loading the data for a user to update, use SqlDataAdaptor.

Thanks

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top