Click here to Skip to main content
15,915,094 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Here My code is
PresentationLayer:
C#
using BusinessAccessLayer;
namespace _3_tiermahesh
{
    public partial class PresentationLayer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Gridview1.DataSource = Objbll.ReturnGridViewData();
        }
        ClsBLL Objbll = new ClsBLL();
        protected void btnsave_Click(object sender, EventArgs e)
        {
            try
            {
                Objbll._Name = TextBox1.Text;
                Objbll._Address = TextBox2.Text;
                Objbll._EmailID = TextBox3.Text;
                Objbll._Mobilenumber = TextBox4.Text;
                Objbll.InsertRecord();
                Console.WriteLine("Record Is Inserted Don't Worrry");
            }
            catch(Exception Ex)
            {
                Console.WriteLine(Ex);
            }
        }
    }
}


BusinessLogicLayer:
C#
using System.Data;
using DataAccessLayer;
namespace BusinessAccessLayer
{
   public class ClsBLL
    {
        string Name, Address, EmailID, Mobilenumber;
        DataRow Rec;
        ClsDAL objdal = new ClsDAL();
        public string _Name
        {
            set { Name = value; }
            get { return Name; }
        }
        public string _Address
        {
            set { Address = value; }
            get { return Address; }
        }
        public string _EmailID
        {
            set { EmailID = value; }
            get { return EmailID; }
        }
        
       public string _Mobilenumber
        {
            set { Mobilenumber = value; }
            get { return Mobilenumber; }
        }
        public void InsertRecord()
        {
            Rec = objdal.Ds.Tables[0].NewRow();
            Rec[0] = Name;
            Rec[1] = Address;
            Rec[2] = EmailID;
            Rec[3] = Mobilenumber;
            objdal.Ds.Tables[0].Rows.Add(Rec);

        }
        public object ReturnGridViewData()
        {
           return objdal.Ds.Tables[0];//Here Iam getting the Exception Message as above mentioned.
        }
    }

DataAccessLayer:
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DataAccessLayer
{
    public class ClsDAL
    {
        
        SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        SqlDataAdapter Da; 
        public DataSet Ds; 
        SqlCommandBuilder bldr;
        public void clsDalEmployeeDetails()
        {
            Da = new SqlDataAdapter("Select * from Userinfo", Con);
            Ds = new DataSet();
            Da.Fill(Ds, "Userinfo");
            Da.FillSchema(Ds, SchemaType.Source, "Userinfo");
            bldr = new SqlCommandBuilder(Da);
        }
        public void UpdateDB()
       {
        Da.Update(Ds,"Userinfo");
       }
    }
}
}

Help me Out Thankyou
and give some suggestions about the code i have written and the same format will be used in real time or not? i mean using Rec object to use inserting Records.
Posted
Updated 9-Mar-14 23:51pm
v3
Comments
Which line throws the Exception?

I think you're calling ClsBLL.ReturnGridViewData before you've called ClsDAL.clsDalEmployeeDetails() on the objdal instance.

That means that the DS variable hasn't been initialized yet.
Make sure you initialize DS before first use and you should be fine.

Hope this helps,
Fredrik
 
Share this answer
 
at which point ur getting this error??

Reasons for this error : whenever an object is not found or cannot be accessed.

ex:
C#
datatable dt = new datatable();
dt = null;
var a = dt.rows[0][0].tostring();//now ull get this error


there are lot of instances like this where ull get this error.
 
Share this answer
 
Comments
Aravindba 10-Mar-14 6:22am    
hi dont waste ur time,it they didnt mention which line error,dont try that code and reply
raxhemanth 10-Mar-14 6:45am    
i have pointed that the error is occurs in businesslogiclayer and i have written the description about that error. please have a look and try to solve thanks
AndrewCharlz 10-Mar-14 7:55am    
thanks
Member 10600476 10-Mar-14 12:55pm    
bro that is what ive said there is no table just open the ds its not filled in objdal.Ds.Tables[0];

it might be the error

public DataSet Ds;

change it to public dataset Ds {get; set;}
now try
AndrewCharlz 10-Mar-14 12:58pm    
its me only try and let me know
In ReturnGridViewData() of ClsBLL u are returning objdal.Ds.Tables[0] which is not populated as if now that is why it is throwing exception u have mentioned.To correct it return the objdal.clsDalEmployeeDetails(); and return instance of DataTable from the clsDalEmployeeDetails(); because as u are working with single table use datatable rather than dataset.

Pls refer to the link below for Dataset v/s Datatable Performance

http://social.msdn.microsoft.com/Forums/en-US/47c62835-6187-4251-8d44-f9b8dca3406b/dataset-vs-datatable-performance?forum=netfx64bit[^]
 
Share this answer
 
This error occurs if you are passing a null value somewhere in your code.
 
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