Click here to Skip to main content
15,917,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone, I'm having trouble populating a DataTable with a database.

public DataTable GetData()
    {
        string connectionString =
          System.Web.HttpContext.Current.Application["solarsearch_dbConnectionString"].ToString();

        SqlConnection commandString =
            new SqlConnection(connectionString);

        commandString.Open();

        string SQL = "select * from enquiry";

        SqlCommand objCommand = new SqlCommand(SQL, commandString);
        SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet DS = new DataSet();
        adapter.SelectCommand = objCommand;
        adapter.Fill(DS);

        DataTable dt = DS.Tables[0];
        return dt;
    }


When I execute the code I get the error by

string connectionString =       System.Web.HttpContext.Current.Application["solarsearch_dbConnectionString"].ToString();


saying Object reference not set to an instance of an object.
Any help please.
Posted

1 solution

The error means that "System.Web.HttpContext.Current.Application["solarsearch_dbConnectionString"]" is not valid - it is returning as a null. At a guess, "solarsearch_dbConnectionString" is not in your configuration...

BTW: I would use System.Configuration.Appsettings["tag"]..
because it is applicable inside a class for both web
applications and windows applications. At some point,
you may need to use your business logic for the web
app on the desktop.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900