|
-
October 2nd, 2003, 03:29 AM
#1
Constructor to cancel object creation
Hi,
I have a constructor like the one below that searches the DB for a company with the id and fills the object properties if the record is found. The problem is that if the record is not found the object is still created with empty properties. I want to change this so that if no company is found the object is not created at all.
Also, if a null value(for example) is returned when the company is not found how would I handle this from my calling code?
Any help would be great.
Cheers
public Company(int id)
{
try
{
SqlConnection conn = new SqlConnection(ConnString);
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from Company_Branch where BranchID = @BranchID",conn);
cmd.Connection = conn;
cmd.Parameters.Add(new SqlParameter("@BranchID",DbType.Int16));
cmd.Parameters["@BranchID"].Value = id;
SqlDataReader dr = null;
dr = cmd.ExecuteReader();
if (dr.Read())
{
//load object properties from data reader
}
conn.Close();
dr.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|