I was wondering if anyone knew how to code to read a record from an inventory database? I think you need to use a while loop.

while read rcd1 rcd2 rcd3
do

that's all I got so far. I am really getting upset about this pesudocode thing. I don't understand this at all.

Dani AI

Generated

’s pseudocode captured the basic idea — iterate rows — and both and gave useful starting points. The common gaps in those replies are resource cleanup, SQL safety, and null-value handling. For reliable VB.NET code, prefer explicit column lists (avoid SELECT *), parameterized commands to prevent injection, and Using blocks so connections/commands/readers are always disposed.

Below is a compact VB.NET pattern (single-row example) that builds on ’s reader approach but adds parameterization, Using blocks, and DBNull checks:

' Return a single inventory row safely
Public Function GetItemById(itemId As Integer) As InventoryItem
    Dim connString As String = "Server=.;Database=Inventory;Trusted_Connection=True;"
    Using conn As New SqlConnection(connString)
        Using cmd As New SqlCommand("SELECT ItemId, Name, Quantity FROM Items WHERE ItemId = @id", conn)
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = itemId
            conn.Open()
            Using rdr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleRow)
                If rdr.Read() Then
                    Dim name = If(rdr.IsDBNull(1), String.Empty, rdr.GetString(1))
                    Dim qty = If(rdr.IsDBNull(2), 0, rdr.GetInt32(2))
                    Return New InventoryItem(rdr.GetInt32(0), name, qty)
                End If
            End Using
        End Using
    End Using
    Return Nothing
End Function

Troubleshooting and best practices: verify the connection string and permissions, prefer ExecuteScalar for a single value, log SQL exceptions, never store plain credentials in code (use config or secure storage), and check for DBNull before casting. For many-row, disconnected scenarios use a DataAdapter/DataTable or a lightweight ORM (Dapper/EF) for cleaner mapping and paging.

Recommended Answers

All 2 Replies

Most times we use recordset objects to read from datasources. We do this by:

1. Instantiating the connection
2. Instantiating the recordset object
3. Determining what data we want returned
4. Executing the command.
5. Working with the records.

Here is a brief example of working with the records:

RECORDSET.MoveFirst 'Moves to first record in the set
DO WHILE RECORDSET.EOF = FALSE
<insert code to work with the record>
RECORDSET.MoveNext ' Advances to the next record in the set
LOOP

I was wondering if anyone knew how to code to read a record from an inventory database? I think you need to use a while loop.

while read rcd1 rcd2 rcd3
do

that's all I got so far. I am really getting upset about this pesudocode thing. I don't understand this at all.

do u want to read single record based on conditions means u can use like

Dim connString As String = "server=siraj; database=programmersheaven;" + _
                                   "uid=sa; pwd="
Dim conn As New SqlConnection(connString)
conn.Open()

Dim cmdString As String = "select * from author"
Dim cmd As New SqlCommand(cmdString, conn)

Dim reader As SqlDataReader = cmd.ExecuteReader()

While reader.Read()
            txtData.Text += reader("authorId").ToString()
            txtData.Text += ", "
            txtData.Text += reader("name").ToString()
            txtData.Text += vbCrLf
        End While
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.