简体   繁体   English

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

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

I'm joining two tables - Contact and RetailTrainingUserLevelMap in a Select statement. 我在Select语句中加入了两个表-Contact和RetailTrainingUserLevelMap。

(The common column in both is the RetailTrainingUserLevelID int) (两者中的公共列都是RetailTrainingUserLevelID int)

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

FROM Contact 来自联系人

INNER JOIN RetailTrainingUserLevelMap ON Contact.RetailTrainingUserLevelID = RetailTrainingUserLevelMap.RetailTrainingUserLevelID 内联接RetailTrainingUserLevelMap启用Contact.RetailTrainingUserLevelID = RetailTrainingUserLevelMap.RetailTrainingUserLevelID

AND Contact.RetailTrainingUserLevelID = RetailTrainingUserLevelMap.RetailTrainingUserLevelID AND Contact.RetailTrainingUserLevelID = RetailTrainingUserLevelMap.RetailTrainingUserLevelID

WHERE (Contact.IntranetUserName = @IntranetUserName) 在哪里(Contact.IntranetUserName = @IntranetUserName)


If I run this statement through Visual Studio Query Builder(the test query window), and enter a value for "IntranetUserName" I get: 如果我通过Visual Studio查询生成器(测试查询窗口)运行此语句,并输入“ IntranetUserName”的值,则会得到:

IntranetUserName: IntranetUserName:
John Joe 约翰·乔

CompanyName: 公司名:
Acme Inc. Acme Inc.

RetailTrainingUserLevel: RetailTrainingUserLevel:
Manager 经理

This is my desired output, so far so good. 到目前为止,这是我想要的输出。

If I use this same select statement in my .cs codebehind using a SqlDataReader to bind labels to Some of these columns like this: 如果我在我的.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();

I get the error "Input string was not in a correct format." 我收到错误“输入字符串的格式不正确”。 here: 这里:
userLevelLabel.Text += reader["RetailTrainingUserLevel"]; userLevelLabel.Text + = reader [“ RetailTrainingUserLevel”];

What C# syntax needs to be changed here so I can get that value properly bound to my userLevelLabel? 这里需要更改什么C#语法,以便我可以将该值正确绑定到我的userLevelLabel?

Note: RetailTrainingUserLevelID int 注意:RetailTrainingUserLevelID int
RetailTrainingUserLevel varchar (50) RetailTrainingUserLevel varchar(50)

Thanks for your time and knowledge. 感谢您的时间和知识。

You sure reader["RetailTrainingUserLevel"] is not DBNull?? 您确定reader [“ RetailTrainingUserLevel”]不是DBNull?

might want to do .ToString() on it before trying to add it to another string. 在尝试将其添加到另一个字符串之前,可能需要对它执行.ToString()。

I would just do 我会做

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

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

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