Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I include multiple parameters in a SQL query in C#.net using ODBCPARAMETER?

Posted on 2008-10-24
4
Medium Priority
?
342 Views
Last Modified: 2013-12-17
I have a form that I accept inputs from 5 text boxes.
I want to be able to include each input from the text boxes in my insert into query.
I want to use parameters to prevent database injection.
I also want to update the field in the database as NULL if nothing was entered in a text box.
I am using a SQL backend.

I got the following example from someone:

P = new SqlParameter("@Val1",SqlDbType.VarChar);
if (Text1.Text.Trim.Length > 0){
 P.Value = Text1.Text;
}else{
 P.Value = System.Data.DBNull.Value;
}
cmd.Parameters.Add(P);


P = new SqlParameter("@Val2",SqlDbType.VarChar);
if (Text1.Text.Trim.Length > 0){
 P.Value = Text1.Text;
}else{
 P.Value = System.Data.DBNull.Value;
}
cmd.Parameters.Add(P);

My insert command looks something like this insert into table(("@Val1","@Val2",

But when I debug the program "@Val1","@Val2", are NULL

The record is inserted in the database but the filed are empty


Instead of using "@Val1","@Val2", should I be using something like ? ,? instead because I am using ODBC?

0
Comment
Question by:wademi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 13

Accepted Solution

by:
numberkruncher earned 1500 total points
ID: 22801064
Try the following...it would be interesting to know if your results change:
P = new SqlParameter("@Val1",SqlDbType.VarChar);
if (Text1.Text.Trim.Length > 0){
 P.Value = Text1.Text;
}else{
 P.Value = 'T';
}
cmd.Parameters.Add(P);
 
 
P = new SqlParameter("@Val2",SqlDbType.VarChar);
if (Text1.Text.Trim.Length > 0){
 P.Value = Text1.Text;
}else{
 P.Value = 'T';
}
cmd.Parameters.Add(P);

Open in new window

0
 

Author Comment

by:wademi
ID: 22801312
This did not help P.Value = 'T'. I dont want to change P.Value = System.Data.DBNull.Value because if the text box are blank I want the field to be null.

I think their is something else causing the fields to be null.
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22804339
I found another Experts Exchange question for which the solution may be relevant to your problem. It suggests that you should use OdbcParameter with an OdbcCommand and that named parameters should be replaced with variables.

Check out the following link:
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_23425185.html
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22859343
Did my previous suggestion help with your problem?
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question