Sir,
Please help me.
I am in a function to send sms in c# windows application.
I want to count this string occurance and update the sms send phone number.
Please Provide me the code to count string occurance.
Here the function:-
private void frmSMS_Load(object sender, EventArgs e)
{
SqlDataReader rdr = null;
string Phone = string.Empty;
string Msg = string.Empty;
string userID = "[email protected]";
string Pswrd = "sms12345";
try
{
string cnn = ConfigurationManager.ConnectionStrings["MSCon"].ToString();
SqlConnection cn = new SqlConnection(cnn);
cn.Open();
SqlCommand cmd = new SqlCommand("Get_PatientPhoneNo", cn);
cmd.CommandType = CommandType.StoredProcedure;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Phone = rdr["PHONE"].ToString();
Msg = rdr["COMMENTS"].ToString();
string strCount=SendSMSUsingBS(userID, Pswrd, Phone, Msg);
if (strCount == "1")
{
string cnn1 = ConfigurationManager.ConnectionStrings["MSCon"].ToString();
SqlConnection cn1 = new SqlConnection(cnn);
cn.Open();
SqlCommand cmd1 = new SqlCommand("Update_Sms_Status", cn1);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex + "SMS01", "SMS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Loading

Nathan RPosted Mar 13, 2014, 6:53 AM
protected string SendSMSUsingBS(string strUser, string strPassword, string strRecip, string strMsgText)
{
try
{
string strUrl = null;
strUrl = "http://SMSnMMS.co.in/sms.aspx/";
strUrl += "?ID=" + strUser + "&Pwd=" + strPassword + "&PhNo=" + strRecip + "&Text=" + strMsgText;
Uri objURI = new Uri(strUrl);
WebRequest objWebRequest = WebRequest.Create(objURI);
WebResponse objWebResponse = objWebRequest.GetResponse();
Stream objStream = objWebResponse.GetResponseStream();
StreamReader objStreamReader = new StreamReader(objStream);
string strHTML = objStreamReader.ReadToEnd();
return "1";
}
catch (Exception ex)
{
MessageBox.Show(ex + "SMS03", "SMS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return "0";
}
}
And in the Event:-
private void frmSMS_Load(object sender, EventArgs e)
{
SqlDataReader rdr = null;
string Phone = string.Empty;
string Msg = string.Empty;
string userID = "[email protected]";
string Pswrd = "sms12345";
try
{
string cnn = ConfigurationManager.ConnectionStrings["MSCon"].ToString();
SqlConnection cn = new SqlConnection(cnn);
cn.Open();
SqlCommand cmd = new SqlCommand("Get_PatientPhoneNo", cn);
cmd.CommandType = CommandType.StoredProcedure;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Phone = rdr["PHONE"].ToString();
Msg = rdr["COMMENTS"].ToString();
string strCount=SendSMSUsingBS(userID, Pswrd, Phone, Msg);
if (strCount == "1")
{
string cnn1 = ConfigurationManager.ConnectionStrings["MSCon"].ToString();
SqlConnection cn1 = new SqlConnection(cnn);
cn.Open();
SqlCommand cmd1 = new SqlCommand("Update_Sms_Status", cn1);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex + "SMS01", "SMS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Thank you for the help from your end.
Brijendra GautamPosted Mar 13, 2014, 5:59 AM
We can use Regular Expressions for matching strings and find matched count like this:
string SampleString = "This is test string. This is test string. This is a test string.";
string MatchToString = "This is test";
Regex objReg = new Regex(MatchToString);
if (objReg.IsMatch(SampleString))
{
MessageBox.Show("Success: " + objReg.Matches(SampleString).Count.ToString());
}
Hope this is what you are looking for.