Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
"select TaskTimeIn,RIGHT(CONVERT(VARCHAR,TaskTimeIn,100),7)as TimeIn,TaskTimeOut,RIGHT(CONVERT(VARCHAR,TaskTimeOut,100),7)as TimeOut, TaskDescription FROM DailyTimeSheetTrackers where  EmployeeId='" + UserLog.UserId + "'',' CreateDateTime='"+StaticKeys.getLocaleDatefromUTC()+"'"



I have some confusion about this query can you please correct this query
Posted
Comments
aarif moh shaikh 16-Dec-15 4:07am    
What you mean by Separated Query?? and why?

1 solution

No, we can't - we have no idea what it is meant to do that it doesn't, much less what it doesn't do that you think it should.

But...do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT TaskTimeIn,RIGHT(CONVERT(VARCHAR,TaskTimeIn,100),7) AS TimeIn,TaskTimeOut,RIGHT(CONVERT(VARCHAR,TaskTimeOut,100),7) AS TimeOut, TaskDescription FROM DailyTimeSheetTrackers where  EmployeeId=@ID AND CreateDateTime=@CT", con))
        {
        cmd.Parameters.AddWithValue("@ID", UserLog.UserId);
        cmd.Parameters.AddWithValue("@CT", StaticKeys.getLocaleDatefromUTC());
        ...
        }
    }
It may even fix your problem...
 
Share this answer
 
Comments
Member 12097108 16-Dec-15 4:44am    
Hello Sir...
DateTime date=StaticKeys.getLocaleDatefromUTC();
string constr = ConfigurationManager.AppSettings["Main.ConnectionString"];
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("select CONVERT(VARCHAR CreateDateTime,105)AS LoginDate, TaskTimeIn,RIGHT(CONVERT(VARCHAR,TaskTimeIn,100),7)as TimeIn,TaskTimeOut,RIGHT(CONVERT(VARCHAR,TaskTimeOut,100),7)as TimeOut, TaskDescription FROM DailyTimeSheetTrackers where EmployeeId=@ID AND CreateDateTime=@CT", con))
{
cmd.Parameters.AddWithValue("@ID", UserLog.UserId);
cmd.Parameters.AddWithValue("@CT", date);
using (SqlDataAdapter da = new SqlDataAdapter())
{
cmd.Connection = con;
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
GridView2.DataSource = ds;
GridView2.DataBind();
}
}
}
This is my code.

Incorrect syntax near 'CreateDateTime'. I was get this kind of error I dont how can i solve it.
OriginalGriff 16-Dec-15 4:50am    
Commas are important:
CONVERT(VARCHAR CreateDateTime,105)
Should be
CONVERT(VARCHAR,CreateDateTime,105)
Member 12097108 16-Dec-15 5:00am    
Thank you sir..
OriginalGriff 16-Dec-15 5:08am    
You're welcome!
Member 12097108 16-Dec-15 5:13am    
Sir I have one question.
according to this coding that i have posted .gridview display record now just i want to display last inserted 10 record so how can i implement in this coding.
can you help me or hint for it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900