views:

524

answers:

1

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.

A: 

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();
Stan R.
Hi Stan,The "RetailTrainingUserLevel" is set to NOT allow null.I tried your code suggestion and am still getting the error: "Input string was not in a correct format." at userLevelLabel.Text += reader["RetailTrainingUserLevel"].ToString();I'm confused about why there's an issue. The reader wants a "RetailTrainingUserLevel" value, which in the table is set to varchar - the possible values are Manager, Supervisor, Principal which all seem like strings to me. Not sure what else to try. Is my JOIN statement not set up correctly? Thanks for any other ideas you may have...
Doug