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

Parameter problem adding

I have changed my code from a straight string for my Sql to Parameters and
can't seem to get it to work.

I have the following excerpts from my code:

************************************************** *************
Dim objConn as New SqlConnection (ConnectionString)
Dim CommandText as String = "INSERT INTO ftsolutions.dbo.position
(client,JobTitle,CareerLevel,EducationLevel,Degree Required,Compensation,JobStatus,JobShift,JobCatego ry,JobDescription,Location,ContactName,ContactEmai l,ContactAddress,ContactPhone,ContactFax,Reference Code,WorkExperience,DatePosted)
VALUES (
@client,@jobTitle,@careerLevel,@educationLevel,@de greeRequired,@compensation,@jobStatus,@jobShift,@j obCategory,@jobDescription,@location,@contactName, @contactEmail,@contactAddress,@contactPhone,@conta ctFax,@referenceCode,@workExperience,@datePosted)"

Dim objCmd as New SqlCommand(CommandText,objConn)

objCmd.Parameters.Add("@client",SqlDbType.Char,50) .value = client.text
objCmd.Parameters.Add("@jobTitle",SqlDbType.VarCha r,50).value =
jobTitle.text

************************************************** ***************

I have also see it as:

objCmd.Parameters.Add(New SqlParameter("@client",SqlDbType.Char,50)).value =
client.text

But this doesn't work either.

I get the following error:

************************************************** ********************
Must declare the variable '@client'.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Must declare the
variable '@client'.

Source Error:

Line 71: ' objCommand.Prepare()
Line 72:
Line 73: Dim objDataReader as SqlDataReader = objCommand.ExecuteReader( _
Line 74: CommandBehavior.CloseConnection)
Line 75:
Source File: c:\inetpub\wwwroot\development\staffing\addPositio ns2.aspx
Line: 73
************************************************** ********************

As far as I can tell, @client is being defined.

What am I missing?

Also, in some places I see:

objCmd.Parameters.Add("@client",SqlDbType.VarChar, 50).value = client.text

and other places

objCmd.Parameters.Add("@client",SqlDbType.VarChar) .value = client.text

Why do I need the size of the VarChar (and do I)?

Thanks,

Tom.

Nov 18 '05 #1
1 1166
Suggestion: Take the SQL out of your code and create a stored procedure with
your DBMS. I would create the following stored proc based on your sql
statements
CREATE PROCEDURE dbo.spSomeProcedure
@client as char(50),
@jobtitle as varchar(50),
etc etc etc....
@lastparameter as varchar(50)
AS
INSERT INTO ftsolutions.dbo.position
(client,JobTitle,CareerLevel,EducationLevel,Degree Required,Compensation,

JobStatus,JobShift,JobCategory,JobDescription,Loca tion,ContactName,ContactEm
ail,ContactAddress,ContactPhone,
ContactFax,ReferenceCode,WorkExperience,DatePosted )
VALUES
(@client,@jobTitle,@careerLevel,@educationLevel,@d egreeRequired,@compensatio
n,@jobStatus,@jobShift,@jobCategory,

@jobDescription,@location,@contactName,@contactEma il,@contactAddress,@contac
tPhone,@contactFax,@referenceCode,
@workExperience,@datePosted)
GO

Then from code set up your connection (objCon below).

(VB)
dim objCom as new sqlclient.sqlcommand("spSomeProcedure", objCon)
objCom.CommandType=CommandType.StoredProcedure

'repeat this for all parameters
dim prmClient as new sqlclient.sqlparameter("@client", sqldbtype.char, 50)
prmClient.value=client.text
objcom.parameters.add(prmClient)

'once you've added all the parameters to the command object you execute (for
insert you shouldn't need a return so a execute nonquery will work)
objcon.open()
objcom.executenonquery()
objcon.close()

It may not be the best way, but it's a solution.....
Hope it helps


"tshad" <ts**********@ftsolutions.com> wrote in message
news:e4**************@TK2MSFTNGP11.phx.gbl...
I have changed my code from a straight string for my Sql to Parameters and
can't seem to get it to work.

I have the following excerpts from my code:

************************************************** *************
Dim objConn as New SqlConnection (ConnectionString)
Dim CommandText as String = "INSERT INTO ftsolutions.dbo.position
(client,JobTitle,CareerLevel,EducationLevel,Degree Required,Compensation,JobS
tatus,JobShift,JobCategory,JobDescription,Location ,ContactName,ContactEmail,
ContactAddress,ContactPhone,ContactFax,ReferenceCo de,WorkExperience,DatePost
ed) VALUES (
@client,@jobTitle,@careerLevel,@educationLevel,@de greeRequired,@compensation
,@jobStatus,@jobShift,@jobCategory,@jobDescription ,@location,@contactName,@c
ontactEmail,@contactAddress,@contactPhone,@contact Fax,@referenceCode,@workEx
perience,@datePosted)"
Dim objCmd as New SqlCommand(CommandText,objConn)

objCmd.Parameters.Add("@client",SqlDbType.Char,50) .value = client.text
objCmd.Parameters.Add("@jobTitle",SqlDbType.VarCha r,50).value =
jobTitle.text

************************************************** ***************

I have also see it as:

objCmd.Parameters.Add(New SqlParameter("@client",SqlDbType.Char,50)).value = client.text

But this doesn't work either.

I get the following error:

************************************************** ********************
Must declare the variable '@client'.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Must declare the
variable '@client'.

Source Error:

Line 71: ' objCommand.Prepare()
Line 72:
Line 73: Dim objDataReader as SqlDataReader = objCommand.ExecuteReader( _ Line 74: CommandBehavior.CloseConnection)
Line 75:
Source File: c:\inetpub\wwwroot\development\staffing\addPositio ns2.aspx
Line: 73
************************************************** ********************

As far as I can tell, @client is being defined.

What am I missing?

Also, in some places I see:

objCmd.Parameters.Add("@client",SqlDbType.VarChar, 50).value = client.text
and other places

objCmd.Parameters.Add("@client",SqlDbType.VarChar) .value = client.text

Why do I need the size of the VarChar (and do I)?

Thanks,

Tom.

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can...
2
by: Mike | last post by:
I have a dataadapter that has "where field_name = @parameter". I need to replace the @parameter and then call fillschema and then fill. I've looked too long in the help files and haven't been...
0
by: JSantora | last post by:
Essentially, InsertAT is broken! For the past couple of hours, I've been getting this "Parameter name: '-2147483550' is not a valid value for 'index'." error. Apparently, its caused by having...
3
by: Robert | last post by:
What is the best way to pass a parameter to an ObjectDataSource. I am able to add a new parameter to the SelectParameters, but I would like to just assign a value to an existing parmeter at...
8
by: Alec MacLean | last post by:
Hi, I'm using the DAAB Ent Lib (Jan 2006) for .NET 2.0, with VS 2005 Pro. My project is a Web app project (using the WAP add in). Background: I'm creating a survey system for our company, for...
9
by: serge | last post by:
/* Subject: How to build a procedure that returns different numbers of columns as a result based on a parameter. You can copy/paste this whole post in SQL Query Analyzer or Management Studio...
2
by: JJ | last post by:
How can I set an ObjectDataSource's Parameter programmatically? The slight complication here is that I want to set this NOT in the 'Selecting' method of the ObjectDataSource, but elsewhere. I...
2
by: abhimanyu singh | last post by:
i m facing a serious probs now a days............ its seems easy but for me i found it difficult......... i want to do transformation usig xsl and xml(adding parameter). for IE its working fine...
0
by: Adam Right | last post by:
Hi All, i am using CommandArgument of imagebutton in a Repeater for adding a item to shopping basket, like that; ************************** <asp:Repeater ID="Repeater1" runat="server"...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.