Programming problem ASP.NET c#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Reinier Beeckman

    #1

    Programming problem ASP.NET c#

    Hi got the following problem, it's more a programming problem then really a
    database problem. I have a Database class which connects to a mySQL database on
    the network. In WebForm1.aspx i connect, using the Database class, to this
    database. Works fine. In
    Webform1.aspx i have the main view (SELECT * FROM) of the database. In
    Webform2.aspx i want to see certain details. So when i click on a button in
    Webform1.aspx i go to Webform2.aspx.

    In webform2.aspx i want to use the same (open) connection. What happens now is
    that the connection is closed and made over again, this is needed because else
    the Class Database is not known in WebForm2.aspx. (The Database class is the
    class where i initiate the database)

    My question is how can i use the Database class without initiating it again in
    WebForm2.aspx ??

    This is my Database Class

    using System;
    using System.Data;
    using ByteFX.Data.MyS QLClient;
    using System.Collecti ons;
    namespace webAPP
    {

    public class Database
    {
    private MySQLConnection con;
    private string server, database, user, password;

    public Database(string server, string database, string user, string
    password)
    {
    this.server = server;
    this.database = database;
    this.user = user;
    this.password = password;
    }

    public void Connect()
    {
    string connectionStrin g = "Server="+serve r+
    ";Database="+da tabase+";User ID="+user+";Pas sword="+passwor d+";";
    try
    {
    con = new MySQLConnection (connectionStri ng);
    con.Open();
    if(con!=null)
    Console.WriteLi ne("succes");
    }
    catch(Exception e)
    {
    throw new Exception(e.Mes sage);
    }
    }

    public void Close()
    {
    if(con!=null) con.Close();
    }

    public string[] getValue(string value)
    {
    String[] arrValues = null;
    MySQLDataAdapte r adapter = new MySQLDataAdapte r("SELECT * from pc where
    testnr =" + value, con);
    DataSet ds = new DataSet();
    adapter.Fill(ds );
    DataTable table = ds.Tables[0];
    ArrayList kolomWaarden = new ArrayList();
    foreach (DataRow row in table.Rows)
    {
    arrValues = new String[table.Columns.C ount];
    for (int i=0; i<table.Columns .Count; i++)
    arrValues[i] = Convert.ToStrin g(row[i]);
    }
    return arrValues;
    }

    }
    }


    In WebForm1.aspx i have this code to connect to the database:

    private void openGb()
    {
    try
    {
    if(gb != null)
    gb.Close();
    gb = new Database("serve rname", "databasena am", "username", "password") ;
    //initiate database class
    gb.Connect();
    }
    catch(Exception e)
    {
    Console.WriteLi ne(e);
    }
    }

    In WebForm2.aspx i declare the Database Class by saying

    Database gb;

    In the page_load i then want to run the SQL-Query in gb.getValue( ) But i then
    get a Object reference not found error because the Database Class is not
    instantiated in WebForm2.aspx. That is like: gb = new Database("serve rname",
    "databasena am", "username", "password") ;

    Is there a way to prevent this ??


  • Selvin

    #2
    Re: Programming problem ASP.NET c#

    <ciach>

    Use Session objects

    pozdrawiam

    Przemek Sulikowski


    Comment

    • Selvin

      #3
      Re: Programming problem ASP.NET c#

      <ciach>

      Use Session objects

      pozdrawiam

      Przemek Sulikowski


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Programming problem ASP.NET c#

        It should also be said that performing database operations in this way
        is a very bad idea. Generally speaking, when you want to connect to a
        database, you should open the connection, use it, and then close it. If you
        are changing contexts like this (i.e. moving from page to page), you
        definitely should close the connection. If something goes wrong during the
        transition, then the open connection is left hanging, which is definitely
        another downside.

        However, this doesn't mean that you can't store the result of the query
        (a DataSet) somewhere (like the session).

        Hope this helps.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - nick(dot)paldin o=at=exisconsul ting<dot>com

        "Selvin" <selvin_nospam@ nospam_osadkows ki.com.pl> wrote in message
        news:bku1et$lb2 $1@news.onet.pl ...[color=blue]
        > <ciach>
        >
        > Use Session objects
        >
        > pozdrawiam
        >
        > Przemek Sulikowski
        >
        >[/color]


        Comment

        • Ignacio Machin

          #5
          Re: Programming problem ASP.NET c#

          Hi Reinier,

          You can do several things, one of then is keep the instance of DataBase
          class in a session variable , this is not a very good solution as you will
          have several open connection opened at the same time ( one per session )
          this greatly affect the performance and scalability of your solution.
          another solution would be make the DataBase class a singleton, doing this
          will assure you that only one instance of the class exist in your
          application so all the request will use the same connection to the DB.

          Any of the above forms solve your problem of reuse the same instance in
          webform2.aspx page .

          Now to finish, I would suggest you to take a look at the connection
          pooling capabilities of ADo.NET and the SQLServer provider:
          http://msdn.microsoft.com/library/de...taprovider.asp

          there you will see that you don;t have to worry for this, as the provider
          provide a pooling feature that handle this situation for you.
          Remember you should ALWAYS close the connection AS EARLY AS POSSIBLE.

          Hope this help,

          --
          Ignacio Machin,
          ignacio.machin AT dot.state.fl.us
          Florida Department Of Transportation

          "Reinier Beeckman" <rjlbeeckman@ho tmail.com> wrote in message
          news:ucGAjsygDH A.2292@TK2MSFTN GP10.phx.gbl...[color=blue]
          > Hi got the following problem, it's more a programming problem then really[/color]
          a[color=blue]
          > database problem. I have a Database class which connects to a mySQL[/color]
          database on[color=blue]
          > the network. In WebForm1.aspx i connect, using the Database class, to this
          > database. Works fine. In
          > Webform1.aspx i have the main view (SELECT * FROM) of the database. In
          > Webform2.aspx i want to see certain details. So when i click on a button[/color]
          in[color=blue]
          > Webform1.aspx i go to Webform2.aspx.
          >
          > In webform2.aspx i want to use the same (open) connection. What happens[/color]
          now is[color=blue]
          > that the connection is closed and made over again, this is needed because[/color]
          else[color=blue]
          > the Class Database is not known in WebForm2.aspx. (The Database class is[/color]
          the[color=blue]
          > class where i initiate the database)
          >
          > My question is how can i use the Database class without initiating it[/color]
          again in[color=blue]
          > WebForm2.aspx ??
          >
          > This is my Database Class
          >
          > using System;
          > using System.Data;
          > using ByteFX.Data.MyS QLClient;
          > using System.Collecti ons;
          > namespace webAPP
          > {
          >
          > public class Database
          > {
          > private MySQLConnection con;
          > private string server, database, user, password;
          >
          > public Database(string server, string database, string user, string
          > password)
          > {
          > this.server = server;
          > this.database = database;
          > this.user = user;
          > this.password = password;
          > }
          >
          > public void Connect()
          > {
          > string connectionStrin g = "Server="+serve r+
          > ";Database="+da tabase+";User ID="+user+";Pas sword="+passwor d+";";
          > try
          > {
          > con = new MySQLConnection (connectionStri ng);
          > con.Open();
          > if(con!=null)
          > Console.WriteLi ne("succes");
          > }
          > catch(Exception e)
          > {
          > throw new Exception(e.Mes sage);
          > }
          > }
          >
          > public void Close()
          > {
          > if(con!=null) con.Close();
          > }
          >
          > public string[] getValue(string value)
          > {
          > String[] arrValues = null;
          > MySQLDataAdapte r adapter = new MySQLDataAdapte r("SELECT * from pc[/color]
          where[color=blue]
          > testnr =" + value, con);
          > DataSet ds = new DataSet();
          > adapter.Fill(ds );
          > DataTable table = ds.Tables[0];
          > ArrayList kolomWaarden = new ArrayList();
          > foreach (DataRow row in table.Rows)
          > {
          > arrValues = new String[table.Columns.C ount];
          > for (int i=0; i<table.Columns .Count; i++)
          > arrValues[i] = Convert.ToStrin g(row[i]);
          > }
          > return arrValues;
          > }
          >
          > }
          > }
          >
          >
          > In WebForm1.aspx i have this code to connect to the database:
          >
          > private void openGb()
          > {
          > try
          > {
          > if(gb != null)
          > gb.Close();
          > gb = new Database("serve rname", "databasena am", "username",[/color]
          "password") ;[color=blue]
          > //initiate database class
          > gb.Connect();
          > }
          > catch(Exception e)
          > {
          > Console.WriteLi ne(e);
          > }
          > }
          >
          > In WebForm2.aspx i declare the Database Class by saying
          >
          > Database gb;
          >
          > In the page_load i then want to run the SQL-Query in gb.getValue( ) But i[/color]
          then[color=blue]
          > get a Object reference not found error because the Database Class is not
          > instantiated in WebForm2.aspx. That is like: gb = new[/color]
          Database("serve rname",[color=blue]
          > "databasena am", "username", "password") ;
          >
          > Is there a way to prevent this ??
          >
          >[/color]


          Comment

          Working...