DateTime Sql Query problem

I always get error from this piece of code that saying:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll

Additional information: System error.

CODE:

public DataTable load_job(DateTime date)
{
SqlConnection connection = create_Conn("True","(local)","pubs");

string selectString = "SELECT * FROM job WHERE deadline = @dLine";

SqlCommand command = new SqlCommand(selectString,connection);
command.Parameters.Add(new SqlParameter("@dLine",SqlDbType.DateTime));
command.Parameters["@dLine"].Value = date;

SqlDataAdapter adapter = new SqlDataAdapter(selectString,connection);
DataTable datatab = new DataTable();

connection.Open();
adapter.Fill(datatab);
connection.Close();

return datatab;
}

could anyone help me with this...

thanks in advance



Answer this question

DateTime Sql Query problem

  • Pricerage

    Thanks man you are the best,

    now the code is working, you are truly the best one around


  • per9elsen

    I think you are using the wrong constructor for your SqlDataAdapter resulting in that you are not using the SqlCommand object.

    SqlDataAdapter adapter = new SqlDataAdapter(selectString,connection);

    change to

    SqlDataAdapter adapter = new SqlDataAdapter(command);



  • Darkwings

    Hi Fendy!

    Either do what Andreas has suggested or do the following as I've removed Command object altogether.

    public DataTable load_job(DateTime date)
    {
    SqlConnection connection = create_Conn("True","(local)","pubs");

    string selectString = "SELECT * FROM job WHERE deadline = @dLine";

    SqlDataAdapter adapter = new SqlDataAdapter(selectString,connection);

    adapter.SelectCommand.Parameters.Add(new SqlParameter("@dLine",SqlDbType.DateTime));

    adapter.SelectCommand.Parameters["@dLine"].Value = date;


    DataTable datatab = new DataTable();

    connection.Open();
    adapter.Fill(datatab);
    connection.Close();

    return datatab;
    }



  • Thuy MSFT

    Hey Sohail Iqbal,

    thanks also man, appreciate it !


  • DateTime Sql Query problem