protected void btnCustomerID_Click(object sender, EventArgs e)
{
Customer objCustomer = new Customer();
objCustomer.Insert(tbxCustomerID.Text.Trim(),tbxCompanyName.Text.Trim(),tbxContactName.Text.Trim(),
tbxContactTitle.Text.Trim(), tbxAddress.Text.Trim(),tbxCity.Text.Trim(),tbxRegion.Text.Trim(),
tbxPostalCode.Text.Trim(),tbxCountry.Text.Trim(),tbxPhone.Text.Trim(),tbxFax.Text.Trim());
}
This is the Insert Method in a seperate Class
public bool Insert(string CustomerID, string CompanyName, string ContactName,string ContactTitle, string Address, string City, string Region, string PostalCode, string Country, string Phone, string Fax)
{
SqlCommand objCommand = new SqlCommand();
SqlConnection conn = new SqlConnection("server=sheeban\\sqlexpress;integratedsecurity=SSPI;database=northwind");
SqlParameter objParameter = new SqlParameter();
try
{
string strSql = "INSERT INTO Customers(CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, " +
" PostalCode, Country, Phone,Fax ) VALUES (@CustomerID, @CompanyName,@ContactName, @ContactTitle, @Address, @City, @Region," +
" @PostalCode, @Country, @Phone, @Fax)";
objCommand.CommandText = strSql;
objCommand.Connection = conn;
objParameter = new SqlParameter("@CustomerID", CustomerID);
objCommand.Parameters.Add(objParameter);
objParameter = new SqlParameter("@CompanyName", CompanyName);
objCommand.Parameters.Add(objParameter);
objParameter = new SqlParameter("@ContactName", ContactName);
objCommand.Parameters.Add(objParameter);
objParameter = new SqlParameter("@ContactTitle", ContactTitle);
objCommand.Parameters.Add(objParameter);
objParameter = new SqlParameter("@Address", Address);
objCommand.Parameters.Add(objParameter);
objParameter = new SqlParameter("@City", City);
objCommand.Parameters.Add(objParameter);
objParameter = new SqlParameter("@Region", Region);
objCommand.Parameters.Add(objParameter);
objParameter = new SqlParameter("@PostalCode", PostalCode);
objCommand.Parameters.Add(objParameter);
objParameter = new SqlParameter("@Country", Country);
objCommand.Parameters.Add(objParameter);
objParameter = new SqlParameter("@Phone", Phone);
objCommand.Parameters.Add(objParameter);
objParameter = new SqlParameter("@Fax", Fax);
objCommand.Parameters.Add(objParameter);
objCommand.Connection.Open();
return (objCommand.ExecuteNonQuery() > 0 ? true : false);
}
catch (Exception ex)
{
conn.Close();
return false;
}
finally
{
conn.Close();
}
No comments:
Post a Comment