469,299 Members | 2,050 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

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.
<
Expand|Select|Wrap|Line Numbers
  1. Try
  2.             con.Open()
  3.             param = New SqlParameter()
  4.             param.ParameterName = "@uname"
  5.             param.Value = TextBox1.Text
  6.             cmd.CommandType = CommandType.Text
  7.             cmd.CommandText = "select * from student where fname=@uname"
  8.             cmd.Parameters.Add(param)
  9.             da = New SqlDataAdapter(cmd.CommandText, con)
  10.             da.Fill(ds)
  11.             DataGridView1.DataSource = ds.Tables(0)
  12.         Catch ex As Exception
  13.             MsgBox(ex.Message.ToString)
  14.         End Try
May 5 '10 #1
3 1720
balabaster
797 Expert 512MB
The problem is where you're assigning the data adapter. You're providing the command text and not the command:
Expand|Select|Wrap|Line Numbers
  1. da = New SqlDataAdapter(cmd.CommandText, con)
equates to the same thing as:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. Dim cmd = con.CreateCommand()
  2. Dim param = cmd.CreateParameter()
as opposed to:
Expand|Select|Wrap|Line Numbers
  1. Dim cmd = new SqlCommand()
  2. 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...
May 5 '10 #2
@balabaster
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.

Expand|Select|Wrap|Line Numbers
  1. Try
  2.             con.Open()
  3.             param = New SqlParameter()
  4.             param.ParameterName = "@uname"
  5.             param.Value = "TextBox1.Text"
  6.             cmd.Parameters.Add(param)
  7.             cmd = New SqlCommand("select * from Student where fname=@uname", con)
  8.             'cmd.CommandText = "select * from student where fname=@uname"
  9.             da = New SqlDataAdapter(cmd)
  10.             da.Fill(ds)
  11.             DataGridView1.DataSource = ds.Tables(0)
  12.         Catch ex As Exception
  13.             MsgBox(ex.Message.ToString)
  14.         End Try
May 6 '10 #3
@krisssgopi
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.

Expand|Select|Wrap|Line Numbers
  1. ry
  2.             con.Open()
  3.             param = New SqlParameter()
  4.             param.ParameterName = "@uname"
  5.             param.Value = "TextBox1.Text"
  6.             cmd.Parameters.Add(param)
  7.             cmd = New SqlCommand("select * from Student where fname=@uname", con)
  8.             'cmd.CommandText = "select * from student where fname=@uname"
  9.             da = New SqlDataAdapter(cmd)
  10.             da.Fill(ds)
  11.             DataGridView1.DataSource = ds.Tables(0)
  12.         Catch ex As Exception
  13.             MsgBox(ex.Message.ToString)
  14.         End Try
May 8 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by tim.pascoe | last post: by
2 posts views Thread by Jeff Higgins | last post: by
2 posts views Thread by Boris Zakharin | last post: by
9 posts views Thread by Daniel | last post: by
4 posts views Thread by macneed | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.