I'm joining two tables - Contact and RetailTrainingUserLevelMap in a Select statement.
(The common column in both is the RetailTrainingUserLevelID int)
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)
If I run this statement through Visual Studio Query Builder(the test query window), and enter a value for "IntranetUserName" I get:
IntranetUserName:
John Joe
CompanyName:
Acme Inc.
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:
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"];
What C# syntax needs to be changed here so I can get that value properly bound to my userLevelLabel?
Note: RetailTrainingUserLevelID int
RetailTrainingUserLevel varchar (50)
Thanks for your time and knowledge.
You sure reader["RetailTrainingUserLevel"] is not DBNull??
might want to do .ToString() on it before trying to add it to another string.
I would just do
userLevelLabel.Text += reader["RetailTrainingUserLevel"].ToString();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.