对象引用未设置为我的代码中的对象实例请帮助我 [英] object reference not set to an instance of an object in my code pls help me

查看:57
本文介绍了对象引用未设置为我的代码中的对象实例请帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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得到的对象引用未设置为对象的实例,其中错误是每个都是okey




i got object reference not set to an instance of an object where is error every thing is okey

推荐答案

检查表名:它应该与Fill命令匹配:

Check the table name: it should match the Fill command:
da.Fill(ds,"TemplateMaster details");
 foreach (DataRow dr in ds.Tables["TemplateMaster details"].Rows)

或者更好的是,如果只返回一个DataTable而不是DataSet结果集。

Or better, fill a DataTable instead of a DataSet if you are only returning one set of results.


da.Fill(ds,"TemplateMaster");
 foreach (DataRow dr in ds.Tables["TemplateMaster"].Rows)





谢谢感谢originalGriff



thank to all problem in table name spl thanks to originalGriff


这篇关于对象引用未设置为我的代码中的对象实例请帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆