locked
need help on checking an existing data in a database RRS feed

  • Question

  • User-1974373719 posted
    I have this class that inserts data into a database and works fine. I am inserting the data through formview using objectdataasource.I need help on checking
    the existance of data before being inserted ie if data being inserted exist should bring a popup saying data exist.
    <System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, True)> _
        Public Function AddtblVessels(ByVal vesselName As String) As Boolean
    
            Dim vessels As New SAMI.tblVesselsDataTable()
            Dim vessel As SAMI.tblVesselsRow = vessels.NewtblVesselsRow()
    
            vessel.VesselName = vesselName
    
            vessels.AddtblVesselsRow(vessel)
            Dim rowsAffected As Integer = Adapter.Update(vessels)
            Return rowsAffected = 1
        End Function

    Tuesday, January 29, 2013 6:25 AM

Answers

  • 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);
    }
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 29, 2013 6:30 AM