Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Whats wrong with my call sproc code?

Status
Not open for further replies.

Kalisto

Programmer
Feb 18, 2003
997
GB
Code:
string strConn = ConfigurationSettings.AppSettings["strConnection"]; 
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
			
//Create an SQL Command object, and assign
SqlCommand cmd = new SqlCommand();
cmd.Connection=conn;
cmd.CommandText="spNewUser";
cmd.CommandType=CommandType.StoredProcedure;

//Create The Parameters
SqlParameter param;
//Add Coorperate Status
param = cmd.Parameters.Add("@blCooperate",SqlDbType.Bit);
param.Direction = ParameterDirection.Input;
param.Value = blUserType;
//Add Password
param = cmd.Parameters.Add("@strPassword",SqlDbType.VarChar);
param.Direction = ParameterDirection.Input;
param.Value = tbPassword;
//Add Number of Properties
param = cmd.Parameters.Add("@nProperties",SqlDbType.Int);
param.Direction = ParameterDirection.Input;
param.Value = 0;
//Add First Name
param = cmd.Parameters.Add("@strFName",SqlDbType.VarChar);
param.Direction = ParameterDirection.Input;
param.Value = tbFName;
//Add Last Name
param = cmd.Parameters.Add("@strSName",SqlDbType.VarChar);
param.Direction = ParameterDirection.Input;
param.Value = tbFName;

cmd.ExecuteNonQuery();

When I run the above, I get the following Error Message in my browser
" System.InvalidCastException: Object must implement IConvertible"

The Highlighted line is the NonQuery line
I have never before had issues using sprocs, but this has got me confused.

Anyone know where to even start fixing this ?

Cheers,

K
 
Looks like you are over writing param every time for a new parameter.
I am not sure I use param array but I believe you can add to your command like you are doing. Try adding all of your parameters like this.
Code:
SqlParameter prmstrPassword = New SqlParameter("@strPassword", SqlDbType.VarChar)
prmstrPassword.Value = tbPassword;
prmstrPassword.Direction = ParameterDirection.Input;
cmd.Parameters.Add(prmstrPassword);
This was from your password parameter.

hth,
Marty
 
Thanks Marty, the code works fine the way it is, it does build up the param array. The issue was more fundamental (Fixed it now)

All my fields that came from my form were being passed. not the contents. And there is no IConverter for a text box to SQL VarChar !

All working now, on to the next problem !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top