Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
SqlConnection con = new SqlConnection();
            con.ConnectionString = "Server=(Local); Database=final; Integrated Security=True";
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM [zarfiat] WHERE shahr=@shahr AND mantaghe=@mantaghe",con);
            cmd.Parameters.AddWithValue("shahr",textBox5.Text);
            cmd.Parameters.AddWithValue("mantaghe", textBox6.Text);
            SqlDataReader dr = null;
            dr = cmd.ExecuteReader();
            while(dr.Read())
            {
                zarfiat=Convert.ToInt32(dr["zarfiat"]);
                if (zarfiat > 0)
                {
                    if (textBox1.Text != "" & textBox2.Text != "" & textBox3.Text != "" & textBox4.Text != "" & textBox5.Text != "" & textBox6.Text != "")
                    {
                        cmd = new SqlCommand("INSERT INTO [info] (kod,name,family,tel,shahr,mantaghe) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')");
                        cmd.ExecuteNonQuery();
                    }
                    else label8.Text = "پر کردن تمامی اطلاعات الزامی می باشد";
                }
                else label8.Text = "ظرفیت خالی در  مکان مورد نظر موجود نمی باشد";
            }
            zarfiat--;
            cmd = new SqlCommand("UPDATE [final] SET zarfiat='"+zarfiat+"'");
            cmd.ExecuteNonQuery();
            label8.Text = "مشترک مورد نظر با موفقیت ثبت شد";
            con.Close()




This error appear...

C#
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
Additional information: ExecuteNonQuery: Connection property has not been initialized.
Posted
Updated 11-Feb-14 20:18pm
v2

1 solution

You have NO Connection on cmd the second time around:
cmd = new SqlCommand("INSERT INTO [info] (kod,name,family,tel,shahr,mantaghe) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')");


Should be:

cmd = new SqlCommand("INSERT INTO [info] (kod,name,family,tel,shahr,mantaghe) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')",con);


notice that con was added...

But anyhow, this will NOT Work, as you change the cmd, the while loop will throw an exception, I think.

SqlCommand cmdInsert = new SqlCommand("INSERT INTO [info] (kod,name,family,tel,shahr,mantaghe) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')",con);
cmdInsert.ExecuteNonQuery();


And Again, this is not recommended, use SqlParameter to pass the informations from all the textboxes.
 
Share this answer
 
v2

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