SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Object reference not set to an instance of an object

    Hi there, I hope your help.

    I try this query in asp net, but receive this alert, can you help me?
    thanks in advance.

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

    Object reference not set to an instance of an object.
    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.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:


    Line 618:
    Line 619: DataSet myDataSet = new DataSet();
    Line 620: foreach (DataRow row in myDataSet.Tables[strQuery].Rows)
    Line 621: {
    Line 622: string seriesName = row["t_previous_year"].ToString();


    Source File: d:\Inetpub\wwwroot\public\Chart.aspx Line: 620
    Code:
       private void Page_Load(object sender, System.EventArgs e)
       {
           
           OdbcConnection myConnectionString = new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString);
           myConnectionString.Open();
    
    
    String strQuery = " SELECT " +                       
                      " t_previous_year AS tpy " +                         
                      ",t_current_year AS tcy " +
                      " FROM myTbl2 " +
                      " ORDER BY ID DESC; ";
    
           OdbcCommand objCmd = new OdbcCommand(strQuery, myConnectionString);
           objCmd.CommandType = CommandType.Text;
           objCmd.CommandText = strQuery;
    
    
           DataSet myDataSet = new DataSet();
           foreach (DataRow row in myDataSet.Tables[strQuery].Rows)
           {     
           string seriesName = row["t_previous_year"].ToString();
           Chart1.Series.Add(seriesName);
           Chart1.Series[seriesName].ChartType = SeriesChartType.Line;
           Chart1.Series[seriesName].BorderWidth = 2;
           } 
    
           myConnectionString.Close();
           myConnectionString.Dispose();
    
       }
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  2. #2
    SitePoint Zealot davejuk's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are using the SQL query string as the table identifier?

  3. #3
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thanks for your reply.
    I have changed method but I have this alert.
    Attach the code file aspx.


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

    Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0029: Cannot implicitly convert type 'System.Data.Odbc.OdbcConnection' to 'System.Data.SqlClient.SqlConnection'

    Source Error:



    Line 42: conn.Open();
    Line 43: cmd.CommandText = sSQL;
    Line 44: cmd.Connection = conn;
    Line 45: SqlDataReader dr;
    Line 46: dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    Attached Files Attached Files
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  4. #4
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,638
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    You are declaring a datareader of the wrong type, given you are using ODBC you'll need to use the described type. Or use the more generic IDataReader which works with all datareader implementations.

  5. #5
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok, I understand your observation, thanks a lot.

    http://msdn.microsoft.com/it-it/libr...atareader.aspx
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •