![](/user_photo/1438_p9ksI.png)
Microsoft ASP .NET Professional Projects - Premier Press
.pdf![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI351x1.jpg)
auom.text = ""
hidePanel()
End Sub
Note that we are passing a NULL code_value to the procedure. This fires an insert statement. This SQL query string is passed on to the function RunSQL, which does the actual work of executing the SQL statement.
Delete Mode
The Delete mode is activated when the user clicks on the delete link. I have created a delete ButtonColumn as follows:
<asp:ButtonColumn Text="Delete" CommandName="Delete" HeaderText="Delete"/> I have associated an OnDeleteCommand with this button as follows:
<asp:DataGrid id="Grid1" runat="server"
---------------------------------------------------------------
OnDeleteCommand = "Grid1_delete"
--------------------------------------------------------------- >
In the Grid1_Delete event, I simply build a delete statement and pass it on to the function RunSQL, which applies the query against the database.
Sub Grid1_delete
Sub Grid1_delete(sender As Object, e As DataGridCommandEventArgs)
Dim code_value As string = Grid1.DataKeys.Item(E.Item.ItemIndex).ToString
Dim sql As string
sql = "Delete from stock_master where code_value = " + cstr(code_value)
RunSql(sql)
End Sub
The RunSql Function
This is a generic function, which executes a SQL Action statement against the database. The SQL query statement is passed to it as a parameter. The Grid1_update Sub, the Add_click Sub, and the Grid1_delete Sub call this function to update, add, or delete a record. This function calls the RunSql function of the web service and passes it the connection string, as well as the SQL action query/procedure call. If the procedure/query was executed successfully, the string Success is returned from the function. Otherwise, the appropriate error string is returned, which is displayed in the browser.
Sub RunSql
Sub RunSql(vSQL as string)
Dim ConnStr As String
Dim SQL As String
ConnStr = "Provider=SQLOLEDB; Data Source=(local); "
ConnStr = ConnStr+" Initial Catalog=ASPNET;User ID=sa;"
Dim t As New NameSpaceHersh.SQLService
Dim s As string
s = t.RunSQL(ConnStr,vSQL)
Grid1.EditItemIndex = -1
if s <> "Success" then
Message.Text = s
Message.Style("color") = "red"
End if
response.write (vsql)
Rebind
End Sub
Sorting
The DataGrid enables you to sort the columns by clicking on a link below the columns. Setting the AllowSorting property to true triggers this built-in mechanism, as follows:
<asp:DataGrid id="Grid1" runat="server"
AllowSorting="true"
OnSortCommand="MyDataGrid_Sort">
When this property is set to true, the DataGrid renders the column captions with a linkbutton. If you now click on a column, the OnSortEvent is fired. This event contains the following code:
Sub MyDataGrid_Sort(sender As Object, e As DataGridSortCommandEventArgs)
SortField = e.SortField
ReBind
End Sub
The SortField variable is a Public (string) variable, which holds the name of the column, and by which the DataGrid is to be sorted. It is initially set to the code_display column in the page_load event as follows:
Sub Page_Load(Source As Object, E As EventArgs)
If NOT (isPostBack)
If SortField = "" Then
SortField = "code_display"
End If
ReBind
End If
End Sub
You need to set the SortExpression attribute in the column templates. For example, for the rate column to participate in sorting, you have to set the template as follows:
<asp:TemplateColumn HeaderText="Rate" SortExpression ="rate" >
</asp:TemplateColumn>
Each time the user clicks on a "sortable" column, the MyDataGrid_Sort Sub is fired. The SortField variable again gets set in this method.
Sub MyDataGrid_Sort(sender As Object, e As DataGridSortCommandEventArgs)
SortField = e.SortExpression
ReBind
End Sub
The ReBind function uses the SortExpression attribute to sort the DataView (which in turn binds the DataGrid), and refreshes the DataGrid to reflect the rows sorted by the new sort field, as follows:
Sub ReBind()
Dim ConnStr As String
Dim SQL As String
ConnStr = "Provider=SQLOLEDB; Data Source=(local); "
ConnStr = ConnStr+" Initial Catalog=ASPNET;User ID=sa;"
Dim t As New NameSpaceHersh.SQLService
Dim ds As DataSet
' Bind Grid
SQL = "Select * from stock_master"
ds = t.Populate(ConnStr, SQL)
Dim dv2 As DataView
dv2 = ds.Tables("vTable").DefaultView
dv2.Sort = SortField
Grid1.DataSource= dv2
Grid1.DataBind()
hidePanel()
End Sub
Paging in DataGrid
The DataGrid has a built-in pager control, which displays a user-defined number of pages per page, and also numeric or next/previous buttons at the bottom of the DataGrid. Clicking on these links displays the next set of pages. To enable paging, you set a number of properties as follows:
<asp:DataGrid id="Grid1" runat="server"
AllowPaging="True"
PageSize="3"
PagerStyle-Mode="NumericPages"
![](/html/1438/356/html_wpziZPj8IA.ZbBg/htmlconvd-0Ag2qI354x1.jpg)
PagerStyle-HorizontalAlign="Right"
PagerStyle-NextPageText="Next"
PagerStyle-PrevPageText="Prev"
OnPageIndexChanged="MyDataGrid_Page">
The AllowPaging property must be set to true to enable paging. The PageSize property sets the number of records per page. A PageSize of 3 implies that only three records per page will be shown. If you leave out the PagerStyleMode="NumericPages" property, then instead of numeric links at the bottom, you get two links: next and previous. The PagerStyle-NextPageText and the
PagerStyle-PrevPageText properties are descriptive captions for these two links and they can be any text you want. You are required to code one event. This is the OnPageIndexChanged event, which fires off the MyDataGrid_Page event. You simply call the ReBind function in this event as follows:
Sub MyDataGrid_Page(sender As Object, e As DataGridPageChangedEventArgs)
ReBind
End Sub
Here is the complete listing of StockMaster.aspx.
StockMaster.aspx
<%@Page Language="VB" Inherits="BaseClass" Src="stockmasters.vb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Register TagPrefix="Hersh" TagName="nav" Src="nav.ascx" %>
<html>
<script language="VB" runat="server">
Sub Grid1_Update(sender As Object, e As DataGridCommandEventArgs)
Dim sql As string
Dim code_display As String
Dim rate As String
Dim uom As String
Dim opening As String
Dim closing As String
Dim myTextBox As TextBox
'This is the key value:
'Retrieved from the DataKey, since it's a read only field
Dim code_value As string = Grid1.DataKeys.Item(E.Item.ItemIndex).ToString myTextBox = E.Item.FindControl("edit_name")
code_display = mytextbox.text
myTextBox = E.Item.FindControl("edit_rate") rate = mytextbox.text
myTextBox = E.Item.FindControl("edit_uom") uom = mytextbox.text
myTextBox = E.Item.FindControl("edit_opening") opening = mytextbox.text
myTextBox = E.Item.FindControl("edit_closing") closing = mytextbox.text
'Now execute stored procedure
sql = "Execute p_Stock_master @code_value ="
sql = sql+code_value+", @code_display = '"+code_display+"',@rate="
sql = sql+rate+", @uom='"+uom +"' ,@opening ="+opening+",@closing="+closing RunSql(sql)
End Sub
Sub add_click(Source As Object, E As EventArgs)
Dim sql As string
if acode_display.text = "" then response.write("Incomplete information") exit sub
end if
SQL = "Execute p_stock_master @code_value=NULL,@code_display='" SQL = SQL+ acode_display.text + "',@rate="
SQL = SQL+arate.Text+",@uom="+auom.text+",@opening="+aopening.text
RunSql(sql)
'reset values acode_display.text = "" aopening.text = ""
arate.text = "" auom.text = "" hidePanel()
End Sub
Sub add_show(Source As Object, E As EventArgs)
AddPanel.visible = true
End Sub
Sub Grid1_delete(sender As Object, e As DataGridCommandEventArgs)
Dim code_value As string = Grid1.DataKeys.Item(E.Item.ItemIndex).ToString Dim sql As string
sql = "Delete from stock_master where code_value = " + cstr(code_value) RunSql(sql)
End Sub
Sub MyDataGrid_Page(sender As Object, e As DataGridPageChangedEventArgs)
Grid1.CurrentPageIndex = e.NewPageIndex
ReBind
End Sub
Sub MyDataGrid_Sort(sender As Object, e As DataGridSortCommandEventArgs)
SortField = e.SortExpression
ReBind
End Sub </script> <head>
<style>
a { color:black; text-decoration:none;}
a:hover { color:red; text-decoration:underline;}
</style>
</head>
<body style="font: 10pt verdana; background-color:ivory"> <form runat="server">
<asp:ValidationSummary runat=server headertext="There were errors on the page:" /> <!—— Navigation Start———————>
<Hersh:nav id="menu" runat = server vGridlines = Both
vBorderColor = "Black" vCellPadding = 7 />
<!—— Navigation Ends———————>
<h3><font face="Verdana">Inventory Masters </font></h3> <asp:Label id="Message" runat="server"/>
<asp:Button id="Addshow" text="Add Account" onclick="add_show" runat="server" /> <table width="95%">
<tr>
<td valign="top">
<asp:DataGrid id="Grid1" runat="server" AutoGenerateColumns="false" BackColor="White"
BorderWidth="1px" BorderStyle="Solid" BorderColor="Tan"
CellPadding="2" CellSpacing="0"
Font-Name="Verdana" Font-Size="8pt"
OnEditCommand="Grid1_Edit"
OnCancelCommand="Grid1_Cancel"
OnUpdateCommand="Grid1_Update"
OnDeleteCommand = "Grid1_delete"
DataKeyField="code_value"
AllowPaging="True"
PageSize="20"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Right"
PagerStyle-NextPageText="Next"
PagerStyle-PrevPageText="Prev" OnPageIndexChanged="MyDataGrid_Page" AllowSorting="true" OnSortCommand="MyDataGrid_Sort"> <Columns>
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="OK"
ItemStyle-Wrap="false"
HeaderText="Edit"
HeaderStyle-Wrap="false"/>
<asp:ButtonColumn Text="Delete" CommandName="Delete" HeaderText="Delete"/>
<asp:BoundColumn HeaderText="Account #" ReadOnly="true" DataField="code_value" SortExpression="code_value" />
<asp:TemplateColumn HeaderText="Name" SortExpression="code_display">
<ItemTemplate>
<asp:Label Text='<%# Container.DataItem("code_display") %>' runat="server"/>
</ItemTemplate>
<EditItemTemplate> <asp:RequiredFieldValidator runat=server
controltovalidate=edit_Name errormessage="Name is required.">*
</asp:RequiredFieldValidator> <asp:TextBox id="edit_name"
Text='<%# Container.DataItem("code_display") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Rate" SortExpression="rate" > <ItemTemplate>
<asp:Label Text='<%# Container.DataItem("rate") %>' runat="server" /> </ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="edit_rate" Text='<%# Container.DataItem("rate") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="uom" SortExpression="uom" > <ItemTemplate>
<asp:Label Text='<%# Container.DataItem("uom") %>' runat="server"/> </ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="edit_uom" Text='<%# Container.DataItem("uom") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Opening" > <ItemTemplate>
<asp:Label Text='<%# Container.DataItem("opening") %>' runat="server"/> </ItemTemplate>
<EditItemTemplate> <asp:TextBox id="edit_opening"
Text='<%# Container.DataItem("opening") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn> <asp:TemplateColumn HeaderText="Closing" >
<ItemTemplate>
<asp:Label Text='<%# Container.DataItem("closing") %>' runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="edit_closing" BorderStyle="None" Readonly="True" Text='<%# Container.DataItem("closing") %>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<HeaderStyle BackColor="Gray" ForeColor="White" Font-Bold="true"/>
<ItemStyle ForeColor="DarkSlateBlue"/>
<AlternatingItemStyle BackColor="Beige"/> </asp:DataGrid>
</td>
<td valign="top">
<!---- insert row logic-------->
<asp:Panel id="AddPanel" runat="server" Visible="false"> <table style="font: 8pt verdana">
<tr>
<td colspan="2" bgcolor="#aaaadd" style="font:10pt verdana"> Add a New Account:</td>
</tr>
<tr>
<td nowrap>Name: </td>
<td><asp:TextBox id="acode_display" runat="server" /></td>