名称“”'在当前上下文中不存在

时间:2014-03-16 22:54:07

标签: c# asp.net registration

我的代码中遇到了几个错误。这些错误是:

  • 错误17当前上下文中不存在名称“CommandType”
  • 错误18当前上下文中不存在名称“SqlDbType”
  • 错误35当前上下文中不存在名称“txtCity”

如果您能帮助我理解错误并告诉我如何解决它,我想。

protected void btnSave_Click(object sender, EventArgs e)
{
    // create connectionstring and insert statment
    string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
    string insertSql = " INSERT INTO UserInfo (UID, FN, LN, Password, RePass, Email, Country,State, City)" +
        " values (@UsrNme, @fnbox, @lnamebox, @passtxtbx1, @passtxtbx2, @emailbox, @DrDncoundrlst, @DropDownListSwestate, @citytxtbox)";

    // create SQL Connection

    SqlConnection con = new SqlConnection(connectionString);

    // create sql command and parameters

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandType = CommandType.text;
    cmd.CommandText = insertSql;

    SqlParameter UID = new SqlParameter("@UsrNme", SqlDbType.nvarchar, 50);
    UID.Value = txtUID.text.tostring();
    cmd.Parameters.Add(UID);

    SqlParameter FN = new SqlParameter("@fnbox", SqlDbType.varchar,25);
    cmd.Connection = con;
    FN.Value = txtfn.text.ToString();
    cmd.Parameters.Add(FN);



    SqlParameter LN = new SqlParameter("@lnamebox", SqlDbType.varchar, 25);
    cmd.Connection = con;
    LN.Value = txtLN.text.ToString();
    cmd.Parameters.Add(LN);

    SqlParameter Password = new SqlParameter("@passtxtbx1", SqlDbType.varchar, 25);
    cmd.Connection = con;
    Password.Value = txtPassword.text.ToString();
    cmd.Parameters.Add(Password);


    SqlParameter RePass = new SqlParameter("@passtxtbx2", SqlDbType.varchar, 25);
    cmd.Connection = con;
    RePass.Value = txtRePass.text.ToString();
    cmd.Parameters.Add(RePass);


    SqlParameter Email = new SqlParameter("@emailbox", SqlDbType.varchar, 25);
    cmd.Connection = con;
    Email.Value = txtEmail.text.ToString();
    cmd.Parameters.Add(Email);

    SqlParameter Country = new SqlParameter("@DrDncoundrlst", SqlDbType.varchar, 25);
    cmd.Connection = con;
    Country.Value = txtCountry.text.ToString();
    cmd.Parameters.Add(Country);

    SqlParameter State = new SqlParameter("@DropDownListSwestate", SqlDbType.varchar, 25);
    cmd.Connection = con;
    State.Value = txtState.text.ToString();
    cmd.Parameters.Add(State);

    SqlParameter City = new SqlParameter("@citytxtbox", SqlDbType.varchar, 25);
    cmd.Connection = con;
    City.Value = txtCity.text.ToString();
    cmd.Parameters.Add(City);

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
        lblmsg.Text = "You already complete your registration process";
    }
    catch (SqlException ex)
    {
        string errorMessage = "error in registration user";
        errorMessage += ex.Message;
        throw new Exception(errorMessage);
    }

    finally
    {
        con.Close();
    }

}

1 个答案:

答案 0 :(得分:3)

你可能会过于复杂化。请尝试以下代码...

var connection = new SqlConnection(connectionString);
var cmd = new SqlCommand(insertSql, connection);

cmd.Parameters.AddWithValue("@UsrNme", txtUID.Text.ToString());
cmd.Parameters.AddWithValue("@fnbox", txtfn.Text.ToString());
cmd.Parameters.AddWithValue("@lnamebox", txtLN.Text.ToString());
cmd.Parameters.AddWithValue("@passtxtbx1", txtPassword.Text.ToString());
cmd.Parameters.AddWithValue("@passtxtbx1", txtPassword.Text.ToString());
cmd.Parameters.AddWithValue("@passtxtbx2", txtRePass.Text.ToString());
cmd.Parameters.AddWithValue("@emailbox", txtEmail.Text.ToString());
cmd.Parameters.AddWithValue("@DrDncoundrlst", txtCountry.Text.ToString());
cmd.Parameters.AddWithValue("@DropDownListSwestate", txtState.Text.ToString());
cmd.Parameters.AddWithValue("@citytxtbox", txtCity.Text.ToString());

try
{
    cmd.Connection.Open();
    cmd.ExecuteNonQuery();
    cmd.Connection.Close();

    lblmsg.Text = "You already completed your registration process";
}
catch (SqlException ex)
{
    var errorMessage = "error in registration user";

    errorMessage += ex.Message;

    throw new Exception(errorMessage);
}
finally
{
    con.Close();
}

您还要确保您已声明以下using子句...

using System.Data;
using System.Data.SqlClient;

... txtCity就是您实际上称之为文本框的内容,并且private标识符未将此方法隐藏,或者实际显示在其他表单上,因为您获取的错误意味着该变量超出了方法的范围,或者在您达到此方法之前其生命周期已到期。

这是所有代码的功能。它不是设置你不需要声明参数的大量元数据,而是让SqlCommand为你完成所有艰苦的工作,并根据数据库列,你传入的对象的类型,以及参数的名称。如果您最终允许传递无效数据,那么精心设计的元数据标记都不会使您免于错误。

同样,您真的希望在Sql Server中将insertSql包装到存储过程中...

create procedure adduserinfo @UsrNme               nvarchar (50),
                             @fnbox                varchar (25),
                             @lnamebox             varchar (25),
                             @passtxtbx1           varchar (25),
                             @passtxtbx2           varchar (25),
                             @emailbox             varchar (25),
                             @DrDncoundrlst        varchar (25),
                             @DropDownListSwestate varchar (25),
                             @citytxtbox           varchar (25)

as begin
   INSERT INTO UserInfo 
     ( UID, 
       FN, 
       LN, 
       Password, 
       RePass, 
       Email, 
       Country,
       State, 
       City )
   VALUES
     ( @UsrNme, 
       @fnbox, 
       @lnamebox, 
       @passtxtbx1, 
       @passtxtbx2, 
       @emailbox, 
       @DrDncoundrlst, 
       @DropDownListSwestate, 
       @citytxtbox )
end
go

然后你的SqlCommand声明会如此......

var command = new SqlCommand("adduserinfo", connection) 
{ 
    CommandType = CommandType.StoredProcedure; 
}

...其余的,您可以按照我上面提供的其余代码进行操作。这或多或少都是正确的方法。并且冒着挑剔的风险,考虑提供更多信息和一致格式的变量和参数名称。那些可能在将来修改你的代码的人会感谢你。