473,387 Members | 1,791 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,387 software developers and data experts.

Dynamic cast string value to a specific data type for stored procedure

Problem:
- Stored procedure input parameters data type specific (SqlDbType.Int,
SqlDbType.VarChar,etc).
- Form submitted value in TextBox, DropDownList, etc are all string
type (I guess). Some maybe empty.

I need a function that would grab the string value and dynamically
convert to the corresponding data type specific sql parameter OR
DBNull.Value. The problem is that I have to specifically specify what
data type the function will return. I don't want to do an if for every
single parameter ..

if(DateEnd.Value != "") {
myCommand.Parameters.Add("@strDateEnd",
SqlDbType.VarChar).Value =
Convert.ToDateTime(DateEnd.Value); ;
}
else {
myCommand.Parameters.Add("@strDateEnd",
SqlDbType.VarChar).Value = DBNull.Value;
}

Oct 3 '06 #1
1 2739
The link to the ado.net source generator does this. Take
the code out of it.

--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/..._generator.asp

<te***@yahoo.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Problem:
- Stored procedure input parameters data type specific (SqlDbType.Int,
SqlDbType.VarChar,etc).
- Form submitted value in TextBox, DropDownList, etc are all string
type (I guess). Some maybe empty.

I need a function that would grab the string value and dynamically
convert to the corresponding data type specific sql parameter OR
DBNull.Value. The problem is that I have to specifically specify what
data type the function will return. I don't want to do an if for every
single parameter ..

if(DateEnd.Value != "") {
myCommand.Parameters.Add("@strDateEnd",
SqlDbType.VarChar).Value =
Convert.ToDateTime(DateEnd.Value); ;
}
else {
myCommand.Parameters.Add("@strDateEnd",
SqlDbType.VarChar).Value = DBNull.Value;
}

Oct 4 '06 #2

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

Similar topics

5
by: adrian | last post by:
hi all this is my first post to this group, so pls bear with me while i try to make some sense. i am trying to create a sproc which uses dynamic sql to target a particuar table eg. '.' and...
1
by: Tim Pascoe | last post by:
I am using the Dynamic Cross-Tab code supplied in an article from SQL Server Magazine (http://www.winnetmag.com/SQLServer/Article/ArticleID/15608/15608.html). I modified the script to generate a...
4
by: MD | last post by:
I am trying to create a dynamic SQL statement to create a view. I have a stored procedure, which based on the parameters passed calls different stored procedures. Each of this sub stored procedure...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
1
by: Hifni Shahzard | last post by:
Hi, I got a stored procedure, where it returns a value. But if I execute it. It gives an error as "Invalid cast from System.Int32 to System.Byte.". To make clear how do I execute this, below I'm...
4
by: Bass Pro | last post by:
Hi, I am creating textbox, radiobuttonlist and checkboxlist dynamically depending on data from a table. It is a questionnaire. I add the control on a Panel control during the 1st load_page event....
2
by: syntego | last post by:
We commonly use triggers to log changes to our main tables to historical log tables. In the trigger, we create a concatenated string of the old values by casting them as follows: ...
1
by: itsraghz | last post by:
Hello All, I am using a Dynamic SQL Statement inside a Stored Procedure. I am using IBM DB2 V8.1 database. When i want to have the values to be filled in dynamically, i use the parameter...
0
by: jeoffh | last post by:
Background: I am trying to "merge" some attributes into an existing XML column in my MS SQL 2005 database. The general idea is that I have an XML column in a table and I would like to update/delete...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.