2

I need to query a DataGridView using SQL but don't show to DataGridView.

public chkTime()
{
    InitializeComponent();
}

HRTaffDataContext db = new HRTaffDataContext();
SqlConnection Conn;
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da;
DataTable dt = new DataTable();
DataSet ds = new DataSet();
StringBuilder sb = new StringBuilder();
string appConn = ConfigurationManager.ConnectionStrings["connDB"].ConnectionString;
string strDate;

private void chkTime_Load(object sender, EventArgs e)
{
    connStr();
    return;
}

public void connStr()
{
    Conn = new SqlConnection();
    if (Conn.State == ConnectionState.Open)
    {
        Conn.Close();
    }
    Conn.ConnectionString = appConn;
    Conn.Open();
}

private void button2_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(appConn);

    string sql = "SELECT [filesTA].EmpNo,[Employee].[First Name],[filesTA].ChkDate,[filesTA].ChkIn,[filesTA].ChkOut,[CompanyData].ShortName"
    + " From [WebSP].[dbo].[filesTA] inner join [WebSP].[dbo].[Employee] on [Employee].EmployeeNo=[filesTA].EmpNo INNER JOIN [WebSP].[dbo].[CompanyData] On [CompanyData].Company = [Employee].Company"
    + " WHERE [filesTA].ErrorCode = 0"; // It's work
    + " WHERE [filesTA].ErrorCode = 0 and [filesTA].ChkDate ='" + dateTimePicker.Text.ToString() + "'";

    da = new SqlDataAdapter(sql, Conn);
    DataSet ds = new DataSet();
    da.Fill(ds);
    Conn.Close();

    dgvShow.DataSource = ds.Tables[0];
}
  • "WHERE [filesTA].ErrorCode = 0" works fine.

  • "WHERE [filesTA].ErrorCode = 0 and [filesTA].ChkDate ='" + dateTimePicker.Text.ToString() + "'" does not work.

I need to set where DateTime.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
nettoon493
  • 17,733
  • 7
  • 30
  • 45
  • That means your [filesTA].ChkDate doesn't match the date you selected. – MVCKarl Nov 13 '12 at 07:46
  • 2
    FYI, why don't you use the `SQLParameter`? Easy to use and prevent any SQL injection. [Here you can see](http://stackoverflow.com/a/425896/1498857) a little example – Gianni B. Nov 13 '12 at 07:47

2 Answers2

1

$dateTimePicker.Text returns a string for human reading and your server maybe don't like it.

Try something like:

string sql = string.Format("SELECT [filesTA].EmpNo,[Employee].[First Name],[filesTA].ChkDate,[filesTA].ChkIn,[filesTA].ChkOut,[CompanyData].ShortName"
            + " From [WebSP].[dbo].[filesTA] inner join [WebSP].[dbo].[Employee] on [Employee].EmployeeNo=[filesTA].EmpNo INNER JOIN [WebSP].[dbo].[CompanyData] On [CompanyData].Company = [Employee].Company"
            + " WHERE [filesTA].ErrorCode = 0 and [filesTA].ChkDate ='{0}-{1}-{2}'",
                dateTimePicker.Value.Year,
                dateTimePicker.Value.Month,
                dateTimePicker.Value.Day);
joseangelmt
  • 2,018
  • 18
  • 32
0

You need to use dateTimePicker.Value.ToString() instead of dateTimePicker.Text.ToString()

A simple example of using this can be dateTimePicker.Value.ToString("yyyy-MM-dd")

Sami
  • 8,168
  • 9
  • 66
  • 99