Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 SqlDbType ?

Author  Topic 

DotNetMaster
Starting Member

35 Posts

Posted - 2006-11-15 : 10:59:23
How do I pass SqlDbType to a function ?


public void AddSQLCmdParameter(String pName, SqlDbType pType, object pVal)
{
if (SQLCmd == null)
{
Throw (new System.Exception("Must use SetSQLCommand to initialize SQLCommand object!"));
return;
}

if (SQLConn == null)
{
OpenSQLDb();
}

try
{
SQLCmd.Parameters.Add(pName, pType).value = pVal;
}
catch (Exception ex)
{
Throw (new System.Exception(ex.Message.ToString()));
}
}



I can do that easily with vb.net but with C# it doesnt seem to like the SqlDbType. Basically what I am trying to do is add parameters to an SQLCommand object. The thing is the parameter could be anything from integer to varchar so I dont want to hard code that...

Or should I just pass in an object and check the type ???

Thanks!

DotNetMaster
Starting Member

35 Posts

Posted - 2006-11-15 : 11:21:04
Closer...

Have to pass it in like so:


public void AddSQLCmdParameter(String pName, System.Data.SqlDbType pType, object pVal)
{
if (SQLCmd == null)
{
throw (new System.Exception("Must use SetSQLCommand to initialize SQLCommand object!"));

}

if (SQLConn == null)
{
OpenSQLDB();
}

try
{
SQLCmd.Parameters.Add(pName, pType).value = pVal;
}
catch (Exception ex)
{
throw (new System.Exception(ex.Message.ToString()));
}
}


However it does not like the line:

SQLCmd.Parameters.Add(pName, pType).value = pVal;

complaining that it does not contain a defination for value...

Any idea how to fix this one, all Im trying to do is set a parameter value to the sql command object.

Thanks
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-11-15 : 11:36:44
quote:
Originally posted by DotNetMaster

.....

complaining that it does not contain a defination for value...

Any idea how to fix this one, all Im trying to do is set a parameter value to the sql command object.

Thanks



"Value" needs to be capitalized. Note that the intellisense doesn't work so good with the Add() method for the parameters class in C#, I think because different overloads return different things.

- Jeff
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-11-15 : 11:40:56
quote:
Originally posted by DotNetMaster

Closer...

Have to pass it in like so:


public void AddSQLCmdParameter(String pName, System.Data.SqlDbType pType, object pVal)
{
if (SQLCmd == null)
{
throw (new System.Exception("Must use SetSQLCommand to initialize SQLCommand object!"));

}

if (SQLConn == null)
{
OpenSQLDB();
}

try
{
SQLCmd.Parameters.Add(pName, pType).value = pVal;
}
catch (Exception ex)
{
throw (new System.Exception(ex.Message.ToString()));
}
}


However it does not like the line:

SQLCmd.Parameters.Add(pName, pType).value = pVal;

complaining that it does not contain a defination for value...

Any idea how to fix this one, all Im trying to do is set a parameter value to the sql command object.

Thanks



By the way .. it is a very bad design to use a global command object outside of the scope of your function. You should either pass it in as an argument, or don't bother with this function at all ... I don't see how it is any easier than just adding the parameter with:

SQLCmd.Parameters.Add(pName, pType).Value = pVal;

What is the purpose of this function?

The only time I use helper functions like this are in cases where you want some nice overloads to help you out, like this:

AddParam(sqlCommand cm, string ParamName, string ParamValue)
{
cm.Parameters.Add(ParamName, sqlDbType.varchar).Value = ParamValue;
}


AddParam(sqlCommand cm, string ParamName, int ParamValue)
{
cm.Parameters.Add(ParamName, sqlDbType.int).Value = ParamValue;
}

AddParam(sqlCommand cm, string ParamName, dateTime ParamValue)
{
cm.Parameters.Add(ParamName, sqlDbType.dateTime ).Value = ParamValue;
}


... and so on... It abstracts things a bit and lets you just call Addparam and pass in a different value of different types and the overloads use the correct sqlDbType to create the parameter.


(warning: just typed in, may have case errors or others, but you get the idea)


- Jeff
Go to Top of Page
   

- Advertisement -