473,320 Members | 2,094 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,320 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. Try
  2.             con.Open()
  3.             Dim param As New SqlParameter()
  4.             param.ParameterName = "@uname"
  5.             param.Value = TextBox1.Text
  6.             cmd = New SqlCommand("select * from customer where custname=@uname", con)
  7.             da = New SqlDataAdapter(cmd)
  8.             da.Fill(ds)
  9.             DataGridView1.DataSource = ds.Tables(0)
  10.         Catch ex As Exception
  11.             MsgBox(ex.Message.ToString)
  12.         End Try
  13.         con.Close()
May 16 '10 #1
5 1927
Jim Doherty
897 Expert 512MB
@krisssgopi
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?
May 16 '10 #2
@Jim Doherty
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.
May 16 '10 #3
Jim Doherty
897 Expert 512MB
@krisssgopi
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

Expand|Select|Wrap|Line Numbers
  1. 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
May 16 '10 #4
Curtis Rutland
3,256 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. Try
  2.             con.Open()
  3.             cmd = New SqlCommand("select * from customer where custname=@uname", con)
  4.             cmd.Parameters.AddWithValue("uname", TextBox1.Text);
  5.             da = New SqlDataAdapter(cmd)
  6.             da.Fill(ds)
  7.             DataGridView1.DataSource = ds.Tables(0)
  8.         Catch ex As Exception
  9.             MsgBox(ex.Message.ToString)
  10.         End Try
  11.         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.
May 17 '10 #5
Jim Doherty
897 Expert 512MB
@insertAlias
We learn something new every day I stand corrected :)
May 17 '10 #6

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

Similar topics

1
by: Wajih-ur-Rehman | last post by:
I am using the following query: query = "insert into SystemEventsProperties (ID,PName,PValue) values (@id,@pName,@pValue)"; OdbcParameter param1 = new OdbcParameter(); param1.DbType =...
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...
3
by: Ken Adeniji | last post by:
Must declare the scalar variable '@FirstName' ContactGridViewWebForm.aspx <aspqlDataSource RunAt="server" ID="SqlDataSourceContact" ...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
2
by: saravana784 | last post by:
USE GO /****** Object: StoredProcedure . Script Date: 06/09/2007 20:15:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO
1
by: adz1809 | last post by:
I'm having a problem with updating a record through a form. Here is the error: Server Error in '/AccoEndUser' Application....
1
by: Mark | last post by:
I am really struggling with this, trying to understand why it doesn't work. I've simplified the code down to the essentials. The GridView displays in ReadOnly mode just fine, Clicking the "edit"...
1
by: Tony M | last post by:
vs 2005 - vb .net - web forms - xp pro Can't figure out why I keep getting the error Must declare the scalar variable "@CheckUser". The error happens on SqlDA.Fill(SqlDS). Just trying to make...
3
by: krisssgopi | last post by:
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....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.