Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i m getting the following error in the line
"con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connect"].ToString());" of code below...

Error:
"system.nullreferenceexception object reference not set to an instance of an object."

C#
public partial class _Default : System.Web.UI.Page
{
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    SqlCommand cmd = new SqlCommand();
    SqlConnection con;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }
    public void BindData()
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connect"].ToString());//getting error in this line
        cmd = new SqlCommand("Select * from b_asset_master", con);
        cmd.Connection = con;
        da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        con.Open();
        cmd.ExecuteNonQuery();
        Grid.DataSource = ds;
        Grid.DataBind();
        con.Close();
    }
    protected void Grid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        Grid.CurrentPageIndex = e.NewPageIndex;
        BindData();
    }
}
Posted
Updated 24-Jun-13 23:56pm
v7
Comments
Sergey Alexandrovich Kryukov 25-Jun-13 2:31am    
In what line?
—SA
Prasad_Kulkarni 25-Jun-13 2:32am    
Which line exactly? Obviously some value is unset, but without knowing which one it's impossible to say why the error is occurring.
Orcun Iyigun 25-Jun-13 2:32am    
In which line you get the exception. Specify it.
dimtdj 25-Jun-13 2:33am    
da.Fill(ds);

C#
con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connect"].ToString());//getting error in this line



in this line you are using
C#
ConfigurationManager.ConnectionStrings["Connect"].ToString()


but i think "Connect" key dose not exist in your config file. or you using wrong spell there.
 
Share this answer
 
It depends which line it is on, really, but the most obvious candidate is:
C#
cmd.CommandText = "Select * from b_asset_master";

If you have not assigned a value to cmd, then you will get this error.
Try changing it to:
C#
cmd = new SqlCommand("Select * from b_asset_master", con);
and discard the line after it. The problem should go away.

When you get a problem like this, the first thing to do is look at the code in the debugger, and find the variable that is null - you can usually work out pretty easily from that what you need to do. And it's a lot, lot quicker than asking here each time! :laugh:
 
Share this answer
 
Comments
dimtdj 25-Jun-13 2:52am    
thanks for your concern but it doesn't help me
OriginalGriff 25-Jun-13 3:11am    
So what happened when you debugged it?
dimtdj 25-Jun-13 5:24am    
it is saying that "The ConnectionString property has not been initialized."
OriginalGriff 25-Jun-13 5:39am    
I think you missed something - can you post the exact code that gives the current error?
dimtdj 25-Jun-13 5:42am    
i have update the question... now it contain the whole code...
Where does that ds come from? Looks like that is null.
 
Share this answer
 
Comments
dimtdj 25-Jun-13 2:58am    
SqlDataAdapter da;
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
SqlConnection con;

from the above code... its is written in "class _Default" before page load event
Hi!
Open your connection like this.

SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = Cls_connection;
myConnection.Open();
 
Share this answer
 
Comments
Akmal Abbas Naqvi 25-Jun-13 5:59am    
Or Remove the toString() part from connectionstring line
Hope this will help

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