System.invalidoperationexception:fill:selectcommand.connection属性尚未初始化 [英] System.invalidoperationexception: fill: selectcommand.connection property has not been initialized
问题描述
使用System;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Data.SqlClient;
使用System.Data ;
公共部分类adminpanel_index:System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@Data Source = DESKTOP-Q4BLQB1 \ SQL2014; Initial Catalog = naturetile; Integrated Security = True);
int i;
protected void Page_Load(object sender,EventArgs e)
{
}
protected void Button2_Click(object sender,EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText =select * from adsign where emid =' + TextBox1.Text +'和pass ='+ TextBox1.Text +';
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
i = Convert.ToInt32(dt.Rows.Count.ToString());
if(i == 1 )
{
Response.Redirect(home.aspx);
}
else
{
Label1.Text =你输入了无效的U& p;
}
con.Close() ;
}
}
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class adminpanel_index : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-Q4BLQB1\SQL2014;Initial Catalog=naturetile;Integrated Security=True");
int i;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select* from adsign where emid='" + TextBox1.Text + "'and pass='" + TextBox1.Text + "'";
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
i = Convert.ToInt32(dt.Rows.Count.ToString());
if (i == 1)
{
Response.Redirect("home.aspx");
}
else
{
Label1.Text = "You have enter invalid U&p";
}
con.Close();
}
}
我尝试过:
想要登录成功并重定向到其他页面
What I have tried:
want to login success and redirect to other page
推荐答案
错误不言自明,你还没有设置命令的连接。
The error is self-explanatory, you have not set the connection for the command.
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
当你有错误时,总是说出它在哪一行。
您的代码也容易受到SQL注入攻击,使用参数化查询而不是字符串连接。
Also when you have an error always say what line it is on.
Your code is also liable to SQL injection attacks, use parameterised queries rather than string concatenation.
这篇关于System.invalidoperationexception:fill:selectcommand.connection属性尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!