User-1034726716 posted
This one uses the traditional ado.net but I hope this will give you an idea:
private void UpdateInfo(sting strValue1, int region)
{
SqlConnection conn = new SqlConnection("YOUR CONNECTION STRING");
string sql = "UPDATE TableName SET ColumnName1 = @val1 WHERE RegionID = @region";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Val1", strValue1);
cmd.Parameters.AddWithValue("@region", region);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
//catch exception here
}
finally
{
conn.Close();
}
}
private void InsertInfo(string strValue1, string strValue2, string strValue3)
{
SqlConnection conn = new SqlConnection("YOUR CONNECTION STRING");
string sql = "INSERT INTO TableName (ColumnName1,ColumnName2,ColumnName3) VALUES (@Val1,@Val2,@Val3)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Val1", strValue1);
cmd.Parameters.AddWithValue("@Val2", strValue2);
cmd.Parameters.AddWithValue("@Val3", strValue3);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
//catch exception here
}
finally
{
conn.Close();
}
}
private void CheckIfExist(int region)
{
SqlConnection conn = new SqlConnection("Your Connection String");
DataTable dt = new DataTable();
try
{
conn.Open();
String sql = "SELECT * FROM region WHERE RegionID = @region";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@region",region);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(dt);
if (dt.Rows.Count > 0)
{
Label1.Text = "REGION ID Exist!";
//CALL the UPDATE method here and pass its parameter values
UpdateInfo("Value1",region);
}
else
{
Label1.Text = "REGION ID NOT FOUND!";
//CALL the INSERT method here and pass its parameter values
InsertInfo("value1","Value2","Value3");
}
}
catch (System.Data.SqlClient.SqlException ex)
{
//catch exception here
}
finally
{
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string region = 10;
CheckIfExist(region);
}