Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Following is the code of my project.
Here I want to show data on page, on button click.,so I used data reader,but still error is coming like.
ERROR...

Additional information: There is already an open DataReader associated with this Command which must be closed first.

And also one more problem is that, I am fail to assign date value to datetime picker control.my database field type is " datetime".

Please help me & send me correct code.

C#
private void button7_Click(object sender, EventArgs e)
{

    SqlCommand cmd = new SqlCommand();

//if (cn.State ==ConnectionState .Closed )
{
    //cn.Open();

//SqlDataReader dr;
    //dr  = new SqlDataReader();

cmd.CommandText = "select * from clubAC where accountno = " + txtacno .Text + "";
    cmd .Connection =cn ;

SqlDataReader  dr = cmd.ExecuteReader();
if (dr .Read ())
    {

        txtname.Text = dr["name"].ToString();
        txtnomadd.Text = dr["address"].ToString();
        txtage.Text = dr["age"].ToString();
        txtsex.Text = dr["sex"].ToString();
        dateTimePicker1.Value = Convert.ToDateTime.dr["dateofcommencement"].ToShortDateString;
        //dateTimePicker2.Value = dr["duedate"].ToString();
        txtterm.Text = dr["term"].ToString();
        //dateTimePicker3.Value = dr["lastpaymentdate"].ToString();
        txtbkamt.Text = dr["bookingamount"].ToString();
        txtamtpay .Text  = dr["fullpayableamount"].ToString();
        //dateTimePicker4.Value = dr["dateoptrfdpay"].ToString();
        txtassocode.Text = dr["associatecode"].ToString();
        txtmodepay.Text = dr["modeofpaymengt"].ToString();

        txtoptrfdpay.Text = dr["optionalrefundpay"].ToString();
        txtassoname.Text = dr["associatename"].ToString();
        txtnomage.Text = dr["nomineeage"].ToString();
        txtnomadd.Text = dr["nomineeaddress"].ToString();
        txtnomname.Text = dr["nomineename"].ToString();
        txtnomrel.Text = dr["relation"].ToString();
        cmd.Dispose();
        dr.Close();
     }
Posted
Updated 28-May-11 20:54pm
v3

1 solution

To fix your first problem, reverse the order of these two lines:
cmd.Dispose();
dr.Close();

Becomes:
dr.Close();
cmd.Dispose();


Secondly, don't create your strings by concatenation: it leaves you wide open to an accidental or deliberate SQL Injection attack. Use Parametrized queries instead:
cmd.CommandText = "select * from clubAC where accountno = @AN";
cmd.Parameters.AddWithValue("@AN", txtacno.Text);
cmd.Connection =cn ;

The final problem you report needs more information: what is the field type that you are storing the date in the database?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-May-11 15:29pm    
Good catch, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900