Click here to Skip to main content
15,889,406 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I don't want to tell about this. I am getting this error and got frustrated of this for and please help me i am new to this and I wanted to retrieve image from database to asp.net page in C#. I can able to only insert image to sql but can't able to retrieve. and on .net i am not able to understand or not given exact procedure of code so plesae help me to know what is this error and tell me what i have to do in ordr to fix this error thanks in advance.. expecting more help from all my friends in this forum thanks again and help me plz plz..

i am referring to this link http://www.aspsnippets.com/Articles/Display-images-from-SQL-Server-Database-in-ASP.Net-GridView-control.aspx

C#
System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="App_Web_tn0mwnru"
  StackTrace:
       at _Default.Page_Load(Object sender, EventArgs e) in c:\Users\kachu\Documents\Visual Studio 2008\WebSites\ImagesFromDBinGridView\CSharp.aspx.cs:line 17
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:
Posted
Updated 5-Apr-12 10:06am
v3
Comments
ZurdoDev 5-Apr-12 15:41pm    
It tells you what line of code is failing. What is that line of code?
Orcun Iyigun 5-Apr-12 16:06pm    
Improved readabilty and tags.

You need to look at your code: the trace is pretty explicit - look at the CSharp.aspx.cs file, at line 17.

Somewhere, on that line of code you are either trying to use a null value as a class instance (as in myClassVaraible.MyProperty with myClassVariable not set to a new value), or you are passing a null value into a method that doesn't expect one.

Further information we can't give!


this is my CSharp.aspx.cs file

C#
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings[@"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source=KACHU-PC\SQLEXPRESS"].ConnectionString;
        string strQuery = "select ID, Name from tblFiles order by ID";
        SqlCommand cmd = new SqlCommand(strQuery);
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            GridView2.DataSource = dt;
            GridView2.DataBind();

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            con.Close();
            sda.Dispose();
            con.Dispose();
            dt.Dispose(); 
        } 
    }
}


n on line 17 its connection string and connection string is rite as far i re re checked..


OK...that's not too difficult.
When you use System.Configuration.ConfigurationManager.ConnectionStrings you are supposed to use the name of a connection string in your web.config:
HTML
<connectionStrings>
  <add name="LoggingDatabase" connectionString="Database=SMLogging;Data Source=GRIFFPC\SQLEXPRESS;Initial Catalog=SMLogging;Integrated Security=True"/>
  <add name="MembershipDatabase" connectionString="Database=SMMembership;Data Source=GRIFFPC\SQLEXPRESS;Initial Catalog=SMMembership;Integrated Security=True"/>
  <add name="DownloadDatabase" connectionString="Data Source=GRIFFPC\SQLEXPRESS;Initial Catalog=SMInterchange;Integrated Security=True"/>
</connectionStrings>

C#
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MembershipDatabase"].ConnectionString;

You are trying to access a connection string called "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source=KACHU-PC\SQLEXPRESS" which doesn't exist. So it throws an null reference error when you access it.

Either use the name of a web.config string or use the actual string directly and it will work. The former is preferable, as it means you can make changes without altering the code.
 
Share this answer
 
v2
Comments
karthikh87 5-Apr-12 23:45pm    
this is my CSharp.aspx.cs file

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings[@"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source=KACHU-PC\SQLEXPRESS"].ConnectionString;
string strQuery = "select ID, Name from tblFiles order by ID";
SqlCommand cmd = new SqlCommand(strQuery);
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
GridView2.DataSource = dt;
GridView2.DataBind();

}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
dt.Dispose();
}
}
}

n on line 17 its connection string and connection string is rite as far i re re checked..
OriginalGriff 6-Apr-12 2:15am    
Answer updated
hello,

I saw you code and Also run on my m/c
it is successfully run.
no doubt in your code.
i have doubt in connection string.

So, Please check your connection string is proper or not by using Visual studio.


Best regards,
Anil Avhad
 
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