plz help me in .net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeronjaison
    New Member
    • Mar 2008
    • 2

    plz help me in .net

    can any one help me
    im new to dot net my program is


    using System;
    using System.Data;
    using System.Configur ation;
    using System.Web;
    using System.Web.Secu rity;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.W ebControls.WebP arts;
    using System.Web.UI.H tmlControls;
    using System.Data.Sql Client;

    public partial class _Default : System.Web.UI.P age
    {

    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;




    protected void Page_Load(objec t sender, EventArgs e)
    {


    SqlConnection con = new SqlConnection(@ "Data Source=.\SQLEXP RESS;AttachDbFi lename=|DataDir ectory|\project .mdf;Integrated Security=True;U ser Instance=True") ;
    con.Open();

    SqlCommand cmd = new SqlCommand ("select * from CountryMaster", con);
    da = new SqlDataAdapter( cmd);
    ds = new DataSet();
    da.Fill(ds,"Cou ntryMaster");
    GridView1.DataS ource = ds.Tables["CountryMas ter"];
    GridView1.DataB ind();


    }


    protected void Button1_Click(o bject sender, EventArgs e)
    {
    SqlCommand cmd = new SqlCommand("ins ert into CountryMaster values('"+TextB ox1.Text+"')",c on);
    cmd.ExecuteNonQ uery();

    }


    }

    and i am having the error can u solve it


    Server Error in '/project' Application.
    --------------------------------------------------------------------------------

    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.InvalidO perationExcepti on: ExecuteNonQuery : Connection property has not been initialized.

    Source Error:


    Line 40: {
    Line 41: SqlCommand cmd = new SqlCommand("ins ert into CountryMaster values('"+TextB ox1.Text+"')",c on);
    Line 42: cmd.ExecuteNonQ uery();
    Line 43:
    Line 44: }


    Source File: c:\Documents and Settings\Admini strator\My Documents\Visua l Studio 2005\WebSites\p roject\Default. aspx.cs Line: 42

    Stack Trace:


    [InvalidOperatio nException: ExecuteNonQuery : Connection property has not been initialized.]
    System.Data.Sql Client.SqlComma nd.ValidateComm and(String method, Boolean async) +80
    System.Data.Sql Client.SqlComma nd.InternalExec uteNonQuery(DbA syncResult result, String methodName, Boolean sendToPipe) +133
    System.Data.Sql Client.SqlComma nd.ExecuteNonQu ery() +149
    _Default.Button 1_Click(Object sender, EventArgs e) in c:\Documents and Settings\Admini strator\My Documents\Visua l Studio 2005\WebSites\p roject\Default. aspx.cs:42
    System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e) +96
    System.Web.UI.W ebControls.Butt on.RaisePostBac kEvent(String eventArgument) +116
    System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring eventArgument) +31
    System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler sourceControl, String eventArgument) +32
    System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData) +72
    System.Web.UI.P age.ProcessRequ estMain(Boolean includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint) +3839




    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:2.0.507 27.832; ASP.NET Version:2.0.507 27.832
  • sweatha
    New Member
    • Mar 2008
    • 44

    #2
    Originally posted by jeronjaison
    can any one help me
    im new to dot net my program is


    using System;
    using System.Data;
    using System.Configur ation;
    using System.Web;
    using System.Web.Secu rity;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.W ebControls.WebP arts;
    using System.Web.UI.H tmlControls;
    using System.Data.Sql Client;

    public partial class _Default : System.Web.UI.P age
    {

    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;




    protected void Page_Load(objec t sender, EventArgs e)
    {


    SqlConnection con = new SqlConnection(@ "Data Source=.\SQLEXP RESS;AttachDbFi lename=|DataDir ectory|\project .mdf;Integrated Security=True;U ser Instance=True") ;
    con.Open();

    SqlCommand cmd = new SqlCommand ("select * from CountryMaster", con);
    da = new SqlDataAdapter( cmd);
    ds = new DataSet();
    da.Fill(ds,"Cou ntryMaster");
    GridView1.DataS ource = ds.Tables["CountryMas ter"];
    GridView1.DataB ind();


    }


    protected void Button1_Click(o bject sender, EventArgs e)
    {
    SqlCommand cmd = new SqlCommand("ins ert into CountryMaster values('"+TextB ox1.Text+"')",c on);
    cmd.ExecuteNonQ uery();

    }


    }

    and i am having the error can u solve it


    Server Error in '/project' Application.
    --------------------------------------------------------------------------------

    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.InvalidO perationExcepti on: ExecuteNonQuery : Connection property has not been initialized.

    Source Error:


    Line 40: {
    Line 41: SqlCommand cmd = new SqlCommand("ins ert into CountryMaster values('"+TextB ox1.Text+"')",c on);
    Line 42: cmd.ExecuteNonQ uery();
    Line 43:
    Line 44: }


    Source File: c:\Documents and Settings\Admini strator\My Documents\Visua l Studio 2005\WebSites\p roject\Default. aspx.cs Line: 42

    Stack Trace:


    [InvalidOperatio nException: ExecuteNonQuery : Connection property has not been initialized.]
    System.Data.Sql Client.SqlComma nd.ValidateComm and(String method, Boolean async) +80
    System.Data.Sql Client.SqlComma nd.InternalExec uteNonQuery(DbA syncResult result, String methodName, Boolean sendToPipe) +133
    System.Data.Sql Client.SqlComma nd.ExecuteNonQu ery() +149
    _Default.Button 1_Click(Object sender, EventArgs e) in c:\Documents and Settings\Admini strator\My Documents\Visua l Studio 2005\WebSites\p roject\Default. aspx.cs:42
    System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e) +96
    System.Web.UI.W ebControls.Butt on.RaisePostBac kEvent(String eventArgument) +116
    System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring eventArgument) +31
    System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler sourceControl, String eventArgument) +32
    System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData) +72
    System.Web.UI.P age.ProcessRequ estMain(Boolean includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint) +3839




    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:2.0.507 27.832; ASP.NET Version:2.0.507 27.832



    I guess problem is on ur connection coding. It is much better to use the connection coding in the following way

    SqlConnection con = new SqlConnection(" user id=sa;password= ;data source=data source name;initial catalog=databas e name;server=ser ver name");

    Here it is important for us to include only following namespaces

    using System.Data;
    using System.Data.Sql Client;

    -suganya

    Comment

    • saran23
      New Member
      • Mar 2008
      • 28

      #3
      Originally posted by jeronjaison
      can any one help me
      im new to dot net my program is


      using System;
      using System.Data;
      using System.Configur ation;
      using System.Web;
      using System.Web.Secu rity;
      using System.Web.UI;
      using System.Web.UI.W ebControls;
      using System.Web.UI.W ebControls.WebP arts;
      using System.Web.UI.H tmlControls;
      using System.Data.Sql Client;

      public partial class _Default : System.Web.UI.P age
      {

      SqlConnection con;
      SqlCommand cmd;
      SqlDataAdapter da;
      DataSet ds;




      protected void Page_Load(objec t sender, EventArgs e)
      {


      SqlConnection con = new SqlConnection(@ "Data Source=.\SQLEXP RESS;AttachDbFi lename=|DataDir ectory|\project .mdf;Integrated Security=True;U ser Instance=True") ;
      con.Open();

      SqlCommand cmd = new SqlCommand ("select * from CountryMaster", con);
      da = new SqlDataAdapter( cmd);
      ds = new DataSet();
      da.Fill(ds,"Cou ntryMaster");
      GridView1.DataS ource = ds.Tables["CountryMas ter"];
      GridView1.DataB ind();


      }


      protected void Button1_Click(o bject sender, EventArgs e)
      {
      SqlCommand cmd = new SqlCommand("ins ert into CountryMaster values('"+TextB ox1.Text+"')",c on);
      cmd.ExecuteNonQ uery();

      }


      }

      and i am having the error can u solve it


      Server Error in '/project' Application.
      --------------------------------------------------------------------------------

      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.InvalidO perationExcepti on: ExecuteNonQuery : Connection property has not been initialized.

      Source Error:


      Line 40: {
      Line 41: SqlCommand cmd = new SqlCommand("ins ert into CountryMaster values('"+TextB ox1.Text+"')",c on);
      Line 42: cmd.ExecuteNonQ uery();
      Line 43:
      Line 44: }


      Source File: c:\Documents and Settings\Admini strator\My Documents\Visua l Studio 2005\WebSites\p roject\Default. aspx.cs Line: 42

      Stack Trace:


      [InvalidOperatio nException: ExecuteNonQuery : Connection property has not been initialized.]
      System.Data.Sql Client.SqlComma nd.ValidateComm and(String method, Boolean async) +80
      System.Data.Sql Client.SqlComma nd.InternalExec uteNonQuery(DbA syncResult result, String methodName, Boolean sendToPipe) +133
      System.Data.Sql Client.SqlComma nd.ExecuteNonQu ery() +149
      _Default.Button 1_Click(Object sender, EventArgs e) in c:\Documents and Settings\Admini strator\My Documents\Visua l Studio 2005\WebSites\p roject\Default. aspx.cs:42
      System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e) +96
      System.Web.UI.W ebControls.Butt on.RaisePostBac kEvent(String eventArgument) +116
      System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring eventArgument) +31
      System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler sourceControl, String eventArgument) +32
      System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData) +72
      System.Web.UI.P age.ProcessRequ estMain(Boolean includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint) +3839




      --------------------------------------------------------------------------------
      Version Information: Microsoft .NET Framework Version:2.0.507 27.832; ASP.NET Version:2.0.507 27.832
      Just give conn.open() before the ExecuteNonQuery statement and in the very next line close the connection by giving conn.Close()

      In the page load too u have never closed the connection. a connection must closed immediately after the command is executed. Or else the connection will cause network congestion.

      One main thing,
      U have specified the connection in page load, it is Protected function and u cant use the same connection string in another function or event.(Button Click here)

      U have to specify the connection globally where u have created a connection object

      the code goes like this

      SqlConnection conn=new Sqlconnection(U r connection....) ;

      this must be the very first line(global)

      Thanks
      Saravanan

      Comment

      • kunal pawar
        Contributor
        • Oct 2007
        • 297

        #4
        Hi, Error is expected, coz u have to initializes connection object before used command object.
        As u, initialize connection object on page load event its scope is limited for that block. so it gives an error. thats it
        Please
        see below code

        protected void Button1_Click(o bject sender, EventArgs e)
        {
        SqlConnection con = new SqlConnection() ;
        con.connections tring="u r connection string";
        con.open();
        SqlCommand cmd = new SqlCommand("ins ert into CountryMaster values('"+TextB ox1.Text+"')",c on);
        cmd.ExecuteNonQ uery();

        }


        }

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Somehow you have managed to sneak through the compiler, TWO SqlConnections with the same variable name.
          Where you have this:
          Code:
          SqlConnection con;
          SqlCommand cmd;
          SqlDataAdapter da;
          DataSet ds;
          
          protected void Page_Load(object sender, EventArgs e)
          {
          SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector
          Should be:
          Code:
          SqlConnection con;
          SqlCommand cmd;
          SqlDataAdapter da;
          DataSet ds;
          
          protected void Page_Load(object sender, EventArgs e)
          {
          con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector
          But, also your connection string could be wrong, make sure you check at:
          404 - Page Not Found. Shown when a URL cannot be mapped to any kind of resource.

          Comment

          Working...