Hye, can one please explain to me about the code below. I need to know what exactly should be inside Field1 and Field2? I'm new to vb.net. I know that tbltemp should be the name of our table but im confused about the Field1 and Field2.

Dim strSQL as String
strSQL = "SELECT Field1, Field2 FROM tbltemp WHERE Field1 = '" & TextBox1.Text & "'"

Dani AI

Generated

As noted, Field1 and Field2 are merely placeholders: they must be the actual column names from the table (exact spelling and, on case-sensitive servers, exact case). If a column name contains spaces or is a reserved word, enclose it in brackets (for example, [Column Name]). Column names and types determine whether values must be quoted in SQL and which SqlDbType to use when passing parameters.

To list the real column names and their data types, run this in the database (for example from SSMS):

SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'tbltemp';

Use a parameterized command in VB.NET rather than concatenating TextBox text into SQL. Parameterization prevents SQL injection and avoids quoting/type mistakes. Example pattern:

Dim sql As String = "SELECT ColumnA, ColumnB FROM tbltemp WHERE ColumnA = @val"

Using conn As New SqlConnection(connString)
    Using cmd As New SqlCommand(sql, conn)
        cmd.Parameters.Add("@val", SqlDbType.NVarChar, 100).Value = TextBox1.Text.Trim()
        conn.Open()
        Using rdr As SqlDataReader = cmd.ExecuteReader()
            While rdr.Read()
                ' use rdr("ColumnA"), rdr("ColumnB")
            End While
        End Using
    End Using
End Using

Practical notes and common pitfalls:

  • If the filter column is numeric or datetime, declare the parameter with the matching SqlDbType (do not pass numbers as quoted strings).
  • The string "dboBatchReportHeader" in the later post looks like a missing schema separator; the typical form is dbo.BatchReportHeader.
  • Avoid AddWithValue for production code when precise types/lengths matter; prefer Parameters.Add with explicit SqlDbType.
  • For debugging, inspect SqlException.Message and parameter values (avoid logging secrets). Trim input to remove stray whitespace.

These checks explain what belongs in Field1/Field2 and give a safer, debuggable pattern for running the query from VB.NET.

Recommended Answers

All 2 Replies

Is this line of code correct? I'm just trying..

sqlCmd = New SqlCommand _
                ("SELECT FormulaCode, BatchAbsNo, FormulaName, BatchStartTime, BatchFinishTime FROM dboBatchReportHeader WHERE FormulaCode = '" & TextBoxIngCode.Text & "'")

field1 and field2 should be the name of the columns in your table

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.