Wednesday, November 24, 2010

LINQ to SQL : Inserting Data to Database using


<>

   Company ID < / td>

    < id = "  TextBoxID " runat = " s e r v e r ">

  < /tr>



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 = "

Information Successfully saved!

";

    }


   }

No comments: