简体   繁体   English

输入字符串的格式不正确

[英]Input string was not in a correct format

  public int getcid(string UserName)
  {
            SqlConnection con = new SqlConnection(strConnString);
            con.Open();

            int js;

            string query = "select Username from register_tab where Email='" + UserName + "' ";
            sqlda = new SqlDataAdapter(query, con);
            DataSet ds = new DataSet();
            sqlda.Fill(ds);

            js = Convert.ToInt32(ds.Tables[0].Rows[0]["Username"].ToString());

            return (js);

在此处输入图片说明

Change your method to this and check it out:将您的方法更改为此并检查一下:

SqlConnection con = new SqlConnection(strConnString);
            con.Open();
string js;
string query= "select Username from register_tab where Email= @username";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("@username",SqlDbType.VarChar, 50).Value =  
                                           UserName;

using(SqlDataReader reader= cmd.ExecuteReader())
{
    while (reader.Read())
    {
        js=  reader["Username"].ToString();
    }
}

con.Close();
return js;

Also why do you set your UserName to Email Column in your query?另外,为什么在查询中将用户名设置为电子邮件列? And why do you use DataSet if you only want to return int?如果只想返回 int,为什么要使用 DataSet?

UPDATE: no need to convert to int.更新:无需转换为 int。

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

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