ExecuteNonQuery: Connection property has not been initialized. Description: An unhandled exception [Resolved]

Posted by Kishore22 under ASP.NET on 9/20/2013 | Points: 10 | Views : 2890 | Status : [Member] | Replies : 13
ExecuteNonQuery: Connection property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: ExecuteNonQuery: Connection property has not been initialized.

Source Error:


Line 141: con.Open();
Line 142: SqlCommand cmd1 = new SqlCommand("delete from tab where id="+TID.Text);
Line 143: cmd1.ExecuteNonQuery();
Line 144: connect();
Line 145: con.Close();




Responses

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Declare SqlConnection object reference globally...
SqlConnection con;

public partial class _Default : System.Web.UI.Page
{

con = new SqlConnection("user id=PMS;password=pms@123;Data Source=ad-server;DataBase=PMS");
SqlCommand cmd1 = new SqlCommand("delete from tab where id="+TID.Text, con);
.
.
.
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link
http://stackoverflow.com/questions/15253286/executenonquery-connection-property-has-not-been-initialized
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.connection.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Show us the how you initialized con object?

Refer this link
http://www.dreamincode.net/forums/topic/296414-executenonquery-connection-property-has-not-been-initializederror/

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kishore22 on: 9/20/2013 [Member] Starter | Points: 25

Up
0
Down
public void connect()
{



SqlDataAdapter da1 = new SqlDataAdapter("select * from tab", con);

System.Data.DataSet ds1 = new System.Data.DataSet();
da1.Fill(ds1);
GridView1.DataSource = ds1.Tables[0].DefaultView;
GridView1.DataBind();
}


Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Where you initialized con object?

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kishore22 on: 9/20/2013 [Member] Starter | Points: 25

Up
0
Down
public partial class _Default : System.Web.UI.Page
{

SqlConnection con = new SqlConnection("user id=PMS;password=pms@123;Data Source=ad-server;DataBase=PMS");

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Try the following one
SqlCommand cmd1 = new SqlCommand("delete from tab where id="+TID.Text, con);



Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kishore22 on: 9/20/2013 [Member] Starter | Points: 25

Up
0
Down
yes it is working thank you somuch

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kishore22 on: 9/20/2013 [Member] Starter | Points: 25

Up
0
Down
i have one more query

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Mark replies as answer before Posted on: 9/20/2013 7:45:47 AM

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kishore22 on: 9/20/2013 [Member] Starter | Points: 25

Up
0
Down
how to bind my data to asp.net controls please give me a code please

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{

GridView1.EditIndex = -1;
connect();
}
protected void GridView1_RowUpdating2(object sender, GridViewUpdateEventArgs e)

{

TextBox id = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TID");

TextBox FirstName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");

TextBox Lastname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");

RadioButtonList Gender = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("RadioButtonList1");

DropDownList Qual = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1");

TextBox curemp1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");

TextBox caddress1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");

DropDownList ccity1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("drp1");

TextBox paddress1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");

DropDownList pcity1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2");


SqlCommand cmd1 = new SqlCommand();

cmd1.Connection = con;

cmd1.CommandText = "UPDATE tab SET firstname ='" + TextBox1.Text + "',lastname ='" + TextBox2.Text + "',gender ='" + RadioButtonList1.SelectedValue + "',qual ='" + DropDownList1.Text + "', curemp='" + TextBox3.Text + "',caddress ='" + TextBox4.Text + "',ccity ='" + drp1.Text + "',paddress ='" + TextBox5.Text + "',tpcity ='" + DropDownList2.Text + "', WHERE id='" + TID.Text + "'";


cmd1.ExecuteNonQuery();

GridView1.EditIndex = -1;

connect();
con.Close();
}

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link
http://www.codeproject.com/Articles/37207/Editable-Gridview-with-Textbox-CheckBox-Radio-Butt

What is the problem here?

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
TextBox id = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TID"); 


TextBox FirstName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");

TextBox Lastname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");

RadioButtonList Gender = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("RadioButtonList1");

DropDownList Qual = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1");

TextBox curemp1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");

TextBox caddress1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");

DropDownList ccity1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("drp1");

TextBox paddress1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");

DropDownList pcity1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2");


SqlCommand cmd1 = new SqlCommand();

cmd1.Connection = con;

cmd1.CommandText =
"UPDATE tab SET firstname ='" + FirstName.Text + "',lastname ='" + Lastname.Text + "',gender ='" + Gender.SelectedValue + "',qual ='" + Qual.SelectedValue.ToString()+ "', curemp='" + curemp1.Text + "',caddress ='" + caddress1.Text + "',ccity ='" + ccity1.SelectedValue.ToString() + "',paddress ='" + paddress1.Text + "',tpcity ='" + pcity1.SelectedValue.ToString()+ "', WHERE id='" + id.Text + "'";


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response