I want to search for a number embedded in a string in a field in our log table using a parameter.
select * from vwLogs where log_time >\'02/24/2009\' and message lik
WHERE message like '%' + @ErrorMessage + '%'
Based on your edit I don't immediately see what's causing your error, but I did spot two potential issues:
Try this:
protected void btnRunQuery_Click(object sender, EventArgs e)
{
string strConn = @";";
string strSQL =
"SELECT * "
+ " FROM weblogs.dbo.vwlogs"
+ " WHERE Log_time >= @BeginDate AND Log_Time < @EndDate"
+ " AND (client_user=@UserName OR @UserName IS NULL)"
+ " AND (message like '%' + @ErrorNumber + '%' OR @ErrorNumber IS NULL)"
+ " ORDER BY Log_time DESC";
using (SqlConnection cn = new SqlConnection(strConn))
using (SqlCommand cmd = new SqlCommand(strSQL, cn))
{
cmd.Parameters.Add("@BeginDate", SqlDbType.DateTime).Value =
DateTime.Parse(txtBeginDate.Text).Date;
cmd.Parameters.Add("@EndDAte", SqlDbType.DateTime).Value =
// add one to make search inclusive
DateTime.Parse(txtEndDate.Text).Date.AddDays(1);
cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value =
string.IsNullOrEmpty(txtUserName.Text) ? DBNull.Value : txtUserName.Text;
cmd.Parameters.Add("@ErrorNumber", SqlDbType.VarChar, 50).Value =
string.IsNullOrEmpty(txtErrorNumber.Text) ? DBNull.Value : txtErrorNumber.Text;
cn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
GridView1.DataSource = rdr;
GridView1.DataBind();
}
}
BTW: didn't I give you that code in the first place? :)