Hello,
I am new to .net. So plz help resolve my errors. I have two tables Customer and CustomerAddrress. The Customer ID in the Customer table is the primary key and the CustomerID in the CustomerAddress is a foreign key to the Customer table. I have to insert value into these tabes with one submit click. The CustomerID value from the Customer table should be fetched and then this should be used in CustomerAddress table… I am using asp along with LInq to Sql.
My code is as follows:
private void SaveCustomerInfo()
{
using (MyClassesDataContext context = new MyClassesDataContext())
{
Customer Cust = new Customer();
int Custid = (from c in context.Customers
select c.CustomerID).FirstOrDefault();
if (Custid == 0)
Custid++;
else
Custid = (from d in context.Customers
select d.CustomerID).Max()+1;
Cust.CustomerID = Custid;
Cust.FirstName = txtFname.Text;
Cust.LastName = txtLname.Text;
Cust.UserName = txtuser1.Text;
Cust.Email = txtemail.Text;
Cust.Password = txtpass1.Text;
Cust.Phone = txtPhone.Text;
context.Customers.InsertOnSubmit(Cust);
context.SubmitChanges();
CustomerAddress CustAdd = new CustomerAddress();
CustAdd.Street = txtstreet.Text;
CustAdd.City = txtcity.Text;
CustAdd.State = txtstate.Text;
CustAdd.Zip = Convert.ToInt32(txtzip.Text);
[B] Cust.CustomerAddresses = CustAdd;[/B]
context.CustomerAddresses.InsertOnSubmit(CustAdd);
context.SubmitChanges();
Response.Write("Successful");
}
But i get the error " cannot implicitly convert type ‘CustomerAddress’ to ‘System.data.Linq.EntitySet<Customerddress>’ for the lines marked in bold.
Please help resolve this error. I tried searching for a solution but nothing worked. Is the code wrong and do i have to maintain anything else.
Also, i have maintained autosync to OnInsert and auto generated value =true in both the customer IDs.