Debasis Mohapatra

Debasis Mohapatra

  • NA
  • 381
  • 57.1k

data is not convert to string

Sep 28 2019 10:11 AM
I am trying to convert the id to sting from the drop down. but the data is not saved in the database but the sending the mail is ok. the below is the format
var name = Convert.ToString(Session["Name"]);
var leave_type = ddlLeavetype.SelectedItem.Text.ToString();
// var leave_type = ddlLeavetype.SelectedValue.ToString();
var fromdt = _ut.ConvertDate(txtformdate.Text.Trim().Replace("'", "''"));
var todat = _ut.ConvertDate(txttodate.Text.Trim().Replace("'", "''"));
var reason = txtreasons.Text.Trim().Replace("'", "''");
var approver = ddlline.SelectedValue.ToString();
string sql = "insert into [dbo].[tbl_leave_apply]" +
" ([leave_id] " +
" ,[from_date] " +
" ,[to_date] " +
" ,[Reason] " +
" ,[approver_id] " +
" ) " +
" VALUES " +
" ('" + leave_type + "' " +
" ,'" + fromdt + "' " +
" ,'" + todat + "' " +
" ,'" + reason + "' " +
" ,'" + approver + "' " +
" ) ";
int i = _ut.ExecuteSql(sql);
but when i have changed the
var leave_type = ddlLeavetype.SelectedValue.ToString();
the data is not saved and the mail is taking the dropdown id. when i debug the code. the error
insert into [dbo].[tbl_leave_apply] ([leave_id],[from_date],[to_date] ,[Reason] ,[approver_id] ) VALUES ('PL','08/16/2019' ,'09/26/2019' ,'dasd','51840')
Msg 245, Level 16, State 1, Line 6
Conversion failed when converting the varchar value 'PL' to data type int.

Answers (2)