Click here to Skip to main content
15,075,944 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to insert entries in my database, i only managed to insert first entry. I am using LINQ and don't have any idea what I did wrong:

here is my code:

C#
User newUser = new User();

newUser.FirstName = firstname;
newUser.LastName = lastname;
newUser.UserName = username;
newUser.Email = email;
newUser.Cell = cellno;
newUser.Password = password;
newUser.UserLevel = 1;
newUser.Active = 1; 

see.Users.InsertOnSubmit(newUser);

try
{
    see.SubmitChanges();
}
catch
{
    return false;
}

int locid = registerLocation(latitude, longitude);  //same as the current method
int id = newUser.UserId;

  registerCustomer(id, locid);
}

and then this is registerCustomer Method():

C#
Customer cus = new Customer();
cus.DefaultLocation = locationId;
cus.UserId = userid;
see.Customers.InsertOnSubmit(cus);

try
{
    see.SubmitChanges();
}
catch
{
    return false;
}
Posted
Updated 23-Apr-15 13:18pm
v2
Comments
Sergey Alexandrovich Kryukov 23-Apr-15 18:04pm
   
Where, for goodness sake, you are trying to insert more than one? :-)
—SA
ZurdoDev 23-Apr-15 21:09pm
   
You haven't actually shown any of the code that does the insert. All you have to do is debug the code and you'll see what is happening.

1 solution

Hi Mathale ,

Its not understood that you are looping the code or this execute only once .
If its runs one time then its definitely single entry will inser into database .

Pelase find below code
C#
protected void Button1_Click(object sender, EventArgs e)
   {
       SaveCustomerInfo();
   }

   private void SaveCustomerInfo()
   {
       using (NorthwindDataContext context = new NorthwindDataContext())
       {
           //Create a new instance of the Customer object
           Customer cust = new Customer();
           //Add new values to each fields
           cust.CustomerID = TextBoxID.Text;
           cust.CompanyName = TextBoxCompanyName.Text;
           cust.ContactName = TextBoxContactName.Text;
           cust.ContactTitle = TextBoxContactTitle.Text;
           cust.Address = TextBoxAddress.Text;
           cust.City = TextBoxCity.Text;
           cust.Region = TextBoxRegion.Text;
           cust.PostalCode = TextBoxPostalCode.Text;
           cust.Country = TextBoxCountry.Text;

           //Insert the new Customer object
           context.Customers.InsertOnSubmit(cust);
           //Sumbit changes to the database
           context.SubmitChanges();

           //Display Message for successful operation
           LiteralMessage.Text = "<p style='color:Green;'>Information Successfully saved!</p>";
       }
   }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900