Skip to content
Advertisement

add sql WHERE statement to select new ID:s in database (asp / sql)

I’m trying to update a website where there’s new ID’s for different damage groups. All the new ID’s start after ID 139. In SQL the statement to get the new groups are simply:

SELECT * 
FROM DamageCode 
WHERE ID >= 139

However, I want to implement the same statement in my ASP code and in the method that fetches the current damage groups. Here’s the current method for it:

public List<DamageCode> GetDamageCodes(int codeId) 
{
    var list = new List<DamageCode>();
    var cmd = new SqlCommand("spDamage_GetDamageCodeComponents", Connection);

    try 
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@CodeId", codeId);

        var dt = new DataTable();
        dt.Load(cmd.ExecuteReader());

        if (dt.Rows.Count > 0) 
        {
            foreach(DataRow i in dt.Rows) 
            {
                var model = new DamageCode();
                model.DamageCodeId = Convert.ToInt32(i["ID"]);
                model.DamageCodeName = i["Swedish"].ToString(); // DamageCode WHERE ID >= 139
                list.Add(model);
            }
        }
    } 
    catch (SqlException ex) 
    {
        throw new Exception(ex.Message);
    } 
    finally 
    {
        Connection.Close();
    }

    return list;
}

Basically: I want to update this method and add some a statement similar to the SQL statement, so that i["ID"] only will fetch ID’s after number 139.

Any ideas on how to do this directly in this method?

I’ve tried adding a new sql similar to:

string query = 'SELECT * FROM DamageCode WHERE ID >= 139';

But, I am unsure where to use the query later in the method.

Advertisement

Answer

The solution was to change to stored procedure

"spDamage_GetDamageCodeComponents"

in the database and alter the procedure to only show ID where ID >= 139 , this is much simpler than to change the whole method

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement