Any idea why one assign statement works and the other does not?

185 Views Asked by At

Here's my code:

string strSQL = "SELECT * from tMedia where SKU = '" + SKU + "'";
FbCommand command = new FbCommand(strSQL, databaseConn);

if (databaseConn.State == ConnectionState.Closed)
    databaseConn.Open();

FbDataReader data = command.ExecuteReader();
data.Read();  //  only one row is returned

//  assignment to "x" is empty (277?)
string x = (string)data["ProductType"].ToString();

//  find product type and set flag for later testing
//  obviously, these don't work either!
if (data["ProductType"].ToString().Contains("Video "))
    videoFormat = true;
else if (data["ProductType"].ToString().Contains("Music: "))
    audioFormat = true;

//  coProductType.Text assignment is correct
coProductType.Text = data["ProductType"].ToString();
3

There are 3 best solutions below

0
On BEST ANSWER

It's working now... here's the code if anyone is interested... I don't understand why moving the objects to outside of the method made it work. If anyone can enlighten me, I would really appreciate it.

    string mediaFormat = "";
    bool video;
    bool audio;
    //---------------------------    populate the detail panel    ---------------------|
    private int PopulateDetailPanel(string SKU) {
        decimal convertedMoney;

        clearDetailPanel();  //  clear out old stuff

        //  now, find all data for this SKU
        if (databaseConn.State == ConnectionState.Closed)
            databaseConn.Open();

        string strSQL = "SELECT * from tMedia where SKU = '" + SKU + "'";
        FbCommand command = new FbCommand(strSQL, databaseConn);

        //  find product type and set flag for later testing
        FbDataReader data = command.ExecuteReader();
        data.Read();  //  only one row is returned

        coProductType.Text = data["ProductType"].ToString();  //  while we're here, might as well set it now
        mediaFormat = data["ProductType"].ToString();

        if (mediaFormat.Substring(0, 6) == "Video ")
            video = true;
        else if (mediaFormat.Substring(0, 7) == "Music: ")
            audio = true;
3
On

Since the only difference is the cast to string, it seems like a reasonable first step would be to remove that. It shouldn't be necessary anyway.

3
On

Maybe you need to deal with the problem that will occur when someone enters an invalid SKU and NO data rows are returned.