Must declare the variable @uname

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krisssgopi
    New Member
    • Apr 2010
    • 39

    #1

    Must declare the variable @uname

    Hi all,

    When i run the below code it was throwing an exception must declare the variable @uname. please help me to solve this issue.

    Code:
    Try
                con.Open()
                Dim param As New SqlParameter()
                param.ParameterName = "@uname"
                param.Value = TextBox1.Text
                cmd = New SqlCommand("select * from customer where custname=@uname", con)
                da = New SqlDataAdapter(cmd)
                da.Fill(ds)
                DataGridView1.DataSource = ds.Tables(0)
            Catch ex As Exception
                MsgBox(ex.Message.ToString)
            End Try
            con.Close()
  • Jim Doherty
    Recognized Expert Contributor
    • Aug 2007
    • 897

    #2
    Originally posted by krisssgopi
    Hi all,

    When i run the below code it was throwing an exception must declare the variable @uname. please help me to solve this issue.

    Code:
    Try
                con.Open()
                Dim param As New SqlParameter()
                param.ParameterName = "@uname"
                param.Value = TextBox1.Text
                cmd = New SqlCommand("select * from customer where custname=@uname", con)
                da = New SqlDataAdapter(cmd)
                da.Fill(ds)
                DataGridView1.DataSource = ds.Tables(0)
            Catch ex As Exception
                MsgBox(ex.Message.ToString)
            End Try
            con.Close()
    Do you not need to concatenate the value of the variable to the string not include the name of the parameter variable itself. Look at your sql statement string again, as is it thinks @uname is a column?

    Comment

    • krisssgopi
      New Member
      • Apr 2010
      • 39

      #3
      Originally posted by Jim Doherty
      Do you not need to concatenate the value of the variable to the string not include the name of the parameter variable itself. Look at your sql statement string again, as is it thinks @uname is a column?
      hi please help me. i cant able to get you. please make it in code. so that i can able to get into the clear idea.

      Comment

      • Jim Doherty
        Recognized Expert Contributor
        • Aug 2007
        • 897

        #4
        Originally posted by krisssgopi
        hi please help me. i cant able to get you. please make it in code. so that i can able to get into the clear idea.
        Pre-supposing @uname contains a numeric value then building the SQL string should look like the below should it not? rather than putting @uname inside the string as you have currently

        Code:
        cmd = New SqlCommand("select * from customer where custname=" & @uname, con)
        if the value of @uname is text then you would further need to wrap the value in quotes so that whatever sql is fired to the server to return a recordset would be able to understand the datatype

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Code:
          Try
                      con.Open()
                      cmd = New SqlCommand("select * from customer where custname=@uname", con)
                      cmd.Parameters.AddWithValue("uname", TextBox1.Text);
                      da = New SqlDataAdapter(cmd)
                      da.Fill(ds)
                      DataGridView1.DataSource = ds.Tables(0)
                  Catch ex As Exception
                      MsgBox(ex.Message.ToString)
                  End Try
                  con.Close()
          This should be the proper way to do what you want.

          @Jim Doherty
          The way you are suggesting I do not believe will work. In the code side, the parameter has to be added to the command object, not the command text.

          Also, string concatenation leaves you vulnerable to SQL Injection attacks. Using a parameter like the way I mentioned protects you from that.

          Comment

          • Jim Doherty
            Recognized Expert Contributor
            • Aug 2007
            • 897

            #6
            Originally posted by insertAlias
            Code:
            Try
                        con.Open()
                        cmd = New SqlCommand("select * from customer where custname=@uname", con)
                        cmd.Parameters.AddWithValue("uname", TextBox1.Text);
                        da = New SqlDataAdapter(cmd)
                        da.Fill(ds)
                        DataGridView1.DataSource = ds.Tables(0)
                    Catch ex As Exception
                        MsgBox(ex.Message.ToString)
                    End Try
                    con.Close()
            This should be the proper way to do what you want.

            @Jim Doherty
            The way you are suggesting I do not believe will work. In the code side, the parameter has to be added to the command object, not the command text.

            Also, string concatenation leaves you vulnerable to SQL Injection attacks. Using a parameter like the way I mentioned protects you from that.
            We learn something new every day I stand corrected :)

            Comment

            Working...