Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my code

employee emp = new emp();
DataSet ds = new DataSet("emp");


string nameslist = string.Empty;
foreach (DataRow dr in ds.Tables["EMPLOYEE"].Rows)
{
nameslist += dr["ENAME"];
}

here i got the error
object reference not set to an instance of an object
what can i do help me
thanks in advance....
Posted
Comments
jo.him1988 8-Jul-14 7:12am    
your table name not found or you did not fill your dataset thats why you are getting object reff error here is the solution

Check the spelling: is there a table called "EMPLOYEE"? Remember that C# is case sensitive...

That is assuming there is code between these two lines:
C#
DataSet ds = new DataSet("emp");

 
string nameslist = string.Empty;
If there isn't, then perhaps reading some information into the dataset would be a good idea first?
 
Share this answer
 
object reference error comes when your object is null
here is your ds.Tables["EMPLOYEE"] is null becouse you not fill it
when you created dataset
you also have to create sqldataadapter to fill the dataset and fill time set the name of table i.e

SqlDataAdapter da = new SqlDataAdapter(cmd);
               System.Data.DataSet ds = new System.Data.DataSet("emp");
               da.Fill(ds,"EMPLOYEE");

C#
string nameslist = string.Empty;
foreach (DataRow dr in ds.Tables["EMPLOYEE"].Rows)
{
nameslist += dr["ENAME"];
}



test code is here

try
           {
               System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(@"Data Source=My\SQLEXPRESS;Initial Catalog=Test;Integrated Security=true;");
               con.Open();
               System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
               cmd.CommandText = "select * from EMPLOYEE";
               cmd.Connection = con;
               employee emp= new employee();
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               System.Data.DataSet ds = new System.Data.DataSet("emp");
               da.Fill(ds,"EMPLOYEE");
               string nameslist = string.Empty;
               foreach (DataRow dr in ds.Tables["EMPLOYEE"].Rows)
               {
                   nameslist += dr["ENAME"];
               }
           }
           catch (Exception ex)
           { throw ex;}



happy coding :) :) :)
 
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