I'm using AdventureWorks as a database to get familiar with linq and asp.net interface and was wondering if anyone could take a look at my code behind in C# and give me a optimized way to get the results populated when searched and then updated when submitted.
protected void btnSearch_Click(object sender, EventArgs e)
{
TestDataClassDataContext dc = new TestDataClassDataContext();
Individual ind = new Individual();
var q = from Individual in dc.Individuals
where Individual.CustomerID == Convert.ToInt32(txtCustID.Text)
select Individual;
if (q.Count() > 0)
{
ind = q.First();
Contact con = new Contact();
var q2 = from Contact in dc.Contacts
where Contact.ContactID == ind.ContactID
select Contact;
if (q2.Count() > 0)
{
con = q2.First();
txtFname.Text = con.FirstName;
txtLname.Text = con.LastName;
txtMname.Text = con.MiddleName;
txtPhone.Text = con.Phone;
txtPword.Text = con.PasswordSalt;
txtSuff.Text = con.Suffix;
txtTitle.Text = con.Title;
txtEmail.Text = con.EmailAddress;
ddlEmailPromo.SelectedValue = con.EmailPromotion.ToString();
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
TestDataClassDataContext dc = new TestDataClassDataContext();
Individual ind = new Individual();
var q = from Individual in dc.Individuals
where Individual.CustomerID == Convert.ToInt32(txtCustID.Text)
select Individual;
if (q.Count() > 0)
{
ind = q.First();
Contact con = new Contact();
var q2 = from Contact in dc.Contacts
where Contact.ContactID == ind.ContactID
select Contact;
if (q2.Count() > 0)
{
con = q2.First();
con.FirstName = txtFname.Text;
con.LastName = txtLname.Text;
con.MiddleName = txtMname.Text;
con.Phone = txtPhone.Text;
con.PasswordSalt = txtPword.Text;
con.Suffix = txtSuff.Text;
con.Title = txtTitle.Text;
con.EmailAddress = txtEmail.Text ;
ddlEmailPromo.SelectedValue = con.EmailPromotion.ToString();
}
}