Click here to Skip to main content
16,008,750 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
private void toolStripButton3_Click_1(object sender, EventArgs e)
{
    int damage = Convert.ToInt16(numericUpDown1.Value);
    UGIcon.Open();
    cmd = new SqlCommand("select * from purchase", UGIcon);
    SqlDataReader s = cmd.ExecuteReader();
    while (s.Read())
    {
        if (toolStripTextBox1.Text != s["cm"].ToString())
        {
            MessageBox.Show("Please check the name");
        }
        else
        {
            if (textBox2.Text == "")
            {
                errorProvider1.SetError(textBox2, "Provide Owner name");
            }
            else if (textBox3.Text == "")
            {
                errorProvider1.SetError(textBox3, "Provide Address");
            }
            else if (numericUpDown2.Value >= damage)
            {
                MessageBox.Show("check Damage quantity");
            }
            else if ((maskedTextBox1.Text.Trim().Length <= 10))
            {
                MessageBox.Show("Provide full phone number");
            }
            else
            {

                cmd = new SqlCommand("update purchase set om='" + textBox2.Text + "', address='" + textBox3.Text + "', phone='" + maskedTextBox1.Text.ToString() + "',baled='" + numericUpDown2.Value + "',paid='" + textBox9.Text + "' where cm='" + toolStripTextBox1.Text + "'", UGIcon);
                SqlDataReader dr;
                dr = cmd.ExecuteReader();//There is already an open DataReader associated with this Command which must be closed first. 
                dr.Close();
                MessageBox.Show("Details has been updated sucessfully", "Update Window", MessageBoxButtons.OK, MessageBoxIcon.Information);


            }
        }

    }

    s.Close();
    UGIcon.Close();
}
Posted
Updated 16-Jan-13 17:21pm
v4

You can very well do this,
In your connection string just add "MultipleActiveResultSets=True;"


Hope it helps.
 
Share this answer
 
Comments
selva_1990 16-Jan-13 23:48pm    
ya its working thank you very much for your help :)
else
{
UGIcon.Open();
cmd = new SqlCommand("update purchase set om='" + textBox2.Text + "', address='" + textBox3.Text + "', phone='" + maskedTextBox1.Text.ToString() + "',baled='" + numericUpDown2.Value + "',paid='" + textBox9.Text + "' where cm='" + toolStripTextBox1.Text + "'", UGIcon);
SqlDataReader dr;
dr = cmd.ExecuteReader();
dr.Close();
MessageBox.Show("Details has been updated sucessfully", "Update Window", MessageBoxButtons.OK, MessageBoxIcon.Information);
UGIcon.Close();
}
Here no need to open the connection, because currenlty connection is in open mode . if you want to open the connection , first you close the connection in if condition.
 
Share this answer
 
Comments
selva_1990 16-Jan-13 23:17pm    
i removed connections in else satement.. now i m getting error as "There is already an open DataReader associated with this Command which must be closed first".
josh-jw 16-Jan-13 23:33pm    
cmd = new SqlCommand("select * from purchase", UGIcon);
SqlDataReader s = cmd.ExecuteReader();
while (s.Read())

HERE you have one connection and its not closed , but in else statement you are going to open a sqldatareader connection.means two sql datareader connection is open at that time.
selva_1990 16-Jan-13 23:45pm    
can u edit the solution and write where i have to close.... i'm not clear
selva_1990 16-Jan-13 23:50pm    
i mean where and all i have to close just you itself edit and post... i m not good with programming.. sorry for disturbing

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