Asp.Net Dropdown Control items from Database

Assalamualaykum Wr Br…Today we explore dropdown control list items binding from database table on to UI. Before we quick start, there are few properties that we need to remember when dealing with Dropdown controls. They are:
1. DataTextField
2. DataValueField
3. DataSource
4. DataBind
There are many other properties like selectedIndex, IndexChanged but we focus on above.
Lets start the POC:
I just drag & drop the Dropdownlist control from toolbox on form beside Qualification label. The items of this dropdownlist should be populated from Table Register and column Qualification. To accomplish it, I write Sql Query as: select qualification from register in sqlcommand class or sqldataadapter as shown below:
As I need dropdownlist to be populated on page_load hence I am calling method filldropdown
In filldropdown method,sqlconnection is established then sqldataadapter executes the query fetches data and fills in dataset which becomes datasource to dropdownlist and finally binds to it. The above code produces following output:
One thing to be noted is that all items are being populated from database, if we comment the statement Dropdownlist1.AppendDataBoundItems=true.
If we uncomment it then it is possible to add items manually to dropdownlist which is done by statement as dropdownlist1.items.add(“”); whose output is shown as:
Jazakallah khair…thats all for the day…


Asp.Net GridView 4

Assalamualaykum Wr Br…GridView displays data directly on to the page with out any proper style as shown below:
If we apply style over to gridview as shown below:

<div style=” height:100%; width:100%; overflow:auto;”>
<asp:gridview runat=”server” id=”gv1″ >
…respective columns…
we get scroll bar when data exceeds than specified height and width on page.
Note: the syntax is pure sensitive, if you miss any colon (:) or double quotes (“) or semi-colon(;) then that does not reflect on UI of Page.
Thats all for the day…hope would be useful in future…
Jazakallah Khair…

Asp.Net GridView 3

Assalamualaykum Wr Br…In this post we would like to explore the few events of Asp.Net GridView. Lets explore RowEditing, RowUpdating Events here. Goto Properties of the GridView and click the Events tab as shown:
You can see list of events of Gridview like SelectedIndexChanged, RowCancelingEdit, RowCommand,RowDeleted, RowDeleting, RowUpdated, RowUpdating etc.
Double click the RowEditing Event textbox in the property strip that takes you to the eventhandler method as shown below:
The above pic have RowEditing, RowDataBound Events whose code is shown.
RowDataBound event is used to bind data to specific row which is called foreach row.
We can filter out the execution of RowDataBound specific to Controls, as in our case we are finding out dropdownlist controls. we create reference entity of DropDownList ad to which FindControl method of GridViewRowEventArguments. Specify the control id of Dropdownlist as parameter to FindControl method. Later we execute the establish connection, execute the command and binds to dropdownlist, whose output is as shown:
Thats all for the day..Hope enjoyed…
Jazakallah Khair…

Asp.Net GridView Part 2

Assalamualaykum Wr Br…In this post we would explore the ways to bind the GridView with database. First is quite common where we explore through wizard by which you observe bind attributes and the other is by code where you need to change the Bind attributes to asp:templatefield followed by the respective attributes.
The best practice by code is to define and implement a method that fetches the data from DB and binds it to GridView. Call that method into the page_load event with not post back check. this populates the data on to the gridview at page load…lets do the poc on it…
The above image depicts two methods i.e. Page_Load and filldata methods. The First statement of Page_Load method is if constraint which checks the page is either requested for first time or being requested multiple. When Constraint is true then calls filldata method.
In this method, connection is established with the sqldatasource and sqldataadapter, which executes the command, fetches the data and fills in Dataset and the same is provided to GridView as Datasource and binds to it. Hence the output is displayed as:
This is how dataSource is binded through code to GridView…hope enjoyed it..Jazakallah khair…

Asp.Net Gridview part 1

Assalamualaykum Wr we explore the’s powerful control to display data as in tabular format with built-in Edit, Update, Cancel and Insert options, named as GRIDVIEW. The general syntax of markup is as shown:
<asp:GridView ID=”gridview1″ runat=”server”>

To add columns to the above grid, need to specify as shown:

<asp:GridView ID=”gridview1″ runat=”server”>

Under <Columns>,specify the Columns names by using attribute as shown:
<asp:TemplateField HeaderText=”Name” SortExpressio=”Name”></asp:TemplateField>
Observe the asp:templatefield tag which has HeaderText which is your column name that appears on your gridview. SortExpression is the value based on which it sorts. If you need Three columns on Gridview to be shown then specify the three asp:templateFileds with headertext. This is all about columns, how about rows then?
We specify the rows of the gridview with label control under <ItemTemplate> which must be under <asp:TemplateField> this asp:TemplateField should be under Columns tag, as shown below:
<asp:GridView id=”gridview1″ runat=”server”>
<asp:TemplateField HeaderText=”Name” SortExpression=”Name”>
<asp:label id=”lbl_name” runat=”server” text=”Name”></label>

<asp:TemplateField HeaderText=”Name” SortExpression=”Name”>
<asp:label id=”lbl_name” runat=”server” text=”Name”></label>

<asp:TemplateField HeaderText=”Qualification” SortExpression=”Qualification”>
<asp:label id=”lbl_Qualification” runat=”server” text=”Qualification”></label>
</ItemTemplate> <asp:TemplateField HeaderText=”Contact” SortExpression=”Contact”>
<asp:label id=”lbl_contact” runat=”server” text=”Contact”></label>

This does not gives any output on ur aspx page as we are not binding data to gridview control, gridview displays data only after binding to it. In later post we would cover this topic, but in this post I just covered about the basic templates of GridView…thats all for the day..

jazakallah khair

Tips & Tricks Visual Studio & VDI

Assalamualaykum Wr Br…
After a long time posting here…was much busy with Eid and other issues…Lets start our journey with the things that I encountered these days…
I was working with VDI (Virtual Desktop Interface) system connected to my local desktop. When I lock the system by pressing Windows key + L, my local system gets locked and when I press Ctrl + Alt + Del that populates local system’s Task Manager Screen with other options as well. But when I need to switch VDI’s System Task Manager then I need to press Ctrl +Alt + End ..
normally when we open visual studio, we see code with our line numbers as shown below:
When I want line numbers to be display in Visual studio editor then Go to main menu of visual studio Tools –> Options –>Text Editors –> All Languages –> General Line Number CheckBox Check it. as shown below:

After checking the box then observe the visual studio editor with line number in each line….

hope it helped anyone or atleast to me…have a good day…:)