object reference not set to an instance of an object

Last post 05-08-2008 8:47 AM by tejp. 2 replies.

Sort Posts:

  • object reference not set to an instance of an object

    05-08-2008, 4:14 AM
    • Loading...
    • tejp
    • Joined on 11-26-2006, 4:18 PM
    • Posts 41

    Hi 

     I have create a class with 2 methods. method 1 is used to create and open the mysql connection.

    Method 2 is the query method where a query is passed in from my aspx page.

    But when i send the query through i get the error as shown in the subject line.

    here is the code for the query

     

     MySqlCommand mysqlcmd = new MySqlCommand(strQuery, conSQL);
     MySqlDataReader mysqlReader = mysqlcmd.ExecuteReader();

     

    code from the aspx page is

    protected void but_connclass(object sender, EventArgs e)
        {
            conClass conCL = new conClass(); //instance of the class

            if (conCL.DBConnect() == true)
            {
                string strQry = "Select * FROM tblclients";
                if (conCL.DBquery(strQry) == true) --> errors occurs here!!!!!
                {
                    lblconclass.Text = "hanji";
                }
            }

     

    i think the problem is  MySqlCommand mysqlcmd = new MySqlCommand(strQuery, conSQL); where the conSQL is defined in a different method

    method dbconnect 

    MySqlConnection conSQL = new MySqlConnection(strConnection);

    conSQL.ConnectionString = strConnection;

     so how can i make that variable in scope with the second method as there are 2 seperate calls to the each method.

    regards

     

    Tj
     

     

  • Re: object reference not set to an instance of an object

    05-08-2008, 6:54 AM
    Answer
    • Loading...
    • Gery128
    • Joined on 02-03-2008, 1:18 AM
    • Pune, India
    • Posts 44

    just wrap your code with below format

    using ("create object here" like connection cn = new connection())

    {

     //database/query  code goes here.....

     

    this will define the scope of variable defined within

    Regards,
    Girish Advani
    ---------------------------------------------------------------------------
    Remember to click “Mark as Answer” on the post that helps you.
    ---------------------------------------------------------------------------
  • Re: object reference not set to an instance of an object

    05-08-2008, 8:47 AM
    • Loading...
    • tejp
    • Joined on 11-26-2006, 4:18 PM
    • Posts 41

     hi

    would the using be added at the top of the page?

    or would you need to do using between the 2 methods in the .cs file?

     

Page 1 of 1 (3 items)