0
 Dim connect As String = "Data Source=DESKTOP-D32ONKB;Initial Catalog=Attendance;Integrated Security=True"
        Using conn As New SqlConnection(connect)
            Dim dt As DataTable = New DataTable()
            Dim sql As String = "SELECT ID,Name,Class,Date FROM stuattrecordAMPM"
            Using command As New SqlCommand(sql, conn)
                Using adapter As New SqlDataAdapter(command)
                    Dim i As Integer = 0
                    For i = 0 To dt.Rows.Count - 1
                        Dim sy As String = dt.Rows(i).Item(0).ToString
                    Next
                    'command.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(TextBox1.Text)
                    adapter.Fill(dt)
                    TextBox1.Text = dt(0)(0)
                End Using
            End Using
        End Using

This code working properly asper my expectation. When I use "where ID=@ID" in sqlcommand It's showing error: 'Input string was not in a correct format.'

        Dim connect As String = "Data Source=DESKTOP-D32ONKB;Initial Catalog=Attendance;Integrated Security=True"
        Using conn As New SqlConnection(connect)
            Dim dt As DataTable = New DataTable()
            Dim sql As String = "SELECT ID,Name,Class,Date FROM stuattrecordAMPM where ID=@ID"
            Using command As New SqlCommand(sql, conn)
                Using adapter As New SqlDataAdapter(command)
                    Dim i As Integer = 0
                    For i = 0 To dt.Rows.Count - 1
                        Dim sy As String = dt.Rows(i).Item(0).ToString
                    Next
                    command.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(TextBox1.Text)
                    adapter.Fill(dt)
                    TextBox1.Text = dt(0)(0)
                End Using
            End Using
        End Using

In this code I'm getting error. Could someone help me how to declare "@ID". Thank you.. Please check the error description. enter image description here

4
  • 1
    That doesn't look like an SQL problem. Convert.ToInt32(TextBox1.Text) seems to have failed. Commented Nov 10, 2022 at 8:47
  • Can you debug the code and output the value for TextBox1.Text ? Commented Nov 10, 2022 at 8:54
  • You have to validate the input (e.g., with Integer,TryParse()) before you try and create a query with invalid values Commented Nov 10, 2022 at 9:12
  • @AnelHodžić, Output value shows TextBox1.Text Commented Nov 10, 2022 at 9:13

2 Answers 2

1

That's maybe because you are trying to add parameters using the statement of the adapter. Try this:

Dim idValue As Int = Convert.ToInt32(TextBox1.Text)
Dim dt As DataTable = New DataTable()  
Dim connect As String = "Data Source=DESKTOP-D32ONKB;Initial Catalog=Attendance;Integrated Security=True"
Using conn As New SqlConnection(connect)        
    Dim sql As String = "SELECT ID,Name,Class,Date FROM stuattrecordAMPM where ID=@ID"
    Using command As New SqlCommand(sql, conn)
        command.Parameters.Add("@ID", SqlDbType.Int).Value = idValue
            Using adapter As New SqlDataAdapter(command)                
                adapter.Fill(dt)                
            End Using            
    End Using
End Using
Dim i As Integer = 0
For i = 0 To dt.Rows.Count - 1
    Dim sy As String = dt.Rows(i).Item(0).ToString
Next
TextBox1.Text = dt(0)(0)

If you want to change the way you using to parse string to int:

Dim idValue As Int = Integer.Parse(TextBox1.Text)
    Dim dt As DataTable = New DataTable()  
    Dim connect As String = "Data Source=DESKTOP-D32ONKB;Initial Catalog=Attendance;Integrated Security=True"
    Using conn As New SqlConnection(connect)        
        Dim sql As String = "SELECT ID,Name,Class,Date FROM stuattrecordAMPM where ID=@ID"
        Using command As New SqlCommand(sql, conn)
            command.Parameters.AddWithValue("ID", idValue)
                Using adapter As New SqlDataAdapter(command)                
                    adapter.Fill(dt)                
                End Using            
        End Using
    End Using
    Dim i As Integer = 0
    For i = 0 To dt.Rows.Count - 1
        Dim sy As String = dt.Rows(i).Item(0).ToString
    Next
    TextBox1.Text = dt(0)(0)
Sign up to request clarification or add additional context in comments.

9 Comments

There's no reason to believe that Integer.Parse(TextBox1.Text) will succeed: since it's probably User input, it's bound to fail in a way or another. You validate the input first, then use the validated / converted input as the value of a Parameter -- You should not suggest using [Parameters].AddWithValue() in this context, you should use [Parameters].Add()
Yes I also think this is not the main problem. Methu maybe you should try to parse it before you connecting to database just like Jimi says. But at the end you should do the things about DataTable, after you fill the datatable.
@DevrimMertYöyen, [Parameters].Add() is showing the same error message. Could you tell me how to work on this issue. Thank you...
@methu I edited codes as Jimi adviced. Can you please try them again. If its still not working please put your TextBox1.Text to a messagebox or log and let us see
@DevrimMertYöyen, Its showing the same error: "Input string was not in a correct format". Could you help on this. Thank you...
|
0

It looks like in your broken code you need/want to have multiple "id" or more than one value. You can do this, but you ALSO then have to add the parameters to the source SQL string.

You can't just add, or have multiple @ID values for the one "@ID". If you want more than one ID value in the same SQL query, then you have to add multiple "@id1" then "@id2" and so on to the SQL text for this to work.

If you have one "@ID" then fine.

However, if you have say id 2, 134, 222, then you would have to add each parameter to the SQL string.

You can do it this way:

 dim strSQL as string = "SELECT * FROM MyTable"
 dim strWhere as string = ""

 dim cmdSQL as New Sqlcommand("", new Sqlconnection("con string here")

  ' add first @id
  strWhere = "@ID1"
  cmd.SQL.Parameters.Add("@ID1", SqlDbType.Int).Value = 124

  ' add 2nd @!id
  strWhere &= ",@ID2"
  cmd.SQL.Parameters.Add("@ID2", SqlDbType.Int).Value = 456

  ' and so on and so on
  cmdSQL.CommandText = strSQL & " WHERE ID IN (" & strWhere & ")"
  dim rstData as new DataTable()
  cmdSQL.connection.Open()
  rstData.Load(cmdSQL.ExecuteReader())

Note VERY interesting that you can create the SQL command object, and are 100% free to add as many new parameters as possible to the cmdSQL object, and EVEN do so without having the SQL command/text set for the SQL command object.

However, you eventually will have to setup/provide/have the sql shoved into that command object. So, build up the multiple "@id1, @id2" etc., and then shove that whole correct SQL string into the cmdSQL object, and it will work.

However, as noted, you are free to add as many parameters to the cmdSQL object, and even do so without having the SQL made/set/created for the cmdSQL object. They thus can be created 100% independent of the existing sql string/text (or better said lack of that SQL string during the parameter adding process).

1 Comment

There were five separate misspellings of keyword/property/method names in the code example here. I have tried to repair them, but please verify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.