Wednesday, 5 August 2015

How to change the CSS class for a div in code-behind?

If your div has runat="server" then you directly use this:

divId.Attributes["class"] = "classname";

How to check atleast one checkbox is checked in asp.net ?

if (!chkListQualification.Items.Cast<ListItem>().Any(i => i.Selected))
{
  lblMessage.Text = "Please select at least one qualification.";
  return ;
}

Thursday, 23 July 2015

Difference between IDENT_CURRENT, @@IDENTITY, SCOPE_IDENTITY in sqlserver ?

  • IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.
  • @@IDENTITY returns the last identity value generated for any table in the current session, across all scopes.
  • SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.

Thursday, 2 July 2015

How to use Literal and Placeholder in asp.net ?

Design Page :-
       <div>
            <fieldset>
                <legend>Using Placeholder & Literals</legend>
                <asp:PlaceHolder ID="PlaceHolder1" runat="server" />
            <br />
                <asp:Literal ID="literalDemo" runat="server" />
            </fieldset>
</div>

Coding Page : -

Make class like this : -

        private static DataTable GetData()
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(Common.Global.myconstring))
            {
                SqlDataAdapter adp = new SqlDataAdapter("GetData", con); //call procedure
                adp.Fill(dt);
            }
            return dt;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = GetData();
            StringBuilder sb = new StringBuilder();
            sb.Append("<table>");
            sb.Append("<tr>");
            foreach (DataColumn col in dt.Columns)
            {
                sb.Append("<th>");
                sb.Append(col.ColumnName);
                sb.Append("</th>");
            }
            sb.Append("</tr>");
            foreach(DataRow dr in dt.Rows)
            {
                sb.Append("<tr>");
                foreach(DataColumn col in dt.Columns)
                {
                    sb.Append("<td>");
                    sb.Append(dr[col.ColumnName]);
                    sb.Append("</td>");
                }
                
                sb.Append("</tr>");
            }
            sb.Append("</html>");
            literalDemo.Text = sb.ToString();

            // you can also use this if you take placeholder in design page.
            PlaceHolder1.Controls.Add(new Literal { Text = sb.ToString() });
        }

Output:-


Thursday, 25 June 2015

Can't find [WebInvoke] and [WebGet]

Need to add the reference System.ServiceModel.Web.dll and import the namespace System.ServiceModel.Web by using below statement using System.ServiceModel.Web; Then it will work.


[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]


[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Save")]



Wednesday, 24 June 2015

what is an array in c#?


Design Page : -
  <asp:Label ID="lblValue" runat="server" ForeColor="Red" />

Code Behind : -
// Multi dimensional Array
            string[] arr = new string[] { "abc", "def", "ghi", "jkl" };
            int[] num1 = new int[] { 0, 1, 2, 6, 6, 7, 4, 4, 4, 3 };

            lblValue.Text = arr[0] + " " + arr[1] + " " + arr[2] + " " + arr[3];

            // Single dimensional Array
            int[] num = new int[10] { 0, 1, 2, 6, 6, 7, 4, 4, 4, 3 };
            string[] j = new string[5] { "a", "b", "c", "d", "e" };
            lblValue.Text = Convert.ToString(abc(num));


            string[,] h = new string[3, 2] { { "Deepak", "Sharma" }, { "Hemal", "Kumar" }, { "Mahesh", "Yadav" } };

            lblValue.Text = h[0, 0] + " " + h[0, 1] + " " + h[1, 0] + " " + h[1, 1] + " " + h[2, 0] + " " + h[2, 1];

Mehod : -
  private int abc(int[] a)
        {
            return a[5] * 2;
        }

Monday, 22 June 2015

How to bind country list in dropdown using xml ?

Make dropdownlist : -
<asp:DropDownList ID="ddlCountry" runat="server" />

Make Country.xml : -
<?xml version="1.0" encoding="utf-8" ?>

  <CountryList>
    <country code="Afghanistan" iso="4"/>
    <country code="Albania" iso="8"/>
    <country code="Algeria" iso="12"/>
    <country code="Angola" iso="24"/>
    <country code="Anguilla" iso="660"></country>
    <country code="Antarctica" iso="10"></country>
    <country code="Antigua And Barbuda" iso="28"></country>
    <country code="Argentina" iso="32"></country>
    <country code="Armenia" iso="51"></country>
    <country code="Aruba" iso="533"></country>
  </CountryList>

Code- Behind:-
private void BindCountries()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(MapPath("Country.xml"));
            DataView dv = ds.Tables[0].DefaultView;
            dv.Sort = "code";
            ddlCountry.DataTextField = "code";
            ddlCountry.DataValueField = "iso";
            ddlCountry.DataSource = dv;
            ddlCountry.DataBind();
            ddlCountry.Items.Insert(0, new ListItem("-Select-", "0"));

        }