【问题标题】:Unable to save data into database无法将数据保存到数据库中
【发布时间】:2016-07-20 09:09:30
【问题描述】:

我正在使用运行时 SQL 查询将一些数据插入数据库,但在此之前我正在检查是否存在任何记录。以下是我的代码

    protected void btnSignUp_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        try
        {
            using (SqlConnection con = new SqlConnection(cs))
            {
                string strgender = "";
                if (Rb_Male.Checked)
                    strgender = "Male";
                else if (Rb_Female.Checked)
                    strgender = "Female";
                else
                {

                    lblMsg.Text = "Please Select Gender";
                    lblMsg.ForeColor = Color.Red;
                }

                con.Open();
                SqlCommand cmdcheck = new SqlCommand();
                cmdcheck.CommandText = "select * from [Users] where E_Mail='" + @tb_Email.Text + "'";
                cmdcheck.Connection = con;
                //cmd.Parameters.AddWithValue("@em", tb_Email.Text);
                SqlDataReader drd = cmdcheck.ExecuteReader();
                if (drd.Read())
                {
                    lblEmail.Visible = true;
                    lblEmail.Text = "Email Already Exsits";
                    lblMsg.ForeColor = Color.Red;
                    lblMsg.Text = "Account not created!!!";

                }
                else
                {
                    string strcmd = "insert into Users values ('" + @tbName.Text + "','" + @tbSName.Text + "','" + @tb_Email.Text + "','" + @tb_Pass.Text + "','" + @DropDownDay.Text + "','" + @DropDownMonth.Text + "','" + @DropDownYear.Text + "','"+ strgender +"')";
                    SqlCommand cmd = new SqlCommand(strcmd, con);
                    cmd.ExecuteNonQuery();
                    lblMsg.Text = "Account created sussecfully";
                    lblMsg.ForeColor = Color.Green;
                    clearallfields();
                }


            }
        }
        catch
        {

            lblMsg.ForeColor = Color.Red;
            lblMsg.Text = "Account not created!!!";


        }


    }
    else
    {
        lblMsg.ForeColor = Color.Red;
        lblMsg.Text = " * Enter Required Field(s)";
    }
}

如果没有与特定电子邮件关联的记录,但如果没有记录,则下面的部分运行良好,但如果没有记录,它会转到 else 部分并在 executenonquery() 之后;它进入了捕获部分

if (drd.Read())
            {
                lblEmail.Visible = true;
                lblEmail.Text = "Email Already Exsits";
                lblMsg.ForeColor = Color.Red;
                lblMsg.Text = "Account not created!!!";

            }
            else
            {
                string strcmd = "insert into Users values ('" + @tbName.Text + "','" + @tbSName.Text + "','" + @tb_Email.Text + "','" + @tb_Pass.Text + "','" + @DropDownDay.Text + "','" + @DropDownMonth.Text + "','" + @DropDownYear.Text + "','"+ strgender +"')";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.ExecuteNonQuery();
                lblMsg.Text = "Account created sussecfully";
                lblMsg.ForeColor = Color.Green;
                clearallfields();
            }


        }
    }
    catch
    {

        lblMsg.ForeColor = Color.Red;
        lblMsg.Text = "Account not created!!!";


    }

请帮我解决这个问题..

【问题讨论】:

  • 异常详情是什么?使用Catch(Exception ex)查看
  • 错误是“已经有一个打开的 DataReader 与此命令关联,必须先关闭。”

标签: c# mysql asp.net database twitter-bootstrap


【解决方案1】:

您的插入语句未指定要添加到 Users 表中的列。在以下行放置断点:

string strcmd = "insert into Users values ('" + @tbName.Text + "','" + @tbSName.Text + "','" + @tb_Email.Text + "','" + @tb_Pass.Text + "','" + @DropDownDay.Text + "','" + @DropDownMonth.Text + "','" + @DropDownYear.Text + "','"+ strgender +"')";

获取strcmd 的值并在 SQL Server Management Studio 中执行它。您将看到它很可能会失败。修复插入语句直到它在 SQL 中工作,然后将其复制到您的 ASP.NET Web 应用程序中。

同时更改你的 catch 块并检查你得到的异常:

catch(Exception ex)
{
    System.Diagnostics.Debugger.Break();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多