繁体   English   中英

使用SqlDataReader和Label时“输入字符串的格式不正确”

[英]“Input string was not in a correct format” when using SqlDataReader and Label

我在Select语句中加入了两个表-Contact和RetailTrainingUserLevelMap。

(两者中的公共列都是RetailTrainingUserLevelID int)

选择Contact.IntranetUserName,Contact.CompanyName,RetailTrainingUserLevelMap.RetailTrainingUserLevel

来自联系人

内联接RetailTrainingUserLevelMap启用Contact.RetailTrainingUserLevelID = RetailTrainingUserLevelMap.RetailTrainingUserLevelID

AND Contact.RetailTrainingUserLevelID = RetailTrainingUserLevelMap.RetailTrainingUserLevelID

在哪里(Contact.IntranetUserName = @IntranetUserName)


如果我通过Visual Studio查询生成器(测试查询窗口)运行此语句,并输入“ IntranetUserName”的值,则会得到:

IntranetUserName:
约翰·乔

公司名:
Acme Inc.

RetailTrainingUserLevel:
经理

到目前为止,这是我想要的输出。

如果我在我的.cs代码中使用SqlDataReader将相同的select语句使用SqlDataReader将标签绑定到以下某些列:

SqlCommand comm;
        SqlConnection conn;
        string intranetConnectionString = ConfigurationManager.ConnectionStrings["DataConnect"].ConnectionString;
        conn = new SqlConnection(intranetConnectionString);
        comm = new SqlCommand("SELECT Contact.IntranetUserName, Contact.CompanyName, RetailTrainingUserLevelMap.RetailTrainingUserLevel FROM Contact INNER JOIN RetailTrainingUserLevelMap ON Contact.RetailTrainingUserLevelID = RetailTrainingUserLevelMap.RetailTrainingUserLevelID AND Contact.RetailTrainingUserLevelID = RetailTrainingUserLevelMap.RetailTrainingUserLevelID WHERE (Contact.IntranetUserName = @IntranetUserName)", conn);

        comm.Parameters.Add("@IntranetUserName", System.Data.SqlDbType.VarChar, 50);
        comm.Parameters["@IntranetUserName"].Value = memberLoginName;

        conn.Open();
        SqlDataReader reader = comm.ExecuteReader();
        while (reader.Read())
        {
            memberCompanyNameLabel.Text += reader["CompanyName"];
           userLevelLabel.Text += reader["RetailTrainingUserLevel"];
        }

        reader.Close();
        conn.Close();

我收到错误“输入字符串的格式不正确”。 这里:
userLevelLabel.Text + = reader [“ RetailTrainingUserLevel”];

这里需要更改什么C#语法,以便我可以将该值正确绑定到我的userLevelLabel?

注意:RetailTrainingUserLevelID int
RetailTrainingUserLevel varchar(50)

感谢您的时间和知识。

您确定reader [“ RetailTrainingUserLevel”]不是DBNull?

在尝试将其添加到另一个字符串之前,可能需要对它执行.ToString()。

我会做

userLevelLabel.Text += reader["RetailTrainingUserLevel"].ToString();

暂无
暂无

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

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