Wednesday, March 7, 2012

Error in ExecuteNonQuery()

Hi,I am developing a small application where in I need to take a few data from the user and put it on the DB. I have stored procs for the same. I am getting an error when I execute the ExecuteNonQuery() command. the error is as follows:

System.InvalidCastException: Object must implement IConvertible. at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Intranet_New.leaveForm.btnSubmit_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\intranet_new\leaveform.aspx.cs:line 138

Snippet of code:

try

{

con =

new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlCon"]);

cmd =

new SqlCommand();

cmd.Connection = con;

cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText = "SP_InsertIntoLeave";

cmd.Parameters.Add("@.empid", SqlDbType.Char, 20);

cmd.Parameters["@.empid"].Value = txtEmplyId.Text;

cmd.Parameters.Add("@.empName", SqlDbType.NVarChar, 50);

cmd.Parameters["@.empName"].Value = txtName.Text;

cmd.Parameters.Add("@.LeaveFrom", SqlDbType.DateTime);

string str_LeaveFrom = ddlDay.SelectedValue + "/" +ddlMonth.SelectedValue + "/" + ddlYear.SelectedValue;

DateTime LF =

new DateTime();

LF = DateTime.Parse(str_LeaveFrom);

string LeaveFrom1 = (LF.ToShortDateString());

cmd.Parameters["@.LeaveFrom"].Value = LeaveFrom1;

cmd.Parameters.Add("@.LeaveTo", SqlDbType.DateTime);

string str_LeaveTo = ddltoDay.SelectedValue + "/" + ddltoMonth.SelectedValue + "/" + ddltoYear.SelectedValue;

DateTime LT =

new DateTime();

LT = DateTime.Parse(str_LeaveTo);

string LeaveTo1 = (LT.ToShortDateString());

cmd.Parameters["@.LeaveTo"].Value = LeaveTo1;

cmd.Parameters.Add("@.TotalDays", SqlDbType.BigInt);

cmd.Parameters["@.TotalDays"].Value = txtNoofDays.Text;

cmd.Parameters.Add("@.TypeOfLeave", SqlDbType.NVarChar, 50);

cmd.Parameters["@.TypeOfLeave"].Value = rbtnType.SelectedValue;

cmd.Parameters.Add("@.ReasonOfLeave", SqlDbType.NVarChar, 1000);

cmd.Parameters["@.ReasonOfLeave"].Value = txtReason;

con.Close();

con.Open();

cmd.ExecuteNonQuery();

con.Close();

Stored proc is as follows:

ALTER PROCEDUREdbo.SP_InsertIntoLeave

(

@.empidas char(20), @.empNameas nvarchar(50), @.totalLeaveas decimal(9) = 12, @.LeaveFromas datetime,

@.LeaveToas datetime, @.TotalDaysas bigint, @.TypeOfLeaveas nvarchar(50), @.ReasonOfLeaveas nvarchar(1000),

@.RemainigLeaveas decimal(9)

)

/*

(

@.parameter1 datatype = default value,

@.parameter2 datatype OUTPUT

)

*/

AS

/* SET NOCOUNT ON */

INSERT INTOLeave_Table

(

emp_id, emp_Name, Total_Leave, Leave_From, Leave_To, Total_no_of_Days, Type_of_Leave, Reason_of_Leave,

Leave_Remaining

)

VALUES

(

@.empid, @.empName, @.totalLeave, @.LeaveFrom, @.LeaveTo, @.TotalDays, @.TypeOfLeave, @.ReasonOfLeave,

@.RemainigLeave

)

RETURN

Thanks in Advance.

just a thought

could it be this line?

cmd.Parameters["@.TotalDays"].Value = txtNoofDays.Text;

you defined the Parameter @.TotalDays as a big int but you are supplying it a string. try parsing txtNoofDays.Text to a int.

int.Parse(txtNoofDays.Text.ToString());

hth,
mcm

No comments:

Post a Comment