Bind Dropdown from data table in asp.net

   Step 1 -Creating Table in SQL Server Database


create a table named MstUser with the columns id, name, country and city. Set the identity property=true for id.

Step 2- insert data into ‘MstUser’ Table

Like this:-



Step 3 -Now You have to Create a WebApplication
  1.               Go to Visual Studio 2015
  2.          New Project ->Web->Asp.net Web Application->-Web Forms>Click Ok






Step 4-Now Add Web Page
  1. ·         Go to the Solution Explorer
  2. ·         Right –Click on the Project Name
  3. ·         Select  ’add New Item’ option
  4. ·         Add new  Web Form from Items and give Name
  5. ·         Click OK



Step 5-Design the page and place the required control on it.
Now drag and drop one DropDownList control, One Button and  one GridView control on the form from toolbox . Like given bellow .




Step 6-Now add the following namespaces:
  1.  using System.Data.SqlClient;
  2.   using System.Data;

Step 7-Now write the connection string to connect to the database:
string str = "Data Source=RND-PCTPX04\\SQLEXPRESS_2014;Initial Catalog=DemoDb;Integrated Security=True";


Step 8-Now bind the DropDownList with the database table.

            Go to cs file of web form and write this code on page load event
protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(str);
        string com = "Select * from MstUser";
        SqlDataAdapter adpt = new SqlDataAdapter(com, con);
        DataTable dt = new DataTable();
        adpt.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataBind();
        DropDownList1.DataTextField = "UserName";
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataBind();
 
    }

Step 9-Now double-click on the Button control and add the following code:
          SqlConnection con = new SqlConnection(str);
          SqlCommand cmd = new SqlCommand("select * from MstUser where id = '" + Drop    DownList1.SelectedValue + "'", con);
          SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
          DataTable dt = new DataTable();
          Adpt.Fill(dt);
          GridView1.DataSource = dt;
          GridView1.DataBind();
          Label1.Text = "record found";
      


Step 10-Code-behind file
To display data in The GridViw use a DataAdapter object to retrieve the data from the database and place that data into a table.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace Demo
{
    public partial class ShowDropdown : System.Web.UI.Page
    {
        string str = "Data Source=RND-PCTPX04\\SQLEXPRESS_2014;Initial Catalog=DemoDb;Integrated Security=True";
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(str);
            string com = "Select * from MstUser";
            SqlDataAdapter adpt = new SqlDataAdapter(com, con);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
            DropDownList1.DataSource = dt;
            DropDownList1.DataBind();
            DropDownList1.DataTextField = "UserName";
            DropDownList1.DataValueField = "ID";
            DropDownList1.DataBind();

        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(str);
            SqlCommand cmd = new SqlCommand("select * from MstUser where id = '" + DropDownList1.SelectedValue + "'", con);
            SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            Adpt.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            Label1.Text = "record found";
        }
    }
}


Step 11-Now run the application  
You will get desired result means you dropdown list load with data from database

Step 12-Now select the User name from DropDownList control and click on the Button to se all details of user .




Export Data table into ms Excel file in asp.net

      private void FnExportDataIntoExcel()
                  {
                                       // get data from Database
                                                   dtDataToExport = objDBClass.GetDataFunction(objParam);   // call Data access layer function to get data from Database to export
   
                                                                if (dtDataToExport != null
                       && dtDataToExport.Rows.Count > 0)
                {
                    DataTable dataTable = dtDataToExport;
                    //File Name
                    string DT = DateTime.Now.ToString("yyyyMMdd_hhmmsssstt");
                    // Set up the Response object...
                    this.Response.BufferOutput = false;
                    this.Response.Charset = string.Empty;
                    this.Response.Clear();
                    this.Response.ClearContent();
                    this.Response.ClearHeaders();

                    this.Response.ContentType = "application/vnd.ms-excel";

                    // Open the document OVER this window:
                    string contentDisposition = "attachment; filename=" + "SalesReport_GST" + "_" + DT + ".xls";
                    this.Response.AppendHeader("content-disposition", contentDisposition);

                    // Create a table HTML tag...
                    string textOutput =
                    "<table border='1' bordercolor='black' rules='all' " +
                    "style='BORDER: 1px double; BORDER-COLLAPSE: collapse;'>\r\n";
                    this.Response.Write(textOutput);

                    // Write out the column headers...
                    textOutput = "<tr height=17 style='height:12.75pt'>\r\n";
                    for (int i = 0; i < dataTable.Columns.Count; i++)
                    {
                        textOutput += string.Format(
                        System.Globalization.CultureInfo.InvariantCulture,
                        "<th align='left'>{0}</th>",
                        dataTable.Columns[i].ColumnName);
                    }
                    textOutput += "</tr>\r\n";
                    this.Response.Write(textOutput);


                    // Write out each row of the DataTable...
                    textOutput = string.Empty;
                    for (int i = 0; i < dataTable.Rows.Count; i++)
                    {
                        System.Data.DataRow dataRow = dataTable.Rows[i];
                        textOutput = "<tr height=17 style='height:12.75pt'>\r\n";

                        // Loop through the columns for each row and grab the values...
                        for (int j = 0; j < dataTable.Columns.Count; j++)
                        {
                            string dataRowText = dataRow[j].ToString().Trim();

                            // Remove any tabs, carriage returns, or newlines from the value...
                            dataRowText = dataRowText.Replace("\t", " ");
                            dataRowText = dataRowText.Replace("\r", string.Empty);
                            dataRowText = dataRowText.Replace("\n", " ");

                            textOutput += string.Format(
                            System.Globalization.CultureInfo.InvariantCulture,
                            "<td align='left' style='mso-number-format:\\@;'>{0}</td>",
                            dataRowText);
                        }

                        textOutput += "</tr>\r\n";
                        this.Response.Write(textOutput);
                    }

                    // Close the HTML table tag... 
                    textOutput = "</table>\r\n";
                    this.Response.Write(textOutput);

                    // Close the Response stream...
                    this.Response.Flush();
                    this.Response.Close();
                    //  this.Response.End();;
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.SuppressContent = true;
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                }
                else
                {
                                                                               
                                                                                // call your function to show message like this
                     string  msg = "No Records Found to export";
                    // bgcolor = "red";
                    //DisplayMessage(msg, bgcolor);
                }
                                }

jQuery blur()


jQuery blur()
The jQuery blur event occurs when element loses focus. It can be generated by via keyboard commands like tab key or mouse click anywhere on the Web page.

It makes you enable to attach a function to the event that will be executed when the element loses focus. Originally, this event was used only with form elements like <input>. In latest browsers, it has been extended to include all element types.

Syntax:
1.      $(selector).blur()  
It triggers the blur event for selected elements.
1.      $(selector).blur(function)  
It adds a function to the blur event.
Parameters of jQuery blur() 
Parameter
Description
Function
It is an optional parameter. It is used to specify the function to run when the element loses the focus (blur).

Example of jQuery blur()
Let's take an example to demonstrate jQuery blur() event.
<!DOCTYPE html>  
<html>  
<head>  
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
<script>  
$(document).ready(function(){  
    $("input").blur(function(){  
        alert("This text box has lost its focus.");  
    });  
});  
</script>  
</head>  
<body>  
Enter your name: <input type="text">  
</body>  
</html>