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 ??


Working...