Advertisement
Advertisement
| 06.15.2008 at 11:29PM PDT, ID: 23487237 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: |
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlConnection conNwind;
SqlCommand cmdSelect, cmdSelectID;
SqlDataReader dtrCustomer;
string connStr = ConfigurationManager.ConnectionStrings["ConnectionNWind"].ConnectionString;
conNwind = new SqlConnection(connStr);
conNwind.Open();
string select = "Select * from Customers where CustomerID=@ID";
cmdSelectID = new SqlCommand(select, conNwind);
cmdSelectID.Parameters.AddWithValue("@ID", txtId.Text);
dtrCustomer = cmdSelectID.ExecuteReader();
if (dtrCustomer.Read())
{
Label1.Text = "ID already exist!";
}
else
{
string strInsert = "Insert into Customers(CustomerID,Companyname,Contactname,Phone) values(@customerId,@companyname,@contactname,@phone)";
cmdSelect = new SqlCommand(strInsert, conNwind);
cmdSelect.Parameters.AddWithValue("@customerId", txtId.Text);
cmdSelect.Parameters.AddWithValue("@companyname", txtCompanyName.Text);
cmdSelect.Parameters.AddWithValue("@contactname", txtContactName.Text);
cmdSelect.Parameters.AddWithValue("@phone", txtPhone.Text);
int intAdd = cmdSelect.ExecuteNonQuery();
if (intAdd != 0)
Label1.Text = "Record successfully added into database!";
else
Label1.Text = "Record not added into database!";
}
dtrCustomer.Close();
conNwind.Close();
}
|