.VB code:
Imports
System
Imports
System.Configuration
Imports
System.Data
Imports
System.Data.SqlClient
Imports
Telerik.Web.UI
Imports
System.Configuration.ConfigurationManager
Partial
Class Mistras_Admin_NDEEquipmentList
Inherits System.Web.UI.Page
Dim myConnection As New SqlConnection(AppSettings("Conn"))
Public Function GetDataTable(ByVal query As String) As DataTable
Dim ConnString As String = ConfigurationManager.ConnectionStrings("ConnMR").ConnectionString
Dim conn As SqlConnection = New SqlConnection(ConnString)
Dim adapter As SqlDataAdapter = New SqlDataAdapter
adapter.SelectCommand =
New SqlCommand(query, conn)
Dim table1 As New DataTable
conn.Open()
Try
adapter.Fill(table1)
Finally
conn.Close()
End Try
Return table1
End Function
Public ReadOnly Property NDEE() As DataTable
Get
Dim obj As Object = Me.Session("NDEE")
If (Not obj Is Nothing) Then
Return CType(obj, DataTable)
End If
Dim myDataTable As DataTable = New DataTable
myDataTable = GetDataTable(
"SELECT Equipment.EquipmentID, EquipmentType.EquipmentType, Equipment.Manufacturer, Equipment.Model, Equipment.SerialNo, Equipment.Megahertz, Equipment.ProbeDiameter, Equipment.TypeOrForm, Equipment.Inactive FROM Equipment with (nolock) inner join EquipmentType with (nolock) on Equipment.EquipmentTypeID = EquipmentType.EquipmentTypeID WHERE Equipment.EquipmentID IN (SELECT EquipmentID FROM Equipment_Location_Link WHERE LocationID = " & ddlLocations.SelectedValue & ")")
Me.Session("NDEE") = myDataTable
Return myDataTable
End Get
End Property
Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
Me.RadGrid1.DataSource = Me.NDEE
Me.NDEE.PrimaryKey = New DataColumn() {Me.NDEE.Columns("EquipmentID")}
End Sub
Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender
If Not IsPostBack Then
'Me.RadGrid1.MasterTableView.Items(1).Edit = True
Me.RadGrid1.MasterTableView.Rebind()
End If
End Sub
Protected Sub RadGrid1_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.UpdateCommand
Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
Dim MyUserControl As UserControl = CType(e.Item.FindControl(GridEditFormItem.EditFormUserControlID), UserControl)
'Locate the changed row in the DataSource
Dim changedRows As DataRow() = Me.NDEE.Select("EquipmentID = " & editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("EquipmentID"))
If (Not changedRows.Length = 1) Then
e.Canceled =
True
Return
End If
'Update new values
Using updateCommand As New SqlCommand("Update_Equipment", myConnection)
updateCommand.CommandType = CommandType.StoredProcedure
updateCommand.Parameters.AddWithValue(
"@EID", editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("EquipmentID"))
updateCommand.Parameters.AddWithValue(
"@ETID", CType(MyUserControl.FindControl("ddlET"), DropDownList).SelectedValue)
updateCommand.Parameters.AddWithValue(
"@Man", CType(MyUserControl.FindControl("txtMan"), TextBox).Text)
updateCommand.Parameters.AddWithValue(
"@Mod", CType(MyUserControl.FindControl("txtModel"), TextBox).Text)
updateCommand.Parameters.AddWithValue(
"@SNo", CType(MyUserControl.FindControl("txtSBN"), TextBox).Text)
updateCommand.Parameters.AddWithValue(
"@MHz", CType(MyUserControl.FindControl("txtMHz"), TextBox).Text)
updateCommand.Parameters.AddWithValue(
"@PD", CType(MyUserControl.FindControl("txtPD"), TextBox).Text)
updateCommand.Parameters.AddWithValue(
"@ToF", CType(MyUserControl.FindControl("txtMPFT"), TextBox).Text)
updateCommand.Parameters.AddWithValue(
"@Ina", CType(MyUserControl.FindControl("cbInactive"), CheckBox).Checked)
myConnection.Open()
updateCommand.ExecuteNonQuery()
myConnection.Close()
End Using
Dim newValues As Hashtable = New Hashtable
newValues(
"Manufacturer") = CType(MyUserControl.FindControl("txtMan"), TextBox).Text
newValues(
"Model") = CType(MyUserControl.FindControl("txtModel"), TextBox).Text
newValues(
"SerialNo") = CType(MyUserControl.FindControl("txtSBN"), TextBox).Text
newValues(
"TypeOrForm") = CType(MyUserControl.FindControl("txtMPFT"), TextBox).Text
newValues(
"Megahertz") = CType(MyUserControl.FindControl("txtMHz"), TextBox).Text
newValues(
"ProbeDiameter") = CType(MyUserControl.FindControl("txtPD"), TextBox).Text
newValues(
"EquipmentType") = CType(MyUserControl.FindControl("ddlET"), DropDownList).SelectedItem.Text
newValues(
"Inactive") = CType(MyUserControl.FindControl("cbInactive"), CheckBox).Checked
changedRows(0).BeginEdit()
Try
Dim entry As DictionaryEntry
For Each entry In newValues
changedRows(0)(
CType(entry.Key, String)) = entry.Value
Next
changedRows(0).EndEdit()
Me.NDEE.AcceptChanges()
Catch ex As Exception
changedRows(0).CancelEdit()
Dim lblError As Label = New Label()
lblError.Text =
"Unable to update Equipment. Reason: " + ex.Message
lblError.ForeColor = System.Drawing.Color.Red
RadGrid1.Controls.Add(lblError)
e.Canceled =
True
End Try
End Sub
Protected Sub RadGrid1_InsertCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles RadGrid1.InsertCommand
If Session("Insert") = False Then
Session(
"Insert") = True
Exit Sub
Else
Session(
"Insert") = False
End If
Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
Dim userControl As UserControl = CType(e.Item.FindControl(GridEditFormItem.EditFormUserControlID), UserControl)
'Prepare new row to add it in the DataSource
Dim newRow As DataRow = Me.NDEE.NewRow
'Insert new values
Dim newValues As Hashtable = New Hashtable
Using insertCommand As New SqlCommand("Insert_Equipment", myConnection)
insertCommand.CommandType = CommandType.StoredProcedure
insertCommand.Parameters.AddWithValue(
"@ETID", CType(userControl.FindControl("ddlET"), DropDownList).SelectedValue)
insertCommand.Parameters.AddWithValue(
"@Man", CType(userControl.FindControl("txtMan"), TextBox).Text)
insertCommand.Parameters.AddWithValue(
"@Mod", CType(userControl.FindControl("txtModel"), TextBox).Text)
insertCommand.Parameters.AddWithValue(
"@SNo", CType(userControl.FindControl("txtSBN"), TextBox).Text)
insertCommand.Parameters.AddWithValue(
"@MHz", CType(userControl.FindControl("txtMHz"), TextBox).Text)
insertCommand.Parameters.AddWithValue(
"@PD", CType(userControl.FindControl("txtPD"), TextBox).Text)
insertCommand.Parameters.AddWithValue(
"@ToF", CType(userControl.FindControl("txtMPFT"), TextBox).Text)
insertCommand.Parameters.AddWithValue(
"@Ina", CType(userControl.FindControl("cbInactive"), CheckBox).Checked)
myConnection.Open()
insertCommand.ExecuteNonQuery()
myConnection.Close()
End Using
newValues(
"Manufacturer") = CType(userControl.FindControl("txtMan"), TextBox).Text
newValues(
"Model") = CType(userControl.FindControl("txtModel"), TextBox).Text
newValues(
"SerialNo") = CType(userControl.FindControl("txtSBN"), TextBox).Text
newValues(
"TypeOrForm") = CType(userControl.FindControl("txtMPFT"), TextBox).Text
newValues(
"Megahertz") = CType(userControl.FindControl("txtMHz"), TextBox).Text
newValues(
"ProbeDiameter") = CType(userControl.FindControl("txtPD"), TextBox).Text
newValues(
"EquipmentType") = CType(userControl.FindControl("ddlET"), DropDownList).SelectedItem.Text
newValues(
"Inactive") = CType(userControl.FindControl("cbInactive"), CheckBox).Checked
'make sure that unique primary key value is generated for the inserted row
newValues(
"EquipmentID") = (CType(Me.NDEE.Rows((Me.NDEE.Rows.Count - 1))("EquipmentID"), Integer) + 1)
Try
For Each entry As DictionaryEntry In newValues
newRow(
CType(entry.Key, String)) = entry.Value
Next
Me.NDEE.Rows.Add(newRow)
Me.NDEE.AcceptChanges()
Catch ex As Exception
Dim lblError As Label = New Label()
lblError.Text =
"Unable to insert Equipment. Reason: " + ex.Message
lblError.ForeColor = System.Drawing.Color.Red
RadGrid1.Controls.Add(lblError)
e.Canceled =
True
End Try
End Sub
Protected Sub RadGrid1_DeleteCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles RadGrid1.DeleteCommand
Dim ID As String = (CType(e.Item, GridDataItem)).OwnerTableView.DataKeyValues(e.Item.ItemIndex)("EquipmentID").ToString
Using deleteCommand As New SqlCommand("Delete_Equipment", myConnection)
deleteCommand.CommandType = CommandType.StoredProcedure
deleteCommand.Parameters.AddWithValue(
"@EID", ID)
myConnection.Open()
deleteCommand.ExecuteNonQuery()
myConnection.Close()
End Using
Dim NDEETable As DataTable = Me.NDEE
If Not (NDEETable.Rows.Find(ID) Is Nothing) Then
NDEETable.Rows.Find(ID).Delete()
NDEETable.AcceptChanges()
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = False Then
Session(
"Insert") = True
'Load Locations dropdownlist
ddlLocations.Items.Add(
New ListItem(" ", "0"))
Using selectCommand As New SqlCommand("select_Locations", myConnection)
selectCommand.CommandType = CommandType.StoredProcedure
myConnection.Open()
Using reader As SqlDataReader = selectCommand.ExecuteReader
While reader.Read
ddlLocations.Items.Add(
New ListItem(reader("WebName"), reader("LocationID")))
End While
End Using
myConnection.Close()
End Using
End If
End Sub
Protected Sub ddlLocations_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlLocations.SelectedIndexChanged
Me.RadGrid1.DataSource = Me.NDEE
Me.NDEE.PrimaryKey = New DataColumn() {Me.NDEE.Columns("EquipmentID")}
Me.RadGrid1.MasterTableView.Rebind()
End Sub
End
Class
.aspx code:
<
asp:Content ID="Content1" ContentPlaceHolderID="PageContent" Runat="Server">
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type="text/javascript">
function RowDblClick(sender, eventArgs) {
sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
}
</script>
</telerik:RadCodeBlock>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadGrid1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<asp:DropDownList ID="ddlLocations" runat="server" AutoPostBack="true" /><br /><br />
<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" ShowStatusBar="true" OnPreRender="RadGrid1_PreRender"
OnNeedDataSource="RadGrid1_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand"
OnInsertCommand="RadGrid1_InsertCommand" OnDeleteCommand="RadGrid1_DeleteCommand">
<MasterTableView Width="100%" CommandItemDisplay="Top" DataKeyNames="EquipmentID">
<Columns>
<telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
</telerik:GridEditCommandColumn>
<telerik:GridBoundColumn UniqueName="Manufacturer" HeaderText="Manufacturer" DataField="Manufacturer">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="Model" HeaderText="Model" DataField="Model">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="SerialNo" HeaderText="Serial/Batch No." DataField="SerialNo">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="TypeOrForm" HeaderText="MT or PT<br/>Form/Type" DataField="TypeOrForm">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="Megahertz" HeaderText="MHz" DataField="Megahertz">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="ProbeDiameter" HeaderText="Probe<br/>Diameter" DataField="ProbeDiameter">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="EquipmentType" HeaderText="Equipment<br/>Type" DataField="EquipmentType">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="Inactive" HeaderText="Inactive" DataField="Inactive">
</telerik:GridBoundColumn>
<telerik:GridButtonColumn UniqueName="DeleteColumn" Text="Delete" CommandName="Delete" />
</Columns>
<EditFormSettings UserControlName="NDEEUpdateForm.ascx" EditFormType="WebUserControl" >
<EditColumn UniqueName="EditCommandColumn1">
</EditColumn>
</EditFormSettings>
</MasterTableView>
<ClientSettings>
<ClientEvents OnRowDblClick="RowDblClick" />
</ClientSettings>
</telerik:RadGrid><br />
<asp:Label ID="test" runat="server" />
</
asp:Content>