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.

2
  • That means your [filesTA].ChkDate doesn't match the date you selected. Commented Nov 13, 2012 at 7:46
  • 2
    FYI, why don't you use the SQLParameter? Easy to use and prevent any SQL injection. Here you can see a little example Commented Nov 13, 2012 at 7:47

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

thank so much! it's work. :D ='{0}-{1}-{2}'" << does it's mean??
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")

1 Comment

thanks so much. i try dateTimePicker.Value.ToString("yyyy-MM-dd") it's workd. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.