473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 1945
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

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

Similar topics

5
by: tim.pascoe | last post by:
I'm attempting to modify some Crosstab generating code, and I need some advice/examples. Currently, the code uses a single string variable to store the dynamically generated query...
2
by: Jeff Higgins | last post by:
I would like to use a string variable to declare a variable. How would I accomplish this goal? string nameMainMenu = "mnuMain"; string nameMenuItem = "mnuFile"; MainMenu {nameMainMenu} = new...
2
by: Boris Zakharin | last post by:
I have the following code upon which I receive the error "Must declare the variable '@job_id'". --Begin Code -- OleDbConnection conDetail = new OleDbConnection("Provider=SQLOLEDB.1;Integrated...
6
by: Michael | last post by:
Hi. I am trying to declare variables based on DB column. I have a table with 2 columns: "varname" and "varvalue". values for example: varName: 'varPhone' varValue: '450-111-2222' During the...
4
by: farmerdoug | last post by:
If I declare a variable to be type void * how do I assign values to it (int, float, and char ) and how do I read the variable. Thanks
9
by: Daniel | last post by:
I'm getting an error on this code that says index out of range. It's an unhandled exception error. What is the problem? List<unsigned intls(1000); ls = u ; Daniel
4
by: macneed | last post by:
how can i declare a variable inside a if case? if(today != recordday) { string NewRecordOfToday; } .. other work... .. .. if(today != recordday)
2
by: santusapi | last post by:
Hello all, I am not getting how to declare a variable as public in ExtJS, so that i can access it from html document. It would be nice if i get how to call a function of extjs from html. ...
5
by: krisssgopi | last post by:
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. Try con.Open() Dim param As New...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.