protected void Submit_Click(object sender, EventArgs e)
{
//string strCon = "Server=yourServer;Database=BuspassDb;User Id=Sa;Password=india;";
SqlConnection sqlConn = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand("select * from Console where Text = @TextTmp", sqlConn);
//cmd.CommandText = "CheckIfStringExists";
cmd.Parameters.AddWithValue("@TextTmp", txtString.Text);
//SqlParameter param = new SqlParameter();
//param.ParameterName = "@TextTmp";
//param.Value = txtString.Text;
//cmd.Parameters.Add(param);
try
{
sqlConn.Open();
SqlDataReader reader = cmd.ExecuteReader();
//reader = cmd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
txtString.Text = (reader["Text"].ToString());
lblMessage.Text = txtString.Text + ".... is already exists";
}
else
lblMessage.Text = txtString.Text + "... is not exists";
txtString.Text = "";
sqlConn.Close();
//SqlConnection sqlCon = new SqlConnection(strCon);
//SqlCommand cmdd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "InsertConsole2";
cmd.Connection = sqlConn;
//cmd.Parameters.AddWithValue("@TextTmp", txtString.Text);
sqlConn.Open();
cmd.ExecuteScalar();
lblMessage.Text = txtString.Text + "....is Inserted";
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
It shows the following error on the page
我的错误帮助
如果插入未检查的数据
,则检查未插入的数据如果有人帮助完成这个问题,那就
> ExecuteReader: Connection property has not been initialized.
I am writing in C# then it is not working properly.
我在C#中写作然后它无法正常工作。
答案 0 :(得分:9)
更改此行
SqlCommand cmd = new SqlCommand("select * from Console where Text = @TextTmp",sqlConn );
也错误的参数名称也改变了赋值参数的声明
param.ParameterName = "@TextTmp";
仅调用executereader一次
答案 1 :(得分:8)
您需要指定SqlCommand.Connection
。
您可以将其作为第二个参数传递给alternate constructor:
new SqlCommand("select * from Console where Text = @TextTmp", sqlConn);
或者您可以直接设置属性:
cmd.Connection = sqlConn;
答案 2 :(得分:0)
您需要在SqlCommand中指定SqlConnection。见这个例子:
SqlCommand cmd = new SqlCommand("select * from Console where Text = @TextTmp",sqlConn );
答案 3 :(得分:0)
protected void Submit_Click(object sender, EventArgs e)
{
string strCon = "Server=yourServer;Database=yourDB;User Id=Username;Password=Password;";
SqlConnection sqlConn = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand("select * from Console where Text = @TextTmp", sqlConn);
cmd.Parameters.AddWithValue("@TextTmp", txtString.Text);
try
{
sqlConn.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
txtString.Text = reader["Text"].ToString();
lblMessage.Text = txtString.Text + ": String is already exists";
}
else
{
lblMessage.Text = txtString.Text + ": No data";
txtString.Text = "";
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
答案 4 :(得分:0)
此代码存在多个问题,包括但不限于:
Connection
。{/ li>的SqlCommand
属性
@StringTmp
而不是@TextTmp
)。ExecuteReader()
两次。using
阻止或自己致电Dispose()
。此外,您实际上正在为自己分配txtString.Text
。
编辑:代码示例
protected void Submit_Click(object sender, EventArgs e)
{
using (SqlConnection sqlConn = new SqlConnection(strCon))
using (SqlCommand cmd = new SqlCommand("select * from Console where Text = @TextTmp", sqlConn))
{
cmd.Parameters.AddWithValue("@TextTmp", txtString.Text);
try
{
sqlConn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
reader.Read();
txtString.Text = reader["Text"].ToString(); // Why?
lblMessage.Text = txtString.Text + "String is already exists";
}
else
{
lblMessage.Text = txtString.Text + "No data";
txtString.Text = "";
}
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
}
}