Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace AllQueryRun
{
    class Program
    {
        static void Main(string[] args)
        {
            //1: Specify SQL Server connection string
            SqlConnection con =new SqlConnection("Data Source=(local);Initial Catalog=migrationtest; Trusted_Connection=True");

        //  SqlConnection con = new SqlConnection(@"Data  Source =.\SQLEXPRESS;"+@"AttachDbFilename=?C:\SQL Server 2000 Sample Databases\NORTHWIND.MDF?;" +@"Integrated Security=True;Connect Timeout=30;User Instance=true" );

          //2: Create DataAdapter object

            SqlDataAdapter da = new SqlDataAdapter("Select TEMPLATEID,TEMPLATENAME,DESCRIPTION from TemplateMaster", con);

          //3: Create DataSet object to contain  data tables, rows, and columns

          DataSet ds = new DataSet();

         //4: Fill DataSet using query defined for DataAdapter

          da.Fill(ds,"TemplateMaster details");

         //5:Access data row wise using foreach loop

          foreach (DataRow dr in ds.Tables["TemplateMaster"].Rows)

         {

             Console.WriteLine(dr["TEMPLATEID"] + "\t" + dr["TEMPLATENAME"]);
             

         }

         //Closing the connection

         con.Close();
         Console.Write("Program finished, press Enter/Return to continue:");

         Console.ReadLine();

        }
    }
}



i got object reference not set to an instance of an object where is error every thing is okey
Posted
Comments
ZurdoDev 10-Jun-14 7:05am    
Which line causes the error and what is your question?
[no name] 10-Jun-14 7:06am    
No everything is not "okey" otherwise you would not be getting that exception. Since you did not say where the error occurs, you are getting that error for the exact same reason that everyone else gets that error. You are trying to use the methods or properties of an object that is null.
Ramug10 10-Jun-14 7:09am    
5th step you must check whether ds contains tables or not.
Arjunwalmiki 10-Jun-14 7:12am    
in this line i am got error

foreach (DataRow dr in ds.Tables["TemplateMaster"].Rows)
{

Check the table name: it should match the Fill command:
C#
da.Fill(ds,"TemplateMaster details");
 foreach (DataRow dr in ds.Tables["TemplateMaster details"].Rows)
Or better, fill a DataTable instead of a DataSet if you are only returning one set of results.
 
Share this answer
 
C#
da.Fill(ds,"TemplateMaster");
 foreach (DataRow dr in ds.Tables["TemplateMaster"].Rows)


thank to all problem in table name spl thanks to originalGriff
 
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