Exception: must declare the variable '@uname'

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

    Exception: must declare the variable '@uname'

    Hi Team,
    Thanks for your effort. When am trying to execute below code it was throwing an exception must declare the variable @uname. Please trigger me where it was going wrong. thanks in advance.
    <
    Code:
    Try
                con.Open()
                param = New SqlParameter()
                param.ParameterName = "@uname"
                param.Value = TextBox1.Text
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "select * from student where fname=@uname"
                cmd.Parameters.Add(param)
                da = New SqlDataAdapter(cmd.CommandText, con)
                da.Fill(ds)
                DataGridView1.DataSource = ds.Tables(0)
            Catch ex As Exception
                MsgBox(ex.Message.ToString)
            End Try
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    The problem is where you're assigning the data adapter. You're providing the command text and not the command:
    Code:
    da = New SqlDataAdapter(cmd.CommandText, con)
    equates to the same thing as:
    Code:
    da = New SqlDataAdapter("select * from student where fname=@uname", con)
    What you actually need is to new it up using the command object you've painstakingly built and attached your parameter to:
    Code:
    da = New SqlDataAdapter(cmd, con)
    Hope that helps...

    Side note: When you've got an existing command you want to attach a parameter to, it's a better practice to use:
    Code:
    Dim cmd = con.CreateCommand()
    Dim param = cmd.CreateParameter()
    as opposed to:
    Code:
    Dim cmd = new SqlCommand()
    Dim param = new SqlParameter()
    This way if you ever need to modify the type of database connection you're using, you only need to change the connection type - the rest will follow without having to modify tons of code. In fact, where at all possible, the type of connection should even be configurable through a configuration file and should automatically determine the it's type - but that's another topic for another day...

    Comment

    • krisssgopi
      New Member
      • Apr 2010
      • 39

      #3
      Originally posted by balabaster
      The problem is where you're assigning the data adapter. You're providing the command text and not the command:
      Code:
      da = New SqlDataAdapter(cmd.CommandText, con)
      equates to the same thing as:
      Code:
      da = New SqlDataAdapter("select * from student where fname=@uname", con)
      What you actually need is to new it up using the command object you've painstakingly built and attached your parameter to:
      Code:
      da = New SqlDataAdapter(cmd, con)
      Hope that helps...

      Side note: When you've got an existing command you want to attach a parameter to, it's a better practice to use:
      Code:
      Dim cmd = con.CreateCommand()
      Dim param = cmd.CreateParameter()
      as opposed to:
      Code:
      Dim cmd = new SqlCommand()
      Dim param = new SqlParameter()
      This way if you ever need to modify the type of database connection you're using, you only need to change the connection type - the rest will follow without having to modify tons of code. In fact, where at all possible, the type of connection should even be configurable through a configuration file and should automatically determine the it's type - but that's another topic for another day...
      Hi team,

      i modified the code as per your instruction. But it was throwing the same error.
      how shall i declare the sql parameter. Please help me.

      Code:
      Try
                  con.Open()
                  param = New SqlParameter()
                  param.ParameterName = "@uname"
                  param.Value = "TextBox1.Text"
                  cmd.Parameters.Add(param)
                  cmd = New SqlCommand("select * from Student where fname=@uname", con)
                  'cmd.CommandText = "select * from student where fname=@uname"
                  da = New SqlDataAdapter(cmd)
                  da.Fill(ds)
                  DataGridView1.DataSource = ds.Tables(0)
              Catch ex As Exception
                  MsgBox(ex.Message.ToString)
              End Try

      Comment

      • krisssgopi
        New Member
        • Apr 2010
        • 39

        #4
        Originally posted by krisssgopi
        Hi team,

        i modified the code as per your instruction. But it was throwing the same error.
        how shall i declare the sql parameter. Please help me.

        Code:
        Try
                    con.Open()
                    param = New SqlParameter()
                    param.ParameterName = "@uname"
                    param.Value = "TextBox1.Text"
                    cmd.Parameters.Add(param)
                    cmd = New SqlCommand("select * from Student where fname=@uname", con)
                    'cmd.CommandText = "select * from student where fname=@uname"
                    da = New SqlDataAdapter(cmd)
                    da.Fill(ds)
                    DataGridView1.DataSource = ds.Tables(0)
                Catch ex As Exception
                    MsgBox(ex.Message.ToString)
                End Try
        i modified the code as per your instruction. But it was throwing the same error.
        how shall i declare the sql parameter. Please help me.

        Code:
        ry
                    con.Open()
                    param = New SqlParameter()
                    param.ParameterName = "@uname"
                    param.Value = "TextBox1.Text"
                    cmd.Parameters.Add(param)
                    cmd = New SqlCommand("select * from Student where fname=@uname", con)
                    'cmd.CommandText = "select * from student where fname=@uname"
                    da = New SqlDataAdapter(cmd)
                    da.Fill(ds)
                    DataGridView1.DataSource = ds.Tables(0)
                Catch ex As Exception
                    MsgBox(ex.Message.ToString)
                End Try

        Comment

        Working...