Hi Celv,
I have created a sample that fullfill your requirement
C#
string constr = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
SqlConnection con = new SqlConnection();
protected void Save(object sender, EventArgs e)
{
string query = @"Insert INTO Customers VALUES('" + txtName.Text.Trim() + "','" + txtCountry.Text.Trim() + "')";
con = new SqlConnection(constr);
con.Open();
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.ExecuteNonQuery();
}
con.Close();
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void Update(object sender, EventArgs e)
{
if (btnEdit.Text == "Edit")
{
btnEdit.Text = "Update";
}
else
{
btnEdit.Text = "Edit";
}
if (!string.IsNullOrEmpty(txtName.Text.ToString()) && !string.IsNullOrEmpty(txtCountry.Text.ToString()))
{
con = new SqlConnection(constr);
string query = @"Update Customers Set Name = '" + txtName.Text.Trim() + "'," + "Country = '" + txtCountry.Text.Trim() + "' WHERE CustomerId = " + litId.Text.Trim();
con.Open();
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.ExecuteNonQuery();
}
con.Close();
Response.Redirect(Request.Url.AbsoluteUri);
}
else
{
BindCustomer();
}
}
private void BindCustomer()
{
string query = "SELECT CustomerId,Name,Country FROM Customers WHERE CustomerId = (SELECT MAX(CustomerId) FROM Customers)";
con = new SqlConnection(constr);
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
litId.Text = sdr["CustomerId"].ToString();
txtName.Text = sdr["Name"].ToString();
txtCountry.Text = sdr["Country"].ToString();
}
con.Close();
}
}
ScreenShot
