当我运行时发生错误,如下所示SelectCommand.Connection属性尚未初始化. [英] when i run error occurs as follows SelectCommand.Connection property has not been initialized.

查看:72
本文介绍了当我运行时发生错误,如下所示SelectCommand.Connection属性尚未初始化.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,如下所示:

This is my code as follows:

con.Open();
string strQuery = "select Sno,Name,Age,Address" +
             " from Details";
SqlCommand cmd = new SqlCommand(strQuery);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
con.Close();
GridView1.DataSource = ds;
GridView1.DataBind();


当我运行此错误时,将发生以下错误:


When I run this an error occurs as follows:

SelectCommand.Connection property has not been initialized.


该行中发生错误.


The error occurs in this line;

adp.Fill(ds);

推荐答案

添加以下行:
add this line:
cmd.Connection=con;


这篇关于当我运行时发生错误,如下所示SelectCommand.Connection属性尚未初始化.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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