Code Snippets

Count The Number Of Characters Occurrence In A String/ Word

 

Way 1


  1.  

Way 2


  1.  

Reversing The Words In Sentence Using ASP.NET C#

Way1: Using Array.Reverse() method


  1.  

Way2 : without using Array.Reverse() method


  1.  

Reverse String In ASP.NET C#

Way1: Using Array.Reverse() method


  1.  

Way2 : without using Array.Reverse() method


  1.  

 

 

Simple Web Service Method for Generating Country, State and City in Dropdown List

 

Introduction

In this code snippet, I have shared the real time code which was working fine in my project to fill the country state and city based on the dropdown list items selected . Attached the source code in attachment section.

Aspx code


  1.  

Web service methods,

  1. [WebService(Namespace = “http://tempuri.org/”)]
  2. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  3. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
  4. [System.Web.Script.Services.ScriptService]
  5. public class Address_WebService: System.Web.Services.WebService
  6. {
  7.     SqlConnection conn = new SqlConnection(OneStopMethods_Common.constring_Property);
  8.     [WebMethod]
  9. public CascadingDropDownNameValue[] BindCountry(string knownCategoryValues, string category)
  10.     {
  11.         DataSet ds = new DataSet();
  12.         conn.Open();
  13.         SqlCommand cmd = new SqlCommand(“select * from SelectCountry”, conn);
  14.         SqlDataAdapter adp = new SqlDataAdapter(cmd);
  15.         cmd.ExecuteNonQuery();
  16.         adp.Fill(ds);
  17.         conn.Close();
  18.         List < CascadingDropDownNameValue > CountryDetails = new List < CascadingDropDownNameValue > ();
  19. foreach(DataRow DR in ds.Tables[0].Rows)
  20.         {
  21. string CountryID = DR[“CountryId”].ToString();
  22. string CountryName = DR[“County”].ToString();
  23.             CountryDetails.Add(new CascadingDropDownNameValue(CountryName, CountryID));
  24.         }
  25. return CountryDetails.ToArray();
  26.     }
  27. //Web method for bind state
  28.     [WebMethod]
  29. public CascadingDropDownNameValue[] BindState(string knownCategoryValues, string category)
  30.     {
  31.         DataSet ds = new DataSet();
  32. int CountryID;
  33.         StringDictionary CountryDetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
  34.         CountryID = Convert.ToInt32(CountryDetails[“Country”]);
  35.         conn.Open();
  36.         SqlCommand cmd = new SqlCommand(“select * from SelectCountrySelectState where CountryId=@CountryID”, conn);
  37.         cmd.Parameters.AddWithValue(“@CountryId”, CountryID);
  38.         cmd.ExecuteNonQuery();
  39.         SqlDataAdapter adp = new SqlDataAdapter(cmd);
  40.         adp.Fill(ds);
  41.         conn.Close();
  42.         List < CascadingDropDownNameValue > StateDetails = new List < CascadingDropDownNameValue > ();
  43. foreach(DataRow DR in ds.Tables[0].Rows)
  44.         {
  45. string stateID = DR[“StateId”].ToString();
  46. string statename = DR[“State”].ToString();
  47.             StateDetails.Add(new CascadingDropDownNameValue(statename, stateID));
  48.         }
  49. return StateDetails.ToArray();
  50.     }
  51. //Web method for bind city
  52.     [WebMethod]
  53. public CascadingDropDownNameValue[] BindCity(string knownCategoryValues, string category)
  54.     {
  55.         DataSet ds = new DataSet();
  56. int stateID;
  57.         StringDictionary statedetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
  58.         stateID = Convert.ToInt32(statedetails[“State”]);
  59.         conn.Open();
  60.         SqlCommand cmd = new SqlCommand(“Select * from SelectStateSelectCity where StateId= “ + stateID, conn);
  61. // cmd.Parameters.AddWithValue(“@StateID”, StateId)
  62.         cmd.ExecuteNonQuery();
  63.         SqlDataAdapter adp = new SqlDataAdapter(cmd);
  64.         adp.Fill(ds);
  65.         conn.Close();
  66.         List < CascadingDropDownNameValue > CityDetails = new List < CascadingDropDownNameValue > ();
  67. foreach(DataRow DR in ds.Tables[0].Rows)
  68.         {
  69. string CityID = DR[“CityID”].ToString();
  70. string City = DR[“City”].ToString();
  71.             CityDetails.Add(new CascadingDropDownNameValue(City, CityID));
  72.         }
  73. return CityDetails.ToArray();
  74.     }
  75. }

Use AutoComplete Extender in C#

 


  1.  

Webservice code

  1. [WebMethod]
  2. public static List<string> SearchCustomers(string prefixText, int count)
  3. {
  4. using (SqlConnection conn = new SqlConnection())
  5.     {
  6.         conn.ConnectionString = ConfigurationManager
  7.                 .ConnectionStrings[“Webconfig_Con”].ConnectionString;
  8. using (SqlCommand cmd = new SqlCommand())
  9.         {
  10.             cmd.CommandText = “select * from tblCustomer where CompanyName like ‘%” + prefixText + “%’ “;
  11.             cmd.Connection = conn;
  12.             conn.Open();
  13.             List<string> customers = new List<string>();
  14. using (SqlDataReader sdr = cmd.ExecuteReader())
  15.             {
  16. while (sdr.Read())
  17.                 {
  18.                     customers.Add(sdr[“CompanyName”].ToString());
  19.                 }
  20.             }
  21.             conn.Close();
  22. return customers;
  23.         }
  24.     }
  25. }

How to Get the Cell Value From the Cell Formula Type in OpenXML Spreadsheet Document

Introduction

Today I worked in the openxml for reading excel and I got all values as expected but i couldn’t find the value of the Cell which contains the formula. I searched lot in Google and found the following solution finally .Here Sharing the same for others.


  1.  

Hope the above code will be useful ,kindly let me know your thougths.

Resolve the Error Object cannot be cast from DBNull to other Types

 

Reason for the error

In an object-oriented programming language, null means the absence of a reference to an object. DBNull represents an uninitialized variant or nonexistent database column. Source: MSDN.

Actual Code which I faced error

Before changed the code,


  1.  

After changed the code,


  1.  

Conclusion

When the database value return the null value, we recommend to use the DBNull class instead of just specifying as a null like in C# language.

 

Insert Bulk Data in SQL Server Table from any Data Source using C#

 

Background

Few days back , I got a requirement from client says that ,they had plenty of useful excel files and they want to store that in Sql server and view it in a grid view. Each and every excel files had more than 1000 rows , and it I was very difficult to insert the data manually. So I decided to automate it through code and I wrote the below code.

C# Code


  1.  

Control all Panel Visibility with Just One Method

Introduction
Below Code will help you to set the panel  visibility based upon the parent control and Panel object ID. We just need to pass those two as a parameter to the method.
Background
Few days back,I got requirement to put many panel controls to the application.  So as on , the application grows by putting  more panel controls i faced the difficulty . Below i have mentioned the difficulty i have faced and Provided the solution i got.
What problem the below code solves? 
As we know , Panel control can be set to visible true or false as per requirement . And when compared to the View , two or more panels can be set to true whereas in view we could show only one at a time.But the problem is, once we set the required panel to true,it is also developer duty to set the other panels visible to false ,otherwise everything will show together.  So the code will be very large and it will take plenty of time for the developers. so i thought to get  solution for this and finally i wrote the below code:

  1.  
Hope the above was easily understandable.

Clear All the Text Box Value with Single Method

ntroduction
The below code will help you to identify all the child controls from the Parent control and do the action as we wish. Here i have used panel as a parent control and cleared all the text box control  value with one shot by using simple method.
Background
Few day back i got  a requirement to put plenty no of panels in webpage for different purpose ,and with in that panel controls we had several textboxes ,  so whenever we switch between the panel ,we should remove the already entered value and show the empty textbox to the client.   For that, instead of accessing all the textbox control separetly ,we can access through the for each loop together and perform the action.
Sample code

  1.  
Hope the above code will be useful.

463 total views, 2 views today