467,146 Members | 1,260 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

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
  • viewed: 2444
Share:
1 Reply
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Tim Pascoe | last post: by
1 post views Thread by Nathan Bloomfield | last post: by
1 post views Thread by Hifni Shahzard | last post: by
4 posts views Thread by Bass Pro | last post: by
2 posts views Thread by syntego@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.