These lines of code have some useful things:
public List<string> Authenticate(Login objlog)
{
List<string> employee = new List<string>();
DataTable dt = new DataTable("Table1");
using (SqlConnection con = new SqlConnection(connection()))
{
using (SqlCommand cmd = new SqlCommand("spValidate_LoginUser", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserName", objlog.UserName);
cmd.Parameters.AddWithValue("@Password", objlog.Password);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
try
{
con.Open();
da.Fill(dt);
}
catch{
}
finally{if (con.State != ConnectionState.Closed) con.Close();}
}
}
}
if (dt.Rows.Count > 0)
{
foreach(DataRow dr in dt.Rows)
{
string name = dr[0] != DBNull.Value ? dr[0].ToString() : string.Empty;
string id = dr[1] != DBNull.Value ? dr[1].ToString() : string.Empty;
string type = dr[2] != DBNull.Value ? dr[2].ToString() : string.Empty;
string branch = dr[3] != DBNull.Value ? dr[3].ToString() : string.Empty;
employee.Add(name);
employee.Add(id);
employee.Add(type);
employee.Add(branch);
}
}
return employee;
}
</string></string></string>
Using: To dispose object after its use.
Try/Catch: Only needed when access to DataBase.
DataTable: Less heavy that DataSet.
foreach: To avoid extra calculations ds.Table.Rows[0][0].ToString() is not the very good form to do it.
DBNull: To avoid errors when no data returns.
I'm not agree with List<string>, is better to create a Employee class to parse with.
Hope it helps in any way.