高分求按查询按钮后无反应到底怎么回事 大侠们帮帮忙

xinnian_123 2010-01-07 09:56:37
SqlCommand cmd; 

string strCon="server=XCH;database=aaa;uid=sa;pwd=;";
if(TextBox1.Text.Length>0)
{
string strqry="select admin from Table1 where (id='"+TextBox1.Text+"')";
SqlConnection conn=new SqlConnection(strCon);
conn.Open();
cmd=new SqlCommand(); //直接使用new 关键字来创建
cmd.CommandText=strqry;
cmd.Connection=conn; //设置与数据库的连接
}
else
{
Response.Write("<script language=javascript>alert('没有符合条件的表单,请重新查询!');</script>");

}
...全文
158 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
xinnian_123 2010-01-08
  • 打赏
  • 举报
回复
string strqry="select id,admin,pwd from Table1 where (admin='"+TextBox1.Text+"')"; 
SqlConnection conn=new SqlConnection();
conn.ConnectionString = strCon;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = strqry;
cmd.CommandType = CommandType.Text;
conn.Open();
this.datagrid1.DataSource = cmd.ExecuteReader();
this.datagrid1.DataBind();
conn.Close();

这样就对了
xinnian_123 2010-01-08
  • 打赏
  • 举报
回复
OK 搞定了 谢谢各位 给分啦
xinnian_123 2010-01-08
  • 打赏
  • 举报
回复
private void databind()
{
string sql = "select id,admin,pwd from Table1";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection("server=XCH;database=aaa;uid=sa;pwd=");
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
conn.Open();

da.Fill(ds,"admin");
this.Grid1.DataKeyField = "id";
this.Grid1.DataSource = ds.Tables["admin"].DefaultView;
this.Grid1.DataBind();
conn.Close();

}
private void Button1_Click(object sender, System.EventArgs e)
{
string strCon="server=XCH;database=aaa;uid=sa;pwd=;";

if(TextBox1.Text.Length>0)
{

string strqry="select admin from Table1 where (admin='"+TextBox1.Text+"')";
SqlConnection conn=new SqlConnection();
conn.ConnectionString = strCon;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = strqry;
cmd.CommandType = CommandType.Text;
conn.Open();
this.Grid1.DataSource = cmd.ExecuteScalar();
this.Grid1.DataBind();
conn.Close();
}

这样出来的是一列啊 我要查询一行
我是要在admin这列中的一个数据,在表中查询admin这一行的数据
khjian 2010-01-08
  • 打赏
  • 举报
回复
没设置返回信息啊
szaf31954 2010-01-08
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 huangwenquan123 的回复:]
把GridView1.DataSource=cmd.ExecuteReader()
这样就可以了
[/Quote]

对 就这么绑定 给gridview设置数据源

this.gridview.DataSource = cmd.ExecuteReader();
this.gridview.DataBind();
saishangpeiqian 2010-01-08
  • 打赏
  • 举报
回复
看你的代码 首先自己都不清楚 即使刚开始学习 代码不会写可以理解 但是一定要思路清晰
huangwenquan123 2010-01-08
  • 打赏
  • 举报
回复
把GridView1.DataSource=cmd.ExecuteReader()
这样就可以了
李班头 2010-01-08
  • 打赏
  • 举报
回复
绑定,楼主加油!
huangwenquan123 2010-01-08
  • 打赏
  • 举报
回复
把DataGrid.DataSource=cmd.ExecuteReader()
这样就可以了
xinnian_123 2010-01-08
  • 打赏
  • 举报
回复
private void chaxun_Click(object sender, System.EventArgs e)
{
string strCon="server=XCH;database=aaa;uid=sa;pwd=;";
if(TextBox1.Text.Length>0)
{
string strqry="select admin from Table1 where (admin='"+TextBox1.Text+"')";
SqlConnection conn=new SqlConnection();
conn.ConnectionString = strCon;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = strqry;
cmd.CommandType = CommandType.Text;
conn.Open();
cmd.ExecuteScalar();
conn.Close();
}
else
{
Response.Write("<script language=javascript>alert('没有符合条件的表单,请重新查询!');</script>");
}
}

可我要把结果显示在datagrid中 该怎样做
xinnian_123 2010-01-07
  • 打赏
  • 举报
回复
SqlCommand cmd; 

string strCon="server=XCH;database=aaa;uid=sa;pwd=;";
if(TextBox1.Text.Length>0)
{
string strqry="select admin from Table1 where (id='"+TextBox1.Text+"')";
SqlConnection conn=new SqlConnection();
conn.ConnectionString = strCon;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = strqry;
conn.Open();
cmd.ExecuteScalar();
conn.Close();
}
else
{
Response.Write("<script language=javascript>alert('没有符合条件的表单,请重新查询!');</script>");
}

报错啊
aicainiao_110 2010-01-07
  • 打赏
  • 举报
回复
SqlCommand cmd; 

string strCon="server=XCH;database=aaa;uid=sa;pwd=;";
if(TextBox1.Text.Length>0)
{
string strqry="select admin from Table1 where (id='"+TextBox1.Text+"')";
SqlConnection conn=new SqlConnection();
conn.ConnectionString = strCon;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = strqry;
conn.Open();
cmd.ExecuteScalar();
conn.Close();
}
else
{
Response.Write("<script language=javascript>alert('没有符合条件的表单,请重新查询!');</script>");
}

报错
不能在此范围内声明名为“cmd”的局部变量,因为这样会使“cmd”具有不同的含义,而它已经用于“父级或当前”范围以表示其他内容
该怎么解决呢 帮帮忙吧
aicainiao_110 2010-01-07
  • 打赏
  • 举报
回复
关注中。。。。
xinnian_123 2010-01-07
  • 打赏
  • 举报
回复
string   strCon= "server=XCH;database=aaa;uid=sa;pwd=; ";   
if(TextBox1.Text.Length> 0)
{
string strqry= "select admin from Table1 where (id= ' "+TextBox1.Text+ " ') ";
SqlConnection conn=new SqlConnection();
conn.ConnectionString = strCon;
cmd=new SqlCommand();
cmd.CommandText = strqry;
conn.Open();
cmd.ExecuteScalar();
conn.Close();
}
xinnian_123 2010-01-07
  • 打赏
  • 举报
回复
string strCon="server=XCH;database=aaa;uid=sa;pwd=;"; 
if(TextBox1.Text.Length>0)
{
string strqry="select admin from Table1 where (id='"+TextBox1.Text+"')";
SqlConnection conn=new SqlConnection();
conn.ConnectionString = strCon;
cmd=new SqlCommand();
cmd.CommandText = strqry;
conn.Open();
cmd.ExecuteScalar();
conn.Close();
}

这样写点击查询后报错
异常详细信息: System.InvalidOperationException: ExecuteReader: Connection 属性尚未初始化
xinnian_123 2010-01-07
  • 打赏
  • 举报
回复
ID是int
Lovely_baby 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xinnian_123 的回复:]
不好意思我刚学
[/Quote]
可以考虑使用设断点跟踪看看 到底是哪里出错~
szaf31954 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 guozhaoyou1 的回复:]
cmd.ExecuteReader();
再者看看你数据库中ID 是string 还是int
  如果int
还要改这个 where (id="+TextBox1.Text+")
[/Quote]

对 如果ID是INT型的不需要用字符穿连接 直接id="+TextBox1.Text+"就可以
szaf31954 2010-01-07
  • 打赏
  • 举报
回复
SqlCommand cmd;

string strCon="server=XCH;database=aaa;uid=sa;pwd=;";
if(TextBox1.Text.Length>0)
{
string strqry="select admin from Table1 where (id='"+TextBox1.Text+"')";
SqlConnection conn=new SqlConnection();
conn.ConnectionString = strCon;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = strqry;
conn.Open();
cmd.ExecuteScalar();
conn.Close();
}
else
{
Response.Write("<script language=javascript>alert('没有符合条件的表单,请重新查询!');</script>");

}
guozhaoyou1 2010-01-07
  • 打赏
  • 举报
回复
cmd.ExecuteReader();
再者看看你数据库中ID 是string 还是int
如果int
还要改这个 where (id="+TextBox1.Text+")
加载更多回复(9)

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧