Home > database >  How to use cmd.parameters.add("@ID") SQL, VB.NET
How to use cmd.parameters.add("@ID") SQL, VB.NET

Time:11-11

 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

CodePudding user response:

Thats maybe because you are try to add parameters in using statement of 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)

Best Regards Devrim

CodePudding user response:

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.

So, if you have ONE "@ID" then fine.

However, if you have say id 2, 134, 222?

Then you would have to add each parmater 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 Sqlconneciton("con string here")

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

  ' add 2nd @!id
  strWhere &= ",@ID2"
  cmd.SQL.Paramaters.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.conneciton.Open()
  rstData.Load(cmdSQL.ExectuteReader())

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 100% 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).

  • Related