【问题标题】:Input string was not in a correct format c# and asp.net输入字符串的格式不正确 c# 和 asp.net
【发布时间】:2014-08-30 06:52:07
【问题描述】:

我正在开发一个简单的 asp.net 登录页面。一个简单的代码,当用户登录时,它会使用 sql 进行验证,并直接进入工作人员或管理员的页面。但我有这个错误 (int.Parse(myReader.ToString()) > 0) ...输入字符串的格式不正确 我的代码..

string Connection = "Data Source=(ip);Initial Catalog=..;Persist Security Info=True;User ID=(id);Password=(pass)";
SqlConnection myConn = new SqlConnection(Connection);

SqlCommand SelectCommand = new SqlCommand("select * from tar_login where Username ='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "' and Position='" + ddlPosition.Text + "';", myConn);

myConn.Open();

var myReader = SelectCommand.ExecuteScalar();
myConn.Close();

if (myReader != null)
{
    if (int.Parse(myReader.ToString()) > 0)
    {
        if (ddlPosition.Text == "Admin")
        {
            Response.Redirect("manager.aspx");
        }
    }
}

if (myReader != null)
{
    if (int.Parse(myReader.ToString()) > 0)
    {
        if (ddlPosition.Text == "Staff")
        {
            Response.Redirect("staff.aspx");
        }
    }
}

else
{
    Label1.ForeColor = System.Drawing.Color.Red;
    Label1.Text = "Incorrect. Please try again";
    myConn.Close();
}

.help 作为 asp.net 和 c# 的新手

【问题讨论】:

  • 这意味着 myReader.ToString() 不是数字,或者不是 int 格式。
  • 调试你的代码,在var myReader = SelectCommand.ExecuteScalar();添加一个断点并检查它的值是什么,可能不是整数或空
  • @Noctis 在断点处我得到 myReader 为空

标签: c# ado.net


【解决方案1】:

ExecuteScalar 返回一个对象(行),或 null。
那你试试ToString吧。除非您只返回一列,即 int,否则 int.Parse 将无法解析。

您可以尝试int.TryParse,但我相信它会返回 false。 如果你在myReader 中放一个断点,你有什么?它可能会让您/我们更好地了解您正在处理的内容。


编辑:

o_O ?!

你有两个子句说:if (myReader != null)。合并它们。

每当您使用数据库或任何您需要处理的资源时,请使用 using,就像 Saunders 建议的那样。

除此之外,您还回答了您的问题。如果返回 null,则无法将其解析为 int

您必须弄清楚您的数据库问题或查询,然后再试一次。

【讨论】:

    【解决方案2】:

    您的 ExecuteScalar() 带来了一个无法转换为整数的非数字字符(例如“A”)。

    您的代码中似乎不需要检查 > 0。

    myReader != null 就足够了。

    Samiey 上面的建议看起来都很完美。

    【讨论】:

      【解决方案3】:

      试试这个代码

          string Connection = "Data Source=(ip);Initial Catalog=..;Persist Security Info=True;User ID=(id);Password=(pass)";
          SqlConnection myConn = new SqlConnection(Connection);
      
          SqlCommand SelectCommand = new SqlCommand("select * from tar_login where Username ='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "' and Position='" + ddlPosition.Text + "';", myConn);
      
          myConn.Open();
      
          Sqldatareader myReader = SelectCommand.ExecuteReader();
          myConn.Close();
      
           if (reader.HasRows)
              {
      if (ddlPosition.Text == "Admin")
              {
                  Response.Redirect("manager.aspx");
              }
      else if (ddlPosition.Text == "Staff")
              {
                  Response.Redirect("staff.aspx");
              }
          else
          {
              Label1.ForeColor = System.Drawing.Color.Red;
              Label1.Text = "Incorrect. Please try again";
              myConn.Close();
          }
      

      【讨论】:

      • 嗨,我尝试了你的代码,但我在 HasRows 下面有一条波浪线。当我把光标放在它上面时,我得到了这个错误:'object'不包含'HasRows'的定义,并且找不到接受'object'类型的第一个参数的方法'HasRows'的扩展
      • stackoverflow.com/questions/12609959/…检查datareader是否有行的链接
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多