Advertisements

Archive

Archive for the ‘Csharp’ Category

How to access all types of server controls in ASP.Net using Javscript/JQuery OR Clear all server controls values using javascript or Jquery


How to access server side controls in ASP.Net using Javascript/JQuery

Using client side script such as JavaScript and Jquery, we can access all server side controls from ASPX page and it will get more performance on web application as it is not causing post back. While development we are frequently facing a feature to implement that “Clear All” function in the ASPX page. Suppose in a User Entry form, most probably there will be having a button for clear all values that user was trying to enter previously.

Get all textboxes, radio buttons and checkboxes  in an ASPX page from JavaScript

To access all server side textbox controls in an aspx page using JavaScript, we have to find all controls with tag name is input and type is text. Below mentioned code will access all textboxes in a aspx page from JavaScript and clear those values to empty. The type of the radio button is ‘radio’ and checkbox is ‘checkbox’.

        var elements = document.getElementsByTagName("input");
            for (var ii = 0; ii < elements.length; ii++) {
                if (elements[ii].disabled == false) {
                    if (elements[ii].type == "text") {
                        elements[ii].value = "";
                    }
                    if (elements[ii].type == "radio") {
                        elements[ii].checked = false;
                    }
                    if (elements[ii].type == "checkbox") {
                        elements[ii].checked = false;
                    }
                }
            }

Image

How to access dropdown list control in an ASPX Page using Javscript/JQuery

The tagname for the dropdownlist and listbox controls in an aspx page is ‘select’. We can access all dropdown list in a aspx page by checking tagname of each controls as same as below sample code.

var select = document.getElementsByTagName("select");
            var length = select.length;
            for (i = 0; i < length; i++) {
                for (var j = 0; j < select[i].length; j++) {
                    select[i][j].selected = false;
                }
            }

A simple example for accessing all server controls in an aspx page from javscript/jquery

We are demonstrating a sample web page containing different server controls like textbox, dropdownlist, checkbox, radiobutton, listbox etc.. and clearing all this values to default by using javscript/jquery. Here we are accessing all serverside controls without knowing their ids and names.

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”JqueryValidation.aspx.cs”

Inherits=”ExperimentLab.JqueryValidation” %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Clear all server controls using Javascript</title>
    <style type="text/css">
        h1
        {
            font-style: italic;
            font-weight: bold;
            color: Gray;
            font-size: small;
            text-decoration: underline;
        }
        body
        {
            color: Purple;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div style="border: 1px solid black; padding: 20px; width: 60%;">
        <table>
            <tr>
                <td>
                    <h1>
                        TextBoxes</h1>
                </td>
            </tr>
            <tr>
                <td>
                    Name
                </td>
                <td>
                    <asp:TextBox ID="txtName" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    Age
                </td>
                <td>
                    <asp:TextBox ID="txtAge" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    <h1>
                        Dropdown Lists</h1>
                </td>
            </tr>
            <tr>
                <td>
                    Gender
                </td>
                <td>
                    <asp:DropDownList ID="ddlGender" runat="server" Width="150">
                        <asp:ListItem Text="Select" Value="0"></asp:ListItem>
                        <asp:ListItem Text="Male" Value="1"></asp:ListItem>
                        <asp:ListItem Text="Female" Value="2"></asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    Religion
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" Width="150">
                        <asp:ListItem Text="Select" Value="0"></asp:ListItem>
                        <asp:ListItem Text="Muslim" Value="1"></asp:ListItem>
                        <asp:ListItem Text="Christian" Value="2"></asp:ListItem>
                        <asp:ListItem Text="Hindu" Value="3"></asp:ListItem>
                        <asp:ListItem Text="Others" Value="4"></asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    <h1>
                        Radio Button Lists</h1>
                </td>
            </tr>
            <tr>
                <td>
                    Nationality
                </td>
                <td>
                    <asp:RadioButtonList ID="rdNationality" runat="server">
                        <asp:ListItem Text="India" Value="0" />
                        <asp:ListItem Text="Other" Value="1" />
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <td>
                    <h1>
                        Checkbox Lists</h1>
                </td>
            </tr>
            <tr>
                <td>
                    Languages Known
                </td>
                <td>
                    <asp:CheckBoxList ID="chkLanguages" runat="server">
                        <asp:ListItem Text="Malayalam" Value="0" />
                        <asp:ListItem Text="English" Value="1" />
                        <asp:ListItem Text="Spanish" Value="2" />
                    </asp:CheckBoxList>
                </td>
            </tr>
            <tr>
                <td>
                    <h1>
                        Listbox</h1>
                </td>
            </tr>
            <tr>
                <td>
                    Technical Skills
                </td>
                <td>
                    <asp:ListBox ID="lstSkills" runat="server" SelectionMode="Multiple" Width="150">
                        <asp:ListItem Text="ASP.Net" Value="1" />
                        <asp:ListItem Text="SQL Server" Value="2" />
                        <asp:ListItem Text="MVC" Value="3" />
                        <asp:ListItem Text="JQuery" Value="4" />
                        <asp:ListItem Text="Javscript" Value="5" />
                        <asp:ListItem Text="MySQL" Value="6" />
                    </asp:ListBox>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button runat="server" ID="btnClick" Text="Clear All"
                    OnClientClick="clearAllServerFields();" />
                </td>
            </tr>
        </table>
    </div>
    <script type="text/javascript" language="javascript">
        function clearAllServerFields() {

            //Code for accessing all dropdown lists and listboxes in an aspx page
            var select = document.getElementsByTagName("select");
            var length = select.length;
            for (i = 0; i < length; i++) {
                for (var j = 0; j < select[i].length; j++) {
                    select[i][j].selected = false;
                }
            }

            var elements = document.getElementsByTagName("input");
            for (var ii = 0; ii < elements.length; ii++) {
                if (elements[ii].disabled == false) {
                    //Code for accessing all textboxes in an aspx page
                    if (elements[ii].type == "text") {
                        elements[ii].value = "";
                    }
                    //Code for accessing all radio buttons in an aspx page
                    if (elements[ii].type == "radio") {
                        elements[ii].checked = false;
                    }
                    //Code for accessing all checkboxes in an aspx page
                    if (elements[ii].type == "checkbox") {
                        elements[ii].checked = false;
                    }
                }
            }
        }

    </script>
    </form>
</body>
</html>
Advertisements

How to implement SQL Bulk Copy in SQL Server OR Bulk insert into SQL Server using SQL BulkCopy

October 17, 2012 1 comment

SqlBulkCopy for insert multiple rows of data into table in SQL Server

Sometimes we need to insert bulk amount of data into a table in SQL. We can insert bulk data to SQL in different methods. Here we are going to demonstrate how to insert large amount of data using SQL Bulk Copy method.

Example of SQL BulkCopy method

Here we are going to insert multiple rows of sample data into Employee table. First of all create a table in SQL named Employee. After that  create sample data for employee table and finally this multiple rows of employee data insert into table using SQL bulk copy method.

Create a Table in SQL 

CREATE TABLE Employee
(
ID INT,
EmpName varchar(200),
Department varchar(200),
DOB DateTime
);

SQLMulkCopy Example in SQL Server

SQLMulkCopy Example in SQL Server

C# Code to generate sample data and insertion using SQL Bulk Copy

private void loadDataBySQLBulkCopy()
        {
            DataTable dtTblEmployee = new DataTable();
            dtTblEmployee.Columns.Add("ID", typeof(Int32));
            dtTblEmployee.Columns.Add("EmpName", typeof(String));
            dtTblEmployee.Columns.Add("Department", typeof(String));
            dtTblEmployee.Columns.Add("DOB", typeof(DateTime));

            DataRow dtrow = dtTblEmployee.NewRow(); // Create New Row
            dtrow["ID"] = 1; //Bind Data to Columns
            dtrow["EmpName"] = "Wazeem";
            dtrow["Department"] = "Admin";
            dtrow["DOB"] = "1990-10-16 12:00:00.000";
            dtTblEmployee.Rows.Add(dtrow);

            dtrow = dtTblEmployee.NewRow(); // Create New Row
            dtrow["ID"] = 2; //Bind Data to Columns
            dtrow["EmpName"] = "Aslam";
            dtrow["Department"] = "HR";
            dtrow["DOB"] = "1987-05-16 12:00:00.000";
            dtTblEmployee.Rows.Add(dtrow);

            dtrow = dtTblEmployee.NewRow(); // Create New Row
            dtrow["ID"] = 3; //Bind Data to Columns
            dtrow["EmpName"] = "John";
            dtrow["Department"] = "Finance";
            dtrow["DOB"] = "1992-12-11 12:00:00.000";
            dtTblEmployee.Rows.Add(dtrow);

            dtrow = dtTblEmployee.NewRow(); // Create New Row
            dtrow["ID"] = 4; //Bind Data to Columns
            dtrow["EmpName"] = "Mishal";
            dtrow["Department"] = "Infra structure";
            dtrow["DOB"] = "1989-04-01 12:00:00.000";
            dtTblEmployee.Rows.Add(dtrow);

            SqlBulkCopy bulkCopy = new SqlBulkCopy(
                "server=TEST;database=TEST;uid=test;password=test",
                SqlBulkCopyOptions.TableLock);
            bulkCopy.DestinationTableName = "dbo.Employee";
            bulkCopy.WriteToServer(dtTblEmployee);

        }

How to merge two data tables in ASP.Net/C# OR Merge 2 DataTables and store in a new one in ASP.Net/C#

October 14, 2012 Leave a comment

Merge multiple data-tables data into a another data-table using C#/ASP.Net

Here we are going to demonstrate a sample application which shows merge two data tables  in ASP.Net. In this sample we are having two data tables with some data and merge data of each tables data. We binds first data table to a data grid second one to another grid and third data grid binds with merged data table.

Merge data tables in C#/ASP.Net

Merge data tables in C#/ASP.Net

 

ASPX Page for Example of merge two datatables in C#/ASP.Net

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MergeDataTable.aspx.cs"
 Inherits="ExperimentLab.MergeDataTable" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 First DataTable
 <asp:GridView ID="gdDataTableOne" runat="server" /><br />
 Second DataTable
 <asp:GridView ID="gdDataTableTwo" runat="server" /><br />
 Merged DattaTable
 <asp:GridView ID="gdDataTableMergedData" runat="server" /><br />
 </div>
 </form>
</body>
</html>
-*******

CODE BEHIND Page for Merge datatable in C#/ASP.Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace ExperimentLab
{
 public partial class MergeDataTable : System.Web.UI.Page
 {
 DataTable dtOne = new DataTable();
 DataTable dtTwo = new DataTable();
protected void Page_Load(object sender, EventArgs e)
 {
 fillDataTableOne();
 fillDataTableTwo();
 mergeDataTables();
 }
private void mergeDataTables()
 {
 dtOne.Merge(dtTwo, false);
 gdDataTableMergedData.DataSource = dtOne;
 gdDataTableMergedData.DataBind();
 }
private void fillDataTableOne()
 { 
 dtOne.Columns.Add("EmpID", typeof(Int32));
 dtOne.Columns.Add("EmployeeName", typeof(string));
 dtOne.Columns.Add("Department", typeof(string));
 dtOne.Columns.Add("City", typeof(string));
DataRow dtOnerow = dtOne.NewRow(); // Create New Row
 dtOnerow["EmpID"] = 1; //Bind Data to Columns
 dtOnerow["EmployeeName"] = "Ram";
 dtOnerow["Department"] = "Admin";
 dtOnerow["City"] = "Kochi";
 dtOne.Rows.Add(dtOnerow);
dtOnerow = dtOne.NewRow(); // Create New Row
 dtOnerow["EmpID"] = 2; //Bind Data to Columns
 dtOnerow["EmployeeName"] = "Mahesh";
 dtOnerow["Department"] = "Finance";
 dtOnerow["City"] = "Delhi";
 dtOne.Rows.Add(dtOnerow);
dtOnerow = dtOne.NewRow(); // Create New Row
 dtOnerow["EmpID"] = 3; //Bind Data to Columns
 dtOnerow["EmployeeName"] = "Raj";
 dtOnerow["Department"] = "Admin";
 dtOnerow["City"] = "Kolkata";
 dtOne.Rows.Add(dtOnerow);
dtOnerow = dtOne.NewRow(); // Create New Row
 dtOnerow["EmpID"] = 4; //Bind Data to Columns
 dtOnerow["EmployeeName"] = "Virbal";
 dtOnerow["Department"] = "HR";
 dtOnerow["City"] = "Mumbai";
 dtOne.Rows.Add(dtOnerow);
gdDataTableOne.DataSource = dtOne;
 gdDataTableOne.DataBind();
 }
private void fillDataTableTwo()
 {
dtTwo.Columns.Add("EmpID", typeof(Int32));
 dtTwo.Columns.Add("EmployeeName", typeof(string));
 dtTwo.Columns.Add("Department", typeof(string));
 dtTwo.Columns.Add("City", typeof(string));
DataRow dtTworow = dtTwo.NewRow(); // Create New Row
 dtTworow["EmpID"] = 5; //Bind Data to Columns
 dtTworow["EmployeeName"] = "John";
 dtTworow["Department"] = "Delivery";
 dtTworow["City"] = "Goa";
 dtTwo.Rows.Add(dtTworow);
dtTworow = dtTwo.NewRow(); // Create New Row
 dtTworow["EmpID"] = 6; //Bind Data to Columns
 dtTworow["EmployeeName"] = "Jacob";
 dtTworow["Department"] = "Finance";
 dtTworow["City"] = "Kolkata";
 dtTwo.Rows.Add(dtTworow);
dtTworow = dtTwo.NewRow(); // Create New Row
 dtTworow["EmpID"] = 7; //Bind Data to Columns
 dtTworow["EmployeeName"] = "Srini";
 dtTworow["Department"] = "HR";
 dtTworow["City"] = "Jaipur";
 dtTwo.Rows.Add(dtTworow);
dtTworow = dtTwo.NewRow(); // Create New Row
 dtTworow["EmpID"] = 8; //Bind Data to Columns
 dtTworow["EmployeeName"] = "Gopal";
 dtTworow["Department"] = "InfraStructure";
 dtTworow["City"] = "Chennai";
 dtTwo.Rows.Add(dtTworow);
gdDataTableTwo.DataSource = dtTwo;
 gdDataTableTwo.DataBind();
 }
 
 }
}




How to create breadcrumbs in ASP.Net/C# OR Show Navigations for each pages in ASP.Net/C# OR How to implement Sitemap in ASP.Net

October 2, 2012 4 comments

DOWNLOAD SOURCE CODE FOR BREADCRUMB EXAMPLE IN ASP.NET

What is breadcrumbs and what is the use of it?

Breadcrumbs typically appear horizontally across the top of a web page, often below title bars or headers. They provide links back to each previous page the user navigated through to get to the current page or-in hierarchical site structures-the parent pages of the current one. Breadcrumbs provide a trail for the user to follow back to the starting or entry point. A greater-than sign (>) often serves as hierarchy separator, although designers may use other glyphs <http://en.wikipedia.org/wiki/Glyph>  (such as » or >), as well as various graphical treatments.

Typical breadcrumbs look like this:

 Home page > Section page > Subsection page

How to implement breadcrumbs in ASP.Net/C# pages

In order to implement breadcrumbs in ASPX pages, we can use new pages called sitemaps. In ASP.NET the menu can be stored in a file to make it easier to maintain. This file is normally calledweb.sitemap, and is stored in the root directory of the web.

DOWNLOAD SOURCE CODE FOR BREADCRUMB EXAMPLE IN ASP.NET

BREADCRUMB EXAMPLE IN ASP.NET

Example for implementing sitemaps in ASP.Net/C# pages

We are going to demonstrate sample ASP.Net application for implementing Breadcrumbs in ASPX pages. First of all create a new ASP.Net project and create three pages home.aspx, aboutus.aspx  and contactus.aspx.  Then create a sitemap file by “Add New item > SiteMap” and mention the hierarchy of the pages in the sitemaps as follows

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

  <siteMapNode url="Default.aspx" title="Home"  description="Home Page">
    <siteMapNode url="ContactUs.aspx" title="Contact US" 
                 description="Contact US Page" />
    <siteMapNode url="About.aspx" title="About"  description="About " >
      <siteMapNode url="AboutCompany.aspx" title="About Company" 
                   description="About Our Company" />
      <siteMapNode url="AboutService.aspx" title="About Service" 
                   description="About Our Services" />
    </siteMapNode>
  </siteMapNode>
</siteMap>

Once we created sitemap and mentioned the hierarchy of the pages we need to map this sitemaps into all pages that we need to show navigation as follows.

<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
    <asp:SiteMapPath ID="SiteMap1" runat="server">
    </asp:SiteMapPath>

Sample Application of Navigation Menu/Bread Crumbs  

web.sitemap

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode url="Default.aspx" title="Home"  description="Home Page">
    <siteMapNode url="ContactUs.aspx" title="Contact US" 
                 description="Contact US Page" />
    <siteMapNode url="About.aspx" title="About"  description="About " >
      <siteMapNode url="AboutCompany.aspx" title="About Company" 
                   description="About Our Company" />
      <siteMapNode url="AboutService.aspx" title="About Service" 
                   description="About Our Services" />
    </siteMapNode>
  </siteMapNode>
</siteMap>

 About.aspx

<%@ Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="About.aspx.cs" Inherits="BreadCrumb.About" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
    <asp:SiteMapPath ID="SiteMap1" runat="server">
    </asp:SiteMapPath>
    <h2>
        Welcome TO About US Page
    </h2>
    <br />
    <a href="AboutCompany.aspx" runat="server">About Our Company</a>
    <br />
    <a id="A1" href="AboutService.aspx" runat="server">About Our Services</a>
</asp:Content>

Default.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="BreadCrumb._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:SiteMapPath ID="SiteMap1" runat="server">
    </asp:SiteMapPath>
    <h2>
        Welcome to HOME PAGE
    </h2> 
</asp:Content>

 

 

AboutCompany.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="AboutCompany.aspx.cs" Inherits="BreadCrumb.AboutCompany" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
    <asp:SiteMapPath ID="SiteMap1" runat="server">
    </asp:SiteMapPath>
    <h2>
        This is the Page showing about our Company
    </h2>
</asp:Content>

AboutService.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="AboutService.aspx.cs" Inherits="BreadCrumb.AboutService" %>
 <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
    <asp:SiteMapPath ID="SiteMap1" runat="server">
    </asp:SiteMapPath>
    <h2>
        This is the Page showing about our Service
    </h2>
</asp:Content>
-


            DOWNLOAD SOURCE CODE FOR BREADCRUMB EXAMPLE IN ASP.NET

How to implement BalloonPopupExtender in ASP.Net/C# OR Ballon Popup Extender Sample in ASP.Net/C#

September 25, 2012 1 comment

                     DOWNLOAD SOURCE CODE FOR BALLOON POPUP EXTENDER EXAMPLE

How to display good POPUP Window in ASP.Net using Ajax?

Most of the ASP.Net application requires showing some POPUP window to user for showing some messages or information. Also in order to create a ToolTips in ASP.Net/C# web application it’s very difficult to achieve for good looking ToolTip. In the newest Ajax Control Toolkit includes an awesome good looking and more functional popup extender called Balloon POPUP Extender. It’s a very light weight and having good look and feel popup, also we can apply custom styles for POPUP. Balloon POPUP Extender can use as ToolTips in ASP.Net application.

Download and Register New AjaxControl Toolkit

For implementing Balloon POPUP Extender in ASP.Net/C# application, we need to download newest Ajax Control Toolkit and register to the Project references. You can download new Ajax Control Toolkit from this link. Right click the ‘References’ under the ASP.Net project from Visual Studio and browse the dll and press OK.

In the ASPX page we need to register this ajax toolkit dll like below mentioned code.

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>

How to implement Balloon POPUP Extender in ASP.Net/C# Application?

After adding and register the Ajax Control Toolkit as mentioned above, we can start to implement the Balloon POPUP Extender. We can implement Balloon POPUP Extender while clicking/on mouse over/on focus of any server controls in ASP.Net. Below mentioned is the codes for implementing the same.

<ajax:BalloonPopupExtender ID="PopupControlExtender2" runat="server" 
TargetControlID="txtUserName"
 BalloonPopupControlID="pnlBalloon" Position="BottomRight" 
BalloonStyle="Cloud"
 BalloonSize="Medium" UseShadow="true" ScrollBars="Auto" 
DisplayOnMouseOver="true"
 DisplayOnFocus="false" DisplayOnClick="true" />

Balloon Popup Extender Sample

Balloon Popup Extender Sample

Balloon Popup Extender Sample


There are some properties are available for Balloon POPUP Extender control and we can adjust the size,color,style and controls of POPUP using these properties. Below are the main properties of Balloon POPUP Extender

TargetControlID – The ID of the control to attach to.

BalloonPopupControlID – The ID of the control to display.

Position – Optional setting specifying where the popup should be positioned relative to the target control. (TopRight, TopLeft, BottomRight, BottomLeft, Auto) Default value is Auto.

OffsetX/OffsetY – The number of pixels to offset the Popup from its default position, as specified by Position. Default value is 0.

BalloonStyle – Optional setting specifying the theme of balloon popup. (Cloud, Rectangle, Custom). Default value is Rectangle.

BalloonSize – Optional setting specifying the size of balloon popup. (Small, Medium and Large). Default value is Small.

CustomCssUrl – This is required if user choose BalloonStyle to Custom. This specifies the url of custom css which will display custom theme.

CustomClassName – This is required if user choose BalloonStyle to Custom. This specifies the name of the css class for the custom theme.

UseShadow – Optional setting specifying whether to display shadow of balloon popup or not.

ScrollBars – Optional setting specifying whether to display scrollbar if contents are overflowing. This property contains 5 options – None, Horizontal, Vertical, Both and Auto. Default value is Auto.

DisplayOnMouseOver – Optional setting specifying whether to display balloon popup on the client onMouseOver event. Default value is false.

DisplayOnFocus – Optional setting specifying whether to display balloon popup on the client onFocus event. Default value is false.

DisplayOnClick – Optional setting specifying whether to display balloon popup on the client onClick event. Default value is true.

Animations – Generic animations for the PopupControlExtender.

                     OnShow – The OnShow animation will be played each time the popup is displayed. The popup will be positioned correctly               but hidden. The animation can use <HideAction Visible=”true” /> to display the popup along with any other visual effects.

                     OnHide – The OnHide animation will be played each time the popup is hidden.


A very simple example for Balloon POPUP Extender in ASP.Net/C# using Ajax

Here we are demonstrating a very simple for implementing Ajax Balloon POPUP Extender  with full downloadable source code.

ASPX Page

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" 
AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="BaloonPopup._Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" 
TagPrefix="ajax" %>
<asp:Content ID="HeaderContent" runat="server" 
ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" 
ContentPlaceHolderID="MainContent">
<h2>
Welcome to Baloon Popup Extender Sample
</h2>
<p>
For Website/WebApplication creation <a href="http://www.tuvian.com" 
title="aps.net">www.tuvian.com</a>.
</p>
<div>
<ajax:ToolkitScriptManager ID="Scriptmanager1" runat="server">
</ajax:ToolkitScriptManager>
<div style="border: 1px solid gray; padding: 10px; margin: 10px;">
<h3>
Cloud Style Baloon Popup Example</h3>
<br />
<br />
<ajax:BalloonPopupExtender ID="PopupControlExtender2" runat="server" 
TargetControlID="txtUserName"
BalloonPopupControlID="pnlBalloon" Position="BottomRight" BalloonStyle="Cloud"
BalloonSize="Medium" UseShadow="true" ScrollBars="Auto" DisplayOnMouseOver="true"
DisplayOnFocus="false" DisplayOnClick="true" />
UserName :
<asp:TextBox runat="server" ID="txtUserName" />
<asp:Panel runat="server" ID="pnlBalloon">
This is the Cloud Style Ballon Popup</asp:Panel>
</div>
<div style="border: 1px solid gray; padding: 10px; margin: 10px;">
<h3>
Rectangular Baloon Popup Example</h3>
<br />
<br />
<ajax:BalloonPopupExtender ID="Balloonpopupextender1" runat="server" 
TargetControlID="lblShow"
BalloonPopupControlID="pnlRectangularBallon" Position="TopRight" 
BalloonStyle="Rectangle"
BalloonSize="Medium" UseShadow="true" ScrollBars="Auto" 
DisplayOnMouseOver="false"
DisplayOnFocus="false" DisplayOnClick="true" />
<asp:Label runat="server" ID="lblShow" 
Text="Click Here to Show the Rectangular Balloon Popup" />
<asp:Panel runat="server" ID="pnlRectangularBallon">
This is the rectangular ballon popup</asp:Panel>
</div>
<div style="border: 1px solid gray; padding: 10px; margin: 10px;">
<h3>
Custom Style Baloon Popup Example</h3>
<br />
<br />
<ajax:BalloonPopupExtender ID="Balloonpopupextender2" runat="server" 
TargetControlID="txtCustomBallonPopup"
BalloonPopupControlID="pnlCustomBallon" Position="BottomRight" 
BalloonStyle="Custom"
BalloonSize="Medium" UseShadow="true" ScrollBars="Auto" 
DisplayOnMouseOver="true"
CustomCssUrl="Styles/BalloonPopupOvalStyle.css" 
CustomClassName="oval" DisplayOnFocus="false"
DisplayOnClick="true" />
<asp:TextBox runat="server" ID="txtCustomBallonPopup" />
<asp:Panel runat="server" ID="pnlCustomBallon">
This is the Custom Style ballon popup</asp:Panel>
</div>
</div>
</asp:Content>
--------------

                  DOWNLOAD SOURCE CODE FOR BALLOON POPUP EXTENDER EXAMPLE

How to Create a Data Table Dynamically with sample data and Bind to Grid/Create datatable with sample data.

September 7, 2012 4 comments

How to create a datatable with sample data

We are going to demonstrate how to create a sample datatable dynamically in ASP.Net. Here we are creating a ASPx page with a grid view. In the page load of the ASPx page will call a bind method which creating a datatable dynamically and bind the datatable with datagrid. In BindGridviewData function create data for an employee and binding employee data to the employee grid.

ASPxPage

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicCreateDataTable.aspx.cs"
    Inherits="ExperimentLab.DynamicCreateDataTable" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gdEmployee" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code Behind

protected void Page_Load(object sender, EventArgs e)
{
BindGridviewData();
}
//Creating datatable dynamically and bind data to a grid
protected void BindGridviewData()
{
DataTable dt = new DataTable();
//Create datatable Columns dynamically 
dt.Columns.Add("EmployeeID", typeof(Int32));
dt.Columns.Add("EmployeeName", typeof(string));
dt.Columns.Add("Department", typeof(string));
dt.Columns.Add("City", typeof(string));
//Create data table row dynamically add
DataRow dtrow = dt.NewRow(); // Create New Row 
//Assign data to datarow dynamically
dtrow["EmployeeID"] = 1; //Bind Data to Columns
dtrow["EmployeeName"] = "Ramraj";
dtrow["Department"] = "Admin";
dtrow["City"] = "Calicut";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); // Create New Row
//Assign data to datarow dynamically
dtrow["EmployeeID"] = 2; //Bind Data to Columns
dtrow["EmployeeName"] = "Malhothra";
dtrow["Department"] = "HR";
dtrow["City"] = "Mumbai";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); // Create New Row
//Assign data to datarow dynamically
dtrow["EmployeeID"] = 3; //Bind Data to Columns
dtrow["EmployeeName"] = "Sinan Hafis";
dtrow["Department"] = "Admin";
dtrow["City"] = "Delhi";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); // Create New Row
//Assign data to datarow dynamically
dtrow["EmployeeID"] = 4; //Bind Data to Columns
dtrow["EmployeeName"] = "Wazim Jafar";
dtrow["Department"] = "Finance";
dtrow["City"] = "Goa";
dt.Rows.Add(dtrow);
gdEmployee.DataSource = dt;
gdEmployee.DataBind();
}

RESULT 

Dynamic datatable in ASP.Net

Dynamic datatable in ASP.Net

How to Create and use Table-Valued Parameter in C# and T-SQL/ How to pass table to stored procedures in SQL

July 17, 2012 Leave a comment

What is Table – Valued Parameter in SQL Server 2008?

Table-valued parameters provide an easy way to marshal multiple rows of data from a client application to SQL Server without requiring multiple round trips or special server-side logic for processing the data. You can use table-valued parameters to encapsulate rows of data in a client application and send the data to the server in a single parameterized command. The incoming data rows are stored in a table variable that can then be operated on by using Transact-SQL.

What we are going to do with Table – Valued Parameter?

We are going to demonstrate a very simple example for using Table – Valued parameter. In this sample project we will insert bulk amount of data into the table by passing a bulk data using datatable in C# to SQL stored procedure.

Create a Table for insert data using Table – Valued Parameter 

Here we are having a table named Officer and having three fields ID,Name and Salary. We are going to fill the table with bulk data.

CREATE TABLE Officer(
ID INT PRIMARY KEY IDENTITY(1,1),
NAME VARCHAR(50),
SALARY DECIMAL(18, 0))

Stored Procedure for insert data by accepting Table Valued Parameter

Now we are going to create a Stored Procedure that accepting a table type as parameter and insert values in this type into the table.

CREATE PROCEDURE InsertOfficerDetails
(
@OfficerData OfficerDetails readonly
)
AS 
INSERT INTO Officer (Name, Salary)
SELECT Name, Salary
FROM @OfficerData;

C# code to call Stored Procedure to insert data in to the table using Table – Valued Parameter

We are creating a simple ASPX page with a single button. When we click this button we are calling above stored procedure by creating and passing some amount of sample data to the stored procedure as Table – Valued Parameter.

ASPX Page 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CallSP.aspx.cs" 
Inherits="ExperimentLab.CallSP" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <asp:Button ID="btnCallSP" runat="server" Text="Call SP" 
 OnClick="btnCallSP_Click" />
 </div>
 </form>
</body>
</html>

Code Behind

protected void btnCallSP_Click(object sender, EventArgs e)
 {
 try
 {
 DataTable dt = new DataTable();
 DataColumn dtCol = new DataColumn();
 dtCol.ColumnName = "ID";
 dt.Columns.Add(dtCol);
dtCol = new DataColumn();
 dtCol.ColumnName = "Name";
 dt.Columns.Add(dtCol);
dtCol = new DataColumn();
 dtCol.ColumnName = "Salary";
 dt.Columns.Add(dtCol);
for (int i = 0; i < 10; i++)
 {
 DataRow dr = dt.NewRow();
 dr["Name"] = "Name " + i;
 dr["Salary"] = 1000 + i;
 dr["ID"] = i;
 dt.Rows.Add(dr);
 }
 
 string connStr = ConfigurationManager.
 AppSettings["LocalSqlServer"].ToString();
 SqlConnection con = new SqlConnection(connStr);
using (var conn = new SqlConnection(connStr))
 using (var cmd = conn.CreateCommand())
 {
 cmd.Connection = con;
 con.Open();
 cmd.CommandType = CommandType.StoredProcedure;
 cmd.CommandText = "dbo.InsertOfficerDetails";
 SqlParameter param = cmd.Parameters.AddWithValue("@OfficerData", dt);
 cmd.ExecuteNonQuery();
 }
 }
 catch (Exception a)
 {
 Response.Write(a.Message);
 }
 }

Web.Config

<appSettings>
 <add key="LocalSqlServer" 
value="Database=testDB;Server=Servername\SQLEXPRESS;User Id=userid;Password=password"/>
</appSettings>

Hence we discussed about how to create and use Table valued parameters, how to create a store procedure with table type as parameter, how to pass table to stored procedure in C#/Asp.Net,  how to insert multiple rows of data to a table with table valued parameter in SQL, how to insert bulk data to SQL table using Table Valued Parameter in SQL Server 2008 etc..

 

%d bloggers like this: