I am trying to improve my coding style and I understand that my coding style is terrible.
Could you suggest the WAYS of improving my function?
public void polulateSpecs(int itemID, List<neItem> coll)
{
Program p = new Program();
for (int i = 0; i < (coll.Count - 1); i++) //going over all Objects in the list
{
String CatName = coll[i].specCat.Trim(); //define the name of the category
String queryCategoryCheck = "SELECT ID FROM ATTRIBUTE_CATEGORY WHERE ATTRIBUTE_NAME = '" + CatName + "'"; //check if Cat Exist query
String queryCategoryInsert = "INSERT INTO ATTRIBUTE_CATEGORY(ATTRIBUTE_NAME) VALUES ('" + CatName + "')"; //insert new category query
int cdId; //Category ID holder
try
{
String CID = p.querySQLStringReturn(queryCategoryCheck); //get ID
cdId = int.Parse(CID); //parse ID to number
}
catch
{
cdId = 0; // if can't parse to number, set to 0
}
if (cdId == 0) //if value is 0, then no bran exist, therefore create one
{
p.insertSQL(queryCategoryInsert, "Insert New Category " + CatName); //perform insert operation
}
try //now Category should be in the database- get it's ID
{
String CID = p.querySQLStringReturn(queryCategoryCheck);
cdId = int.Parse(CID); //Category ID
}
catch { }
for (int c = 2; c < (coll[i].attributesList.Count); c += 2) //go over
{
String attname = coll[i].attributesList[c].Trim(); // name of the attribute
String attSpec = coll[i].attributesList[c - 1].Trim(); //description of the attribute
String queryAttributeCheck = "SELECT ID FROM ATTRIBUTE_LIST WHERE ATT_NAME = '" + attname + "' AND ATT_SPEC = '" + attSpec + "'"; //check if Attribute Exist query
String queryAttributeInsert = "INSERT INTO ATTRIBUTE_LIST(PARENT_CAT, ATT_NAME, ATT_SPEC) VALUES (" + cdId + ", '" + attname + "', '" + attSpec + "')"; //insert new category query
int aId; //Attribute ID holder
try
{
String AID = p.querySQLStringReturn(queryAttributeCheck); //get ID
aId = int.Parse(AID); //parse ID to number
}
catch
{
aId = 0; // if can't parse to number(does not exist), set it to 0
}
if (aId == 0) //if value is 0, then no bran exist, therefore create one
{
p.insertSQL(queryAttributeInsert, "Insert New Attribute Set " + attname); //perform insert operation
}
try //now Category should be in the database- get it's ID
{
String AID = p.querySQLStringReturn(queryAttributeCheck);
aId = int.Parse(AID); //Attribute ID
}
catch { }
}
//Add final record to database (Item ID and Corresponding Attribute category)
String queryProductToAttributeCheck = "SELECT ID FROM PRODUCT_TO_ATT_CATEGORY WHERE PROD_ID = '" + itemID + "' AND ATT_CAT = '" + cdId + "'"; //check if Attribute Exist query
String queryProductToAttributeInsert = "INSERT INTO PRODUCT_TO_ATT_CATEGORY(PROD_ID, ATT_CAT) VALUES (" + itemID + ", " + cdId + ")"; //insert new category query
int ptaID; //Attribute ID holder
try
{
String PTAID = p.querySQLStringReturn(queryProductToAttributeCheck); //get ID
ptaID = int.Parse(PTAID); //parse ID to number
}
catch
{
// if can't parse to number(does not exist), insert it
ptaID = 0;
}
p.insertSQL(queryProductToAttributeInsert, "Insert Product to Attribute Mapping " + itemID);
}
}
First of all, stop the empty
catchblocks for parsing. If you really want to throw away errors, useTryParseinstead. Next, remove unnecessary parentheses. Also cut down on the superfluous comments, like “perform insert operation” (the code makes it obvious). So: