I am trying to pull information for items to display on the page. For some reason, the Master Category (masCat.cat_name) is not returning data. Here is the code for my SQL statement:
strSQL.CommandText = "Select Top 25 tblItem.item_id, tblItem.item_title
, masCat.cat_name, regCat.cat_name, tblItem.item_lot, tblCosigner.cs_txt_id
, tblItem.item_status, tblItem.item_photo_status, tblItem.item_pr
, tblItem.item_premium
From tblItem
Left Join tblCosigner On (tblItem.item_cs_txt_id = tblCosigner.cs_txt_id)
Left Join tblCDR On (tblItem.item_cdr_txt_id = tblCDR.cs_id)
Left Join tblCat As masCat On (tblItem.item_mcat_id = masCat.cat_id)
Left Join tblCat As regCat On (tblItem.item_cat_id = regCat.cat_id)
Where " + sqlQuery + "
Order By tblItem.item_id;";
try
{
conn.Open();
using (SqlDataReader itemReader = strSQL.ExecuteReader())
{
while (itemReader.Read())
{
resultText += "<tr>\n<td>" + itemReader.GetValue(0).ToString() + "</td>\n";
resultText += "<td>" + itemReader.GetValue(1).ToString() + "</td>\n";
resultText += "<td>" + itemReader.GetValue(2).ToString() + "</td>\n";
resultText += "<td>" + itemReader.GetValue(3).ToString() + "</td>\n";
resultText += "<td>" + itemReader.GetValue(4).ToString() + "</td>\n";
resultText += "<td>" + itemReader.GetValue(5).ToString() + "</td>\n";
resultText += "<td>" + itemReader.GetValue(6).ToString() + "</td>\n";
resultText += "<td>" + itemReader.GetValue(7).ToString() + "</td>\n";
if (itemReader.GetValue(8).ToString().Length > 0)
{
price = double.Parse(itemReader.GetValue(8).ToString());
}
if (itemReader.GetValue(9).ToString().Length > 0)
{
premium = double.Parse(itemReader.GetValue(9).ToString());
}
total = price + premium;
resultText += "<td>" + total.ToString("0.00") + "</td>\n</tr>\n";
}
itemReader.Close();
}
}
I was thinking that my join statement was incorrect for the masCat join, but if I just change tblItem.item_mcat_id to just tblItem.item_cat_id (which is the secondary category, also selected in this same query), it works just fine. Both are being pulled from the exact same table, but by different associated fields in the relational table. I am positive tblItem.item_mcat_id is the correct name of the field for the master category id.
Now I am used to using MySQL, and I do know that with SQL you are supposed to do a Group By as well. But I do not know what I would group by other than tblItem.item_id so that it does not combine rows of data. So I don’t see how that would solve my issue either. Can anyone see anything wrong with this SQL statement? Everything returns fine except for the master category.
I got interrupted several times while writing this, so I kind of lost track of my thinking during it, so I hope I gave enough information. If not, just ask, and I will respond or update my question. Thanks in advance…
EDIT
It’s in the comments, but the sqlQuery variable has a default value of “tblItem.item_id > 0”. It works if I add other criteria to it dynamically (not including the master category still).
Also, I must not have stated something right, but I do receive results from this. I am only missing the master category field (it’s returning blank for this one). The query itself is not failing.
FIXED
Whoever created the tables originally made the tblItem.item_mcat_id zero pad… -.-
Based on the description you’ve given, I’d have to say that these two columns:
Are not the same type or one contains padding where the other does not. You might have to do a cast. Finally MAKE SURE that the item.mcat_id DOES ACTUALLY exist in the cat_id column.