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

T

tedqn

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;
}
 
R

Robbe Morris [C# MVP]

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/adonet_source_code_generator.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top