hi, i am trying to display data by binding to grid view.
It is throwing an exception stating "Exception In ExecuteReader: System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting the varchar value 'TLV001' to data type int."
I am not able to convert username entered in a textbox into varchar.
How to resolve this.
public partial class Reports_GenerateAttendance : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetMonth();
GetYear();
}
protected void GetMonth()
{
//ddl_monthyear.items.clear();
try
{
for (int month = 1; month <= 12; month++)
{
string monthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(month);
ddl_MonthYear.Items.Add(new ListItem(monthName, month.ToString().PadLeft(2, '0')));
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
protected void GetYear()
{
try
{
int year = DateTime.Now.Year;
for (int i = year - 5; i <= year; i++)
{
ListItem li = new ListItem(i.ToString());
ddl_year.Items.Add(li);
}
}
catch (Exception ex)
{
}
}
protected void btn_Details_Click1(object sender, EventArgs e)
{
DataTable dt = new DataTable();
try
{
string username =Convert.ToString(txtUserId.Text);
DBHelper obCon = new DBHelper(ConfigurationManager.AppSettings["localCommon"].ToString());
string query = " select td.agentid , ud.fullname as 'username', sm.ShiftType, sm.StartTime as 'shift in', " +
"sm.EndTime as 'shift out',CAST(td.[Datetime] as time) as 'logged in', " +
" cast(td.[Fld_Date_Time_End] as time) as 'log out' " + " from switch.dbo.timedistribution td " +
" inner join common.dbo.Userdetails ud on td.agentid = ud.UserID " +
"inner join common.dbo.User_Dailyshift uds on uds.userid = td.agentid " +
"inner join common.dbo.shift_master sm on sm.shiftid = uds.shiftid and td.agentid = uds.userid " +
"where month(td.[Datetime]) = " + ddl_MonthYear.SelectedItem.Value +
" and year(td.[Datetime]) = " + ddl_year.SelectedItem.Value +
" and td.agentid = " +username;
DataSet ds = new DataSet();
ds = obCon.ExecuteAdapter(query);
//SqlDataAdapter adpt = new SqlDataAdapter(cmd);
//adpt.Fill(dt);
grid_Report.DataSource = ds;
grid_Report.DataBind();
}
catch (Exception ex)
{
Response.Write(ex);
}
}
}