Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
namespace Practice1
{
    public partial class StdInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Binddata()
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using   (SqlConnection con=new SqlConnection(CS))
            {
                con.Open();
                SqlCommand cmd=new SqlCommand("select * from tab",con);
                cmd.ExecuteNonQuery();
                INFO.DataSource = cmd.ExecuteReader();
                INFO.DataBind();
            }
        }

I am getting error Object reference not set to an instance of an object.
on INFO.DataSource = cmd.ExecuteReader();

i am trying to call Bindadata method from other webpage
help me  friends am beginner to asp.net
Posted
Comments
Vedat Ozan Oner 29-Jun-14 11:23am    
INFO is null. therefore, when you try to access it, you get this error. ensure that you set INFO.
bhushanS123 29-Jun-14 12:55pm    
it having data

1 solution

That code is rather odd.
First off why are you calling ExecuteNonQuery on the SqlCommand and then immediately calling ExecuteReader?
Secondly, why are you trying to assign an SqlReader to a DataSource property? I can't think off the top of my head of any controls where that works...Try using a DataAdapter to fill a DataTable instead, and use the table as the DataSource instead.

But those won't fix your problem, which is probably that INFO does not contain any class instance.
Check your code, and look for an assignment with INFO on the left hand side - either there isn't one - in which case it's null and you will get the error - or your code isn't executing it under all circumstances - in which case it's null and you will still get the error!

BTW: Kudos for using using for the SqlConnection - but you should also use it for the SqlCommand as well - it also implements IDisposable.
 
Share this answer
 
Comments
bhushanS123 29-Jun-14 11:47am    
but using the same code
when i called Bindata method from same webpage
it works...
bhushanS123 29-Jun-14 12:54pm    
Sir i made changes suggested by you
public void Binddata()
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con=new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("select * from tab", con);
con.Open();
DataTable dt = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
dt.Load(dr);
INFO.DataSource = dt;
INFO.DataBind();
cmd.ExecuteNonQuery();



but the problem is when i call these method from same page it works..

n only when we called it from other webpage it gives error as
Object reference not set to an instance of an object.

help me
[no name] 29-Jun-14 14:53pm    
Help you with what? That is the what I would expect to have happen. Do you know what a "class" is? Do you know what an "instance of a class" is? Do you know that most webpages don't exist unless you are looking at them? So your "other webpage" is trying to call.... nothing.
bhushanS123 1-Jul-14 7:28am    
Sir
i calling this method using class and object though it will throw error
.and the other web pages means...other aspx except this class file
sorry if i am not explaining well

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