Harvint Raj
Harvint Raj

Reputation: 69

I would like to Select data in between two dates from from my server. C#

I'm trying to retrieve data in between two dates which the user will select from the dropdown lists.

I have tried something: Here's my code.

string dateFrom = DropDownList1.Text.ToString();
            string dateTo = DropDownList2.Text.ToString();

            string myconstring = ConfigurationManager.ConnectionStrings["KanbanConnectionString"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(myconstring))
            {
                DataTable dat = new DataTable();
                using (SqlCommand cmd = new SqlCommand("SELECT * FROM city.xls WHERE Date BETWEEN @dateFrom AND  @dateTo"))
                {
                    cmd.Parameters.AddWithValue("@dateFrom", dateFrom);
                    cmd.Parameters.AddWithValue("@dateTo", dateTo);
                    conn.Open();
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    da.Fill(dat);
                    D2.DataSource = dat;
                    D2.DataBind();
                }
            }

I am supposed to get the data in a datatable which is D2. However I get an error like this:

Fill: SelectCommand.Connection property has not been initialized.

Upvotes: 0

Views: 67

Answers (3)

Harvint Raj
Harvint Raj

Reputation: 69

This Worked.

DateTime dateFrom = Convert.ToDateTime(DropDownList1.Text);
            DateTime dateTo = Convert.ToDateTime(DropDownList2.Text);

            string myconstring = ConfigurationManager.ConnectionStrings["KanbanConnectionString"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(myconstring))
            {
                DataTable dat = new DataTable();
                string find = "SELECT * FROM city WHERE Date BETWEEN @dateFrom AND  @dateTo";
                using (SqlCommand cmd = new SqlCommand(find, conn))
                {
                    cmd.Parameters.AddWithValue("@dateFrom", dateFrom);
                    cmd.Parameters.AddWithValue("@dateTo", dateTo);
                    conn.Open();
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    da.Fill(dat);
                    D2.DataSource = dat;
                    D2.DataBind();
                }
            }

Upvotes: 0

Atul Mathew
Atul Mathew

Reputation: 455

add the connection initialization

cmd.connection=conn;

then you are good to go :)

Upvotes: 3

Programnik
Programnik

Reputation: 1555

conn.Open();
    using (SqlCommand cmd = conn.CreateCommand("SELECT * FROM city.xls WHERE Date BETWEEN @dateFrom AND  @dateTo"))
                    {
                        cmd.Parameters.AddWithValue("@dateFrom", dateFrom);
                        cmd.Parameters.AddWithValue("@dateTo", dateTo);                           
                        SqlDataAdapter da = new SqlDataAdapter();
                        da.SelectCommand = cmd;
                        da.Fill(dat);
                        D2.DataSource = dat;
                        D2.DataBind();
                    }

Upvotes: 0

Related Questions