0
    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# 编写,然后它无法正常工作。

4

5 回答 5

9

更改此行

 SqlCommand cmd = new SqlCommand("select * from Console where Text = @TextTmp",sqlConn );

同样错误的参数名称也更改了分配参数的语句

 param.ParameterName = "@TextTmp"; 

只调用一次executereader

于 2013-05-03T19:07:50.023 回答
8

您需要指定SqlCommand.Connection.

您可以将其作为第二个参数传递给备用构造函数

new SqlCommand("select * from Console where Text = @TextTmp", sqlConn);

或者您可以直接设置属性:

cmd.Connection = sqlConn;
于 2013-05-03T19:07:11.303 回答
0

这段代码有几个问题,包括但不限于:

  • 您没有分配Connection.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;
            }
        }
    }
于 2013-05-03T19:50:32.080 回答
0

您需要在 SqlCommand 中指定 SqlConnection。看这个例子:

SqlCommand cmd = new SqlCommand("select * from Console where Text = @TextTmp",sqlConn );

查看http://www.dotnetperls.com/sqlconnection了解更多信息。

于 2013-05-03T19:10:32.460 回答
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;
    }
于 2013-05-03T19:11:54.663 回答