简体   繁体   English

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

[英]The name '"' does not exist in the current context

I am facing several errors in my code. 我在代码中遇到了几个错误。 These errors are: 这些错误是:

  • Error 17 The name 'CommandType' does not exist in the current context 错误17名称'CommandType'在当前上下文中不存在
  • Error 18 The name 'SqlDbType' does not exist in the current context 错误18名称'SqlDbType'在当前上下文中不存在
  • Error 35 The name 'txtCity' does not exist in the current context 错误35名称txtCity在当前上下文中不存在

I would like if you can help me to understand the error and tell me how I can fix it. 我想如果您能帮助我理解该错误并告诉我该如何解决。

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();
    }

}

You may be way over-complicating things. 您可能会使事情变得过于复杂。 Try the following code... 尝试以下代码...

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();
}

You also want to make sure the you have the following using clauses declared... 您还想确保已声明以下using子句...

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

... and that txtCity is what you actually called your text box and that it's not hidden from this method by a private identifier or actually appears on a different form because the error you're getting means that the variable is outside the method's scope or has its lifetime expire before you reach this method. ...而txtCity就是您实际上称为文本框的名称,并且不会被private标识符隐藏在此方法中,或者实际上不会以其他形式出现,因为您得到的错误意味着变量不在方法的范围内,或者在使用此方法之前,其生命周期已到期。

Here's what all that code does. 这就是所有代码的作用。 Instead of setting up tons of metadata that you should not need to declare your parameters, it lets SqlCommand do all the hard work for you and figure out what type is what based on the database column, the type of the object you passed in, and the name of the parameter. 无需设置不需要声明参数的大量元数据,它使SqlCommand为您完成所有艰苦的工作,并根据数据库列,传入的对象的类型,确定什么类型是什么,以及参数的名称。 If you end up allowing the passing of invalid data, none of the elaborate metadata markup is going to save you from an error. 如果最终允许传递无效数据,那么精心制作的元数据标记都不会使您免于错误。

Likewise, you really want to look into wrapping your insertSql into a stored procedure like so in Sql Server... 同样,您真的想研究将您的insertSql包装到存储过程中,就像在Sql Server中一样。

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

Then your SqlCommand declaration would look like so... 然后您的SqlCommand声明将如下所示...

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

... and for the rest you'd follow the rest of the code I provided above. ...,其余的您将遵循我上面提供的其余代码。 This would be the more or less proper way to do it. 这或多或少是正确的方法。 And at the risk of sounding nitpicky, consider more informative and consistently formatted variable and parameter names. 并且冒着听起来有些挑剔的风险,请考虑提供更多信息且格式一致的变量和参数名称。 Those who might have to modify your code in the future will thank you for it. 那些将来可能需要修改您的代码的人将感谢您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM