sqlc#-4.0asp.net-4.0

How to avoid every time initialization when value have greater than 0


I have a method that inserts a new record after checking whether it already exists or not.

Here is my method:

protected void btn_save_Click(object sender, EventArgs e)
{
    string MobileNo = "";
    string replaceValue = txt_mobile.Text.Replace(Environment.NewLine, "$");

    string[] values = replaceValue.Split('$');
    int uCnt = 0;
    int sCnt = 0;

    foreach (string item in values)
    {
        SaveRecord(item.Trim(),out MobileNo,out uCnt,out sCnt);
    }

    txt_mobile.Text = string.Empty;

    if(uCnt > 0)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "BulkSMS System", "alert('Mobile No(s) : "+MobileNo.TrimEnd(',')+" Already Exist');", true);
    }

    if(sCnt > 0)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "BulkSMS System", "alert('" + sCnt + " Record(s) Inserted Successfully');", true);
    }

    Get_Data();
}

public void SaveRecord(string value, out string MobileNo, out int uCnt, out int sCnt)
{
    uCnt = 0; //every time initialized to 0
    sCnt = 0; //every time initialized to 0 
    MobileNo = "";

    try
    {
        DataTable dt = new DataTable();
        var dot = Regex.Match(value, @"\+?[0-9]{10}");

        if (dot.Success)
        {
            string str = "SELECT TOP 1 [ID],[MobileNo] FROM[dbo].[whitelistdata]";
            str += " WHERE [UserID] = '" + Convert.ToInt32(ddl_users.SelectedValue.ToString()) + "' AND [SenderId] = '" + Convert.ToInt32(ddl_senders.SelectedValue.ToString()) + "' AND [MobileNo] = '" + value + "'";
            dt = obj.Get_Data_Table_From_Str(str);

            if (dt.Rows.Count > 0)
            {
                uCnt++;
                MobileNo += value + ",";
            }
            else
            {
                string str1 = "INSERT INTO [dbo].[whitelistdata]([UserID],[SenderId],[KeywordID],[MobileNo])";
                str1 += "VALUES (" + Convert.ToInt32(ddl_users.SelectedValue.ToString()) + "," + Convert.ToInt32(ddl_senders.SelectedValue.ToString()) + ",1," + value + ")";
                obj.Execute_Query(str1);
                sCnt++;
            }
        }
    }
    catch (Exception ex)
    {
        CommonLogic.SendMailOnError(ex);
        ClientScript.RegisterStartupScript(this.GetType(), "BulkSMS System", "alert('" + ex.Message.ToString() + "');", true);
    }
}

The problem is every time it's set to 0 when method has been called I want to prevent them when previous value is greater than 0.

Please help me guys..


Solution

  • Please first identify which combination check-in database.

    if UserID AND SenderId combination Match Then

     string str = "SELECT TOP 1 [ID],[MobileNo] FROM[dbo].[whitelistdata]";
     str += " WHERE [UserID] = '" + Convert.ToInt32(ddl_users.SelectedValue.ToString()) + "' AND [SenderId] = '" + Convert.ToInt32(ddl_senders.SelectedValue.ToString()) + "'";
    

    if check the only UserID Match Then

     string str = "SELECT TOP 1 [ID],[MobileNo] FROM[dbo].[whitelistdata]";
     str += " WHERE [UserID] = '" + 
         Convert.ToInt32(ddl_users.SelectedValue.ToString()) +"'";
    

    if UserID OR SenderId combination Match Then

     string str = "SELECT TOP 1 [ID],[MobileNo] FROM[dbo].[whitelistdata]";
     str += " WHERE [UserID] = '" + Convert.ToInt32(ddl_users.SelectedValue.ToString()) + "' OR [SenderId] = '" + Convert.ToInt32(ddl_senders.SelectedValue.ToString()) + "'";
    

    if UserID AND SenderId AND MobileNo combination Match Then

    string str = "SELECT TOP 1 [ID],[MobileNo] FROM[dbo].[whitelistdata]";
    str += " WHERE [UserID] = '" + Convert.ToInt32(ddl_users.SelectedValue.ToString()) + "' AND [SenderId] = '" + Convert.ToInt32(ddl_senders.SelectedValue.ToString()) + "' AND [MobileNo] = '" + value + "'";