how to Call telerik controls inside a template in the code - vb.net

This is my Radgrid and I am trying to call contact type, projectid and ContactName in the .vb code. Please help me obtain it.
As the entire design is under template I was not sure how we access the RadDropDownList, texboxes and ETC.
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" PageSize="50" Width="99%" OnItemUpdated="RadGrid1_ItemUpdated" OnItemInserted="RadGrid1_ItemInserted" OnItemDeleted="RadGrid1_ItemDeleted"
AutoGenerateColumns="False" AutoGenerateEditColumn="True" AutoGenerateDeleteColumn="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AllowAutomaticDeletes="True" OnDataBound="RadGrid1_DataBound">
<MasterTableView DataSourceID="SqlDataSource1" CommandItemDisplay="Top" DataKeyNames="ProjectID,ContactType">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Wrap="true" />
<EditItemStyle HorizontalAlign="Left" />
<Columns>
<telerik:GridBoundColumn DataField="ProjectID" UniqueName="ProjectID" FilterControlAltText="Filter ProjectID column" HeaderText="ProjectID" ReadOnly="True" SortExpression="ProjectID" Display="false">
<ColumnValidationSettings>
<ModelErrorMessage Text=""></ModelErrorMessage>
</ColumnValidationSettings>
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn DataField="ContactType" HeaderText="Contact Type">
<ItemTemplate>
<asp:Label Text='<%# Eval("ContactTypeDesc")%>' runat="server" />
</ItemTemplate>
<InsertItemTemplate>
<telerik:RadDropDownList ID="ddlContactType" runat="server" DropDownWidth="100%" SelectedValue='<%# Bind("ContactType") %>' DataSourceID="SqlDataSource2" DataValueField="ContactTypeID" DataTextField="ContactTypeDesc" />
<br />
<asp:RequiredFieldValidator Display="Dynamic" ErrorMessage="* Required Field" ForeColor="Red" ControlToValidate="ddlContactType" runat="server" />
</InsertItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn DataField="ContactName" HeaderText="Contact Name" SortExpression="ContactName" UniqueName="ContactName" FilterControlAltText="Filter ContactName column">
<ItemTemplate>
<asp:Label Text='<%# Eval("ContactName")%>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox ID="txtName" runat="server" Text='<%# Bind("ContactName")%>' MaxLength="100"></telerik:RadTextBox><br />
<asp:RequiredFieldValidator Display="Dynamic" ErrorMessage="* Required Field" ForeColor="Red" ControlToValidate="txtName" runat="server" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn DataField="ContactEmailAddress" HeaderText="Contact Email">
<ItemTemplate>
<asp:Label Text='<%# Eval("ContactEmailAddress")%>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox ID="txtEmail" runat="server" Text='<%# Bind("ContactEmailAddress")%>' MaxLength="100"></telerik:RadTextBox><br />
<asp:RegularExpressionValidator ID="emailValidator" runat="server" Display="Dynamic" ForeColor="Red"
ErrorMessage="Please, enter valid e-mail address." ValidationExpression="^[\w\.\-]+#[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]{1,})*(\.[a-zA-Z]{2,3}){1,2}$"
ControlToValidate="txtEmail">
</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator Display="Dynamic" ErrorMessage="* Required Field" ForeColor="Red" ControlToValidate="txtEmail" runat="server" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn DataField="ContactInternationalNumber" HeaderText="International Code">
<ItemTemplate>
<asp:Label Text='<%# String.Format("+{0}", Eval("ContactInternationalNumber"))%>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<telerik:RadDropDownList ID="ddlCountryCodes" runat="server" DropDownHeight="150px" DropDownWidth="100%" SelectedValue='<%# Bind("ContactInternationalNumber")%>' DataSourceID="SqlDataSource3" DataValueField="CountryCode" DataTextField="Country" />
<br />
<asp:RequiredFieldValidator Display="Dynamic" ErrorMessage="* Required Field" ForeColor="Red" ControlToValidate="ddlCountryCodes" runat="server" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn DataField="ContactPhoneNumber" HeaderText="Contact Phone">
<ItemTemplate>
<asp:Label Text='<%# Eval("ContactPhoneNumber")%>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox ID="txtPhoneNumber" runat="server" MaxLength="100" Text='<%# Bind("ContactPhoneNumber")%>' /><br />
<%--<telerik:RadMaskedTextBox ID="radMskTxtPhoneNumber" runat="server" SelectionOnFocus="SelectAll" PromptChar="_" Mask="(###) ###-####" Text='<%# Bind("ContactPhoneNumber")%>' />--%>
<asp:RequiredFieldValidator Display="Dynamic" ErrorMessage="* Required Field" ForeColor="Red" ControlToValidate="txtPhoneNumber" runat="server" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="CreatedBy" HeaderText="Created By" ReadOnly="true" SortExpression="CreatedBy" UniqueName="CreatedBy" FilterControlAltText="Filter CreatedBy column">
<ColumnValidationSettings>
<ModelErrorMessage Text=""></ModelErrorMessage>
</ColumnValidationSettings>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="CreateDate" HeaderText="Create Date" DataFormatString="{0:MM-dd-yyyy}" ReadOnly="true" SortExpression="CreateDate" UniqueName="CreateDate" FilterControlAltText="Filter CreateDate column" DataType="System.DateTime">
<ColumnValidationSettings>
<ModelErrorMessage Text=""></ModelErrorMessage>
</ColumnValidationSettings>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="LastModifiedBy" HeaderText="Last Modified By" ReadOnly="true" SortExpression="LastModifiedBy" UniqueName="LastModifiedBy" FilterControlAltText="Filter LastModifiedBy column">
<ColumnValidationSettings>
<ModelErrorMessage Text=""></ModelErrorMessage>
</ColumnValidationSettings>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="LastModifiedDate" HeaderText="Last Modified Date" ReadOnly="true" DataFormatString="{0:MM-dd-yyyy}" SortExpression="LastModifiedDate" UniqueName="LastModifiedDate" FilterControlAltText="Filter LastModifiedDate column" DataType="System.DateTime">
<ColumnValidationSettings>
<ModelErrorMessage Text=""></ModelErrorMessage>
</ColumnValidationSettings>
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>

Please try with the below code snippet.
Protected Sub RadGrid1_ItemInserted(source As Object, e As GridInsertedEventArgs)
Dim editformItem As GridEditFormInsertItem = DirectCast(e.Item, GridEditFormInsertItem)
Dim txtName As RadTextBox = DirectCast(editformItem.FindControl("txtName"), RadTextBox)
Dim ddlContactType As RadDropDownList = DirectCast(editformItem.FindControl("ddlContactType"), RadDropDownList)
' You can access textbox and dropdownlist here
End Sub
OR
Protected Sub RadGrid1_ItemInserted(source As Object, e As GridInsertedEventArgs)
Dim editformItem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
Dim txtName As RadTextBox = DirectCast(editformItem.FindControl("txtName"), RadTextBox)
Dim ddlContactType As RadDropDownList = DirectCast(editformItem.FindControl("ddlContactType"), RadDropDownList)
' You can access textbox and dropdownlist here
End Sub
Let me know if any concern.

Related

Newly added drop down list item attributes do not render

I have a drop down list where I am trying to add attributes to it's items. By using watch in VS2015, I can see that the attributes have been added but they are not being rendered on the page.
The DDL is in a gridview edititemtemplate. I have tried adding the attributes here with not luck. I was having trouble accessing the drop down list in the row. I ended up using a rowediting handler. I also thought the row might not be fully in edit mode so I tried making the row go into edit mode programmatically before my operations.
<asp:GridView ID="gvUserDetails" runat="server" DataSourceID="SqlUserDetails" AutoGenerateColumns="False" DataKeyNames="ID,EmpID" >
<Columns>
<asp:TemplateField HeaderText="Emp ID" SortExpression="EmpID">
<EditItemTemplate>
<asp:Label runat="server" Text='<%# Bind("EmpID") %>' ID="LabelEmpIDUpdate"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("EmpID") %>' ID="LabelEmpID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notification Name" SortExpression="NotificationName">
<EditItemTemplate>
<asp:DropDownList ID="ddNotificationNameUpdt" runat="server" SelectedValue='<%# Bind("NotificationName") %>' DataSourceID="SqlNotificationNames" DataTextField="Name" DataValueField="Name"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelNotificationName" runat="server" Text='<%# Bind("NotificationName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Filter" SortExpression="Filter">
<EditItemTemplate>
<asp:DropDownList ID="ddNotificationFilterUpdt" runat="server" DataSourceID="SqlNotificationFilters" DataTextField="Filter" DataValueField="Filter" SelectedValue='<%# Bind("Filter") %>'></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelNotificationFilter" runat="server" Text='<%# Bind("Filter") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Crew Filter" SortExpression="CrewFilter">
<EditItemTemplate>
<asp:DropDownList ID="ddCrewFilterUpdt" runat="server" SelectedValue='<%# Bind("CrewFilter") %>'>
<asp:ListItem>*</asp:ListItem>
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
<asp:ListItem>D</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelCrewFilter" runat="server" Text='<%# Bind("CrewFilter") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Begin" SortExpression="DateBgn">
<EditItemTemplate>
<asp:TextBox ID="TextBoxBgnUpdt" runat="server" Text='<%# Bind("DateBgn") %>' autocomplete="off" ></asp:TextBox>
<ajaxToolkit:CalendarExtender runat="server" BehaviorID="TextBoxBgnUpdt_CalendarExtender" TargetControlID="TextBoxBgnUpdt" ID="TextBoxBgnUpdt_CalendarExtender" PopupButtonID="TextBoxBgnUpdt"></ajaxToolkit:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelDateBgn" runat="server" Text='<%# Bind("DateBgn") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date End" SortExpression="DateEnd">
<EditItemTemplate>
<asp:TextBox ID="TextBoxEndUpdt" runat="server" Text='<%# Bind("DateEnd") %>' autocomplete="off" ></asp:TextBox>
<ajaxToolkit:CalendarExtender runat="server" BehaviorID="TextBoxEndUpdt_CalendarExtender" TargetControlID="TextBoxEndUpdt" ID="TextBoxEndUpdt_CalendarExtender"></ajaxToolkit:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelDateEnd" runat="server" Text='<%# Bind("DateEnd") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:Button ID="ButtonUpdate" runat="server" CommandName="Update" Text="Update" CausesValidation="true"/>
<asp:Button ID="ButtonCancel" runat="server" CommandName="Cancel" Text="Cancel" OnClick="ButtonCancel_Click" />
<asp:Button ID="ButtonClearEndDate" runat="server" Text="Clear End Date" OnClick="ButtonClearEndDate_Click" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="btnEditNotification" runat="server" Text="Edit" CommandName="Edit" CausesValidation="False" OnClick="btnEditNotification_Click"/>
<asp:Button ID="btnDeleteNotification" runat="server" Text="Delete" CommandName="Delete" CausesValidation="False" OnClientClick = " return confirm('Are you sure you want to delete this notification?');"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="LemonChiffon" />
</asp:GridView>
Dim gv As GridView = CType(sender, GridView)
gv.Rows(e.NewEditIndex).RowState = DataControlRowState.Edit
For i As Integer = 0 To gv.Rows.Count - 1
If i <> e.NewEditIndex Then
gridRow = gv.Rows(i)
For Each cell As Control In gridRow.Cells
For Each ctl As Control In cell.Controls
If TypeOf ctl Is DropDownList Then
foundDropList = CType(ctl, DropDownList)
If foundDropList.ID.Equals("ddNotificationNameUpdt") Then
Debug.Print("found the list")
dropListToUse = foundDropList
End If
End If
Next
Next
End If
Next
For Each item As ListItem In dropListToUse.Items
item.Attributes("Title") = NotificationNameTable(item.Text)
Next
I am expecting the user to click on the Edit button of the gridview and then dropdownlist of the row being edited to have new attributes called "Title" for each of the list items. Any help?
I was able to figure it out. If I use the RowDataBound event to catch the GridView Edit button click and make sure that the RowState and DataControlRowState.Edit are both greater then 0, I will have the GridView in the state that I want. I then will be able to find my control using e.Row.FindControl(). Then I just loop through the list items and add the attributes which are rendered correctly on screen.

how do you preselect checkboxlist items in edititemtemplate in vb.net

I have a checkboxlist in a formview itemtemplate. In edit mode I want to select the items that are in a datatable. I cannot find how to do this. I am using VB.NET.
ASP.NET code:
<asp:FormView ID="rdf" runat="server" Width="100%" DataSourceID="sqldatasource1" OnItemCreated="rdf_ModeChanging" rendermode="Lightweight" onrowupdating="updatebutton_click" DataKeyNames="applicationid" >
<ItemTemplate>
<fieldset style="width: 97%">
<div>
<br />
<asp:Label runat="server" ID="FiscalOrgNameLabel2" Text="Fiscal Organization: " Style="font-weight: bold; font-size: 15px;"></asp:Label><asp:Label Text='<%# Eval("FiscalOrgName") %>' runat="server" ID="FiscalOrgNameLabel1" />
</div>
<br />
<asp:panel id="cntyPanel" runat="server" >
<asp:label ID="countyLabel" runat="server" style="font-weight: bold; font-size: 15px; "
text="County(s): (if applicable)">
<asp:DataList ID="dlCounty" runat="server" DataSourceID="sqlCounty" Style="font-weight: normal;" >
<ItemTemplate>
<li>
<%# Eval("county") %>
</li>
</ItemTemplate>
</asp:DataList>
</asp:label>
</asp:panel>
<asp:panel id="IHEPanel" runat="server" >
<asp:label ID="instLabel" runat="server" style="font-weight:bold; font-size: 15px; "
text="Institution(s): (if applicable)">
<asp:datalist ID="dlIHE" runat="server" DataSourceID="sqlIHE" style="font-weight:normal;">
<ItemTemplate>
<li >
<%# Eval("Institute") %>
</li>
</ItemTemplate>
</asp:datalist>
</asp:label>
</asp:panel>
<asp:SqlDataSource ID="sqlCounty" runat="server" ConnectionString='<%$ ConnectionStrings:COSIGrantsDB %>' SelectCommand="SELECT dbo.County.County as county, dbo.Application_County.ApplicationId, dbo.Application_County.CountyId
FROM dbo.County INNER JOIN
dbo.Application_County ON dbo.County.CountyID = dbo.Application_County.CountyId where applicationid=#appid">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="appid" Name="appid"></asp:QueryStringParameter>
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource runat="server" ID="sqlGetBehalf" ConnectionString='<%$ ConnectionStrings:COSIGrantsDB %>' SelectCommand="getBehalf" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="appid" Name="appid" Type="Int32"></asp:QueryStringParameter>
<asp:QueryStringParameter QueryStringField="apptypeid" Name="apptypeid" Type="Int32"></asp:QueryStringParameter>
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource runat="server" ID="sqlIHE" ConnectionString='<%$ ConnectionStrings:COSIGrantsDB %>' selectcommand="SELECT dbo.Application_IHE.ApplicationID, dbo.IHE.Institute
FROM dbo.Application_IHE INNER JOIN
dbo.IHE ON dbo.Application_IHE.IHE = dbo.IHE.iheID where applicationid = #appid" SelectCommandType="text">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="appid" Name="appid" Type="Int32"></asp:QueryStringParameter>
</SelectParameters>
</asp:SqlDataSource>
<div>
<div class="rdfCommandButtons">
<hr class="rdfHr" />
<telerik:RadButton runat="server" ButtonType="SkinnedButton" RenderMode="Lightweight" CommandName="Edit" Text="Edit" ID="EditButton" ToolTip="Edit" CausesValidation="False" />
</div>
</fieldset>
</ItemTemplate>
<EditItemTemplate>
<fieldset style="width: 97%"> <br />
<asp:Label ID="editApptypeId" runat="server" Text='<%# Eval("applicationtype") %>'></asp:Label>
<div style="display:table; width:100%">
<div style="display:table-row;">
<div style="display:table-cell;">
Fiscal Organizaton:
</div>
<div class="cell textcell">
<asp:TextBox ID="fiscalOrg" runat="server" Text='<%# Bind("FiscalOrgName") %>'></asp:TextBox>
</div>
</div>
</div>
<div id="cntyPanel" visible="true" runat="server">
<div runat="server" style="font-weight: bold; font-size: 15px;">
County(s):
<asp:CheckBoxList ID="ckCounty" runat="server" RepeatColumns="8" DataTextField="county" DataValueField="countyid" ></asp:CheckBoxList>
<br />
</div>
<div id="IHEPanel" visible="false" runat="server">
<div runat="server" style="font-weight:bold; font-size: 15px;">
Institution(s):
<asp:datalist ID="dlIHE" runat="server" DataSourceID="sqlIHE" style="font-weight:normal;">
<ItemTemplate>
<li >
<%# Eval("Institute") %>
</li>
</ItemTemplate>
</asp:datalist>
</div>
</div>
<asp:SqlDataSource runat="server" ID="sqlGetBehalf" ConnectionString='<%$ ConnectionStrings:COSIGrantsDB %>' SelectCommand="getBehalf" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="appid" Name="appid" Type="Int32"></asp:QueryStringParameter>
<asp:QueryStringParameter QueryStringField="apptypeid" Name="apptypeid" Type="Int32"></asp:QueryStringParameter>
</SelectParameters>
</asp:SqlDataSource>
</div>
<asp:SqlDataSource runat="server" ID="sqlIHE" ConnectionString='<%$ ConnectionStrings:COSIGrantsDB %>' selectcommand="SELECT dbo.Application_IHE.ApplicationID, dbo.IHE.Institute
FROM dbo.Application_IHE INNER JOIN
dbo.IHE ON dbo.Application_IHE.IHE = dbo.IHE.iheID where applicationid = #appid" SelectCommandType="text">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="appid" Name="appid" Type="Int32"></asp:QueryStringParameter>
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
<EmptyDataTemplate>
<div>
<div class="rdfEmpty">There are no items to be displayed.</div>
</div>
</EmptyDataTemplate>
</asp:FormView>
VB.NET:
Protected Sub rdf_itemcommand(sender As Object, e As FormViewCommandEventArgs) Handles rdf.ItemCommand
Dim county As CheckBoxList = DirectCast(rdf.FindControl("ckCounty"), CheckBoxList)
conn.Open()
Using cmd As New SqlCommand("select * from application_County where applicationid =" + Request.QueryString("appid"), conn)
Using rdr = cmd.ExecuteReader()
If rdr.HasRows Then
Do While rdr.Read
For Each item As ListItem In county.Items
If item.Value = rdr.GetValue(2) Then
item.Selected = True
End If
Next
Loop
End If
End Using
End Using
End Sub
I get an error on county:
Object reference not set to an instance of an object.
When I debug it, county checkboxlist is empty. Why can't I find the checkboxlist in the edititemtemplate?
Without seeing the code you currently have it is a little difficult to guide you, however if you post the code I would be more than happy to help.
You are going to need a for loop in the code behind the goes through and sets everything to checked.
For each checkBoxItem as listItem in [NameOfCheckBox].Items
checkBoxItem.selected = true
next

Listview not updating databsase with Dropdown list values

When I edit items in a Listview from a Dropdown list and press the standard update button the data is not updating.
This is the structure of my database.
CREATE TABLE [dbo].[Assets]
(
[AssetID] [INT] IDENTITY(20000,1) PRIMARY KEY NOT NULL,
[Asset] NVARCHAR(20) NOT NULL,
[Symbol] NVARCHAR(20) NULL,
[AssetType] NVARCHAR(20) CONSTRAINT chk_asttype CHECK (assettype IN ('Currencies',
'Indices',
'Stocks',
'Commodities')) NOT NULL,CREATE TABLE[dbo].[times]
(
timeid [INT] IDENTITY(20010,1) PRIMARY KEY NOT NULL,
timevalues NVARCHAR (15) DEFAULT '15 Minutes' NOT NULL
)CREATE TABLE [dbo].[results]
(
[resultid] [INT] IDENTITY(20016,1) PRIMARY KEY NOT NULL,
[result] [NVARCHAR](10)CONSTRAINT chk_result CHECK (result IN ('Won',
'Lost',
'Draw',
'Prepare',
'New Signal'))
)CREATE TABLE [dbo].[binarysignals]
(
[signalid] [INT] IDENTITY(30000,1) PRIMARY KEY NOT NULL,
[assetid] [INT]CONSTRAINT fk_binastid FOREIGN KEY REFERENCES assets(assetid) NOT NULL,
[signaldate][DATE] DEFAULT Getdate() NOT NULL,
[direction][VARCHAR](4) CONSTRAINT chk_bindirection CHECK (direction IN ('Call',
'Put')) DEFAULT NULL,
[strikeprice] [SMALLMONEY] DEFAULT NULL,
[expiryprice] [SMALLMONEY] DEFAULT NULL,
[starttime][SMALLDATETIME] DEFAULT NULL,
[expirytime][NVARCHAR](20) DEFAULT NULL,
[resultid] [INT]CONSTRAINT fk_binresid FOREIGN KEY REFERENCES results(resultid) DEFAULT 20016 NOT NULL,
[lastupdatedtime][SMALLDATETIME] DEFAULT Getdate() NOT NULL,
)
And here is my ASP.Net markup code
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<script src="Scripts/jquery-1.9.1.min.js"></script>
$(document).ready(function () {
ShowTime();
});
function ShowTime() {
var dt = new Date();
document.getElementById("lblTime").innerHTML = dt.toLocaleTimeString();
window.setTimeout("ShowTime()", 1000);
}
<div>
<label id="lblTime" style="font-weight: bold"></label>
<br />
<asp:SqlDataSource ID="BO" runat="server" ConnectionString="<%$ ConnectionStrings:Signals2UConnectionString %>"
DeleteCommand="DELETE FROM [BinarySignals] WHERE [SignalID] = #SignalID" InsertCommand="INSERT INTO [BinarySignals] ([AssetID]) VALUES (#AssetID)" SelectCommand="SELECT [SignalID], BinarySignals.AssetID ,[Asset], [SignalDate], [Direction], [StrikePrice], [ExpiryPrice], [StartTime], [ExpiryTime], [Result],Results.ResultID FROM [BinarySignals]
INNER JOIN Assets On BinarySignals.AssetID=Assets.AssetID
INNER JOIN Results On BinarySignals.ResultID=Results.ResultID
ORDER BY BinarySignals.AssetID DESC"
UpdateCommand="UPDATE [BinarySignals] SET [AssetID] = #AssetID, [SignalDate] = #SignalDate, [Direction] = #Direction, [StrikePrice] = #StrikePrice, [ExpiryPrice] = #ExpiryPrice, [StartTime] = #StartTime, [ExpiryTime] = #ExpiryTime, [ResultID] = #ResultID WHERE [SignalID] = #SignalID">
<DeleteParameters>
<asp:Parameter Name="SignalID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="AssetID" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="AssetID" Type="Int32" />
<asp:Parameter DbType="Date" Name="SignalDate" />
<asp:Parameter Name="Direction" Type="String" />
<asp:Parameter Name="StrikePrice" Type="Decimal" />
<asp:Parameter Name="ExpiryPrice" Type="Decimal" />
<asp:Parameter Name="StartTime" Type="DateTime" />
<asp:Parameter Name="ExpiryTime" Type="String" />
<asp:Parameter Name="ResultID" Type="Int32" />
<asp:Parameter Name="SignalID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="Results" runat="server" ConnectionString="<%$ ConnectionStrings:Signals2UConnectionString %>" SelectCommand="SELECT [Result], [ResultID] FROM [Results]"></asp:SqlDataSource>
<asp:ListView ID="ListView1" runat="server" DataSourceID="BO" GroupItemCount="3" InsertItemPosition="LastItem">
<AlternatingItemTemplate>
<td id="Td1" runat="server" style="">SignalID:
<asp:Label ID="SignalIDLabel" runat="server" Text='<%# Eval("SignalID") %>' />
<br />
Asset:
<asp:Label ID="AssetLabel" runat="server" Text='<%# Eval("Asset") %>' />
<br />
SignalDate:
<asp:Label ID="SignalDateLabel" runat="server" Text='<%# Eval("SignalDate","{0:yyyy-MM-dd}") %>' />
<br />
Direction:
<asp:Label ID="DirectionLabel" runat="server" Text='<%# Eval("Direction") %>' />
<br />
StrikePrice:
<asp:Label ID="StrikePriceLabel" runat="server" Text='<%# Eval("StrikePrice") %>' />
<br />
ExpiryPrice:
<asp:Label ID="ExpiryPriceLabel" runat="server" Text='<%# Eval("ExpiryPrice") %>' />
<br />
StartTime:
<asp:Label ID="StartTimeLabel" runat="server" Text='<%# Eval("StartTime") %>' />
<br />
ExpiryTime:
<asp:Label ID="ExpiryTimeLabel" runat="server" Text='<%# Eval("ExpiryTime") %>' />
<br />
Result:
<asp:Label ID="ResultLabel" runat="server" Text='<%# Eval("Result") %>' />
<br />
ResultID:
<asp:Label ID="ResultIDLabel" runat="server" Text='<%# Eval("ResultID") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
</td>
</AlternatingItemTemplate>
<EditItemTemplate>
<td id="Td2" runat="server" style="">SignalID:
Asset:
<asp:TextBox ID="AssetTextBox" runat="server" ReadOnly="true" Text='<%# Bind("Asset") %>' />
<br />
SignalDate:
<asp:TextBox ID="SignalDateTextBox" runat="server" Text='<%# Bind("SignalDate") %>' />
<br />
Direction:
<asp:TextBox ID="DirectionTextBox" runat="server" Text='<%# Bind("Direction") %>' />
<br />
StrikePrice:
<asp:TextBox ID="StrikePriceTextBox" runat="server" Text='<%# Bind("StrikePrice") %>' />
<br />
ExpiryPrice:
<asp:TextBox ID="ExpiryPriceTextBox" runat="server" Text='<%# Bind("ExpiryPrice") %>' />
<br />
StartTime:
<asp:TextBox ID="StartTimeTextBox" runat="server" Text='<%# Bind("StartTime") %>' />
<br />
ExpiryTime:
<asp:TextBox ID="ExpiryTimeTextBox" runat="server" Text='<%# Bind("ExpiryTime") %>' />
<br />
Result:
<asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="Outcome" DataTextField="Result" DataValueField="ResultID" SelectedValue='<%#Bind("ResultID") %>'></asp:DropDownList>
<br />
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<br />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
<br />
</td>
</EditItemTemplate>
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<EmptyItemTemplate>
<td id="Td3" runat="server" />
</EmptyItemTemplate>
<GroupTemplate>
<tr id="itemPlaceholderContainer" runat="server">
<td id="itemPlaceholder" runat="server"></td>
</tr>
</GroupTemplate>
<InsertItemTemplate>
<td id="Td4" runat="server" style="">Asset:
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="Assets" DataTextField="Asset" DataValueField="AssetID" AutoPostBack="True" SelectedValue='<%# Bind("AssetID") %>'>
</asp:DropDownList>
<br />
SignalDate:
<asp:Label ID="SignalDateLabel" runat="server" Text='<%# Bind("SignalDate") %>' />
<br />
Direction:
<asp:TextBox ID="DirectionTextBox" runat="server" Text='<%# Bind("Direction") %>' />
<br />
StrikePrice:
<asp:TextBox ID="StrikePriceTextBox" runat="server" Text='<%# Bind("StrikePrice") %>' />
<br />
ExpiryPrice:
<asp:TextBox ID="ExpiryPriceTextBox" runat="server" Text='<%# Bind("ExpiryPrice") %>' />
<br />
StartTime:
<asp:TextBox ID="StartTimeTextBox" runat="server" Text='<%# Bind("StartTime") %>' />
<br />
ExpiryTime:
<asp:TextBox ID="ExpiryTimeTextBox" runat="server" Text='<%# Bind("ExpiryTime") %>' />
<br />
Result:
<asp:TextBox ID="ResultTextBox" runat="server" Text='<%# Bind("Result") %>' />
<br />
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<br />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
<br />
</td>
</InsertItemTemplate>
<ItemTemplate>
<td id="Td5" runat="server" style="">SignalID:
<asp:Label ID="SignalIDLabel" runat="server" Text='<%# Eval("SignalID") %>' />
<br />
Asset:
<asp:Label ID="AssetLabel" runat="server" Text='<%# Eval("Asset") %>' />
<br />
SignalDate:
<asp:Label ID="SignalDateLabel" runat="server" Text='<%# Eval("SignalDate","{0:yyyy-MM-dd}") %>' />
<br />
Direction:
<asp:Label ID="DirectionLabel" runat="server" Text='<%# Eval("Direction") %>' />
<br />
StrikePrice:
<asp:Label ID="StrikePriceLabel" runat="server" Text='<%# Eval("StrikePrice") %>' />
<br />
ExpiryPrice:
<asp:Label ID="ExpiryPriceLabel" runat="server" Text='<%# Eval("ExpiryPrice") %>' />
<br />
StartTime:
<asp:Label ID="StartTimeLabel" runat="server" Text='<%# Eval("StartTime") %>' />
<br />
ExpiryTime:
<asp:Label ID="ExpiryTimeLabel" runat="server" Text='<%# Eval("ExpiryTime") %>' />
<br />
Result:
<asp:Label ID="ResultLabel" runat="server" Text='<%# Eval("Result") %>' />
<br />
ResultID:
<asp:Label ID="ResultIDLabel" runat="server" Text='<%#Eval("ResultID") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
</td>
</ItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td6" runat="server">
<table id="groupPlaceholderContainer" runat="server" border="0" style="">
<tr id="groupPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr2" runat="server">
<td id="Td7" runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<td id="Td8" runat="server" style="">SignalID:
<asp:Label ID="SignalIDLabel" runat="server" Text='<%# Eval("SignalID") %>' />
<br />
AssetID:
<asp:Label ID="AssetIDLabel" runat="server" Text='<%# Eval("AssetID") %>' />
<br />
Asset:
<asp:Label ID="AssetLabel" runat="server" Text='<%# Eval("Asset") %>' />
<br />
SignalDate:
<asp:Label ID="SignalDateLabel" runat="server" Text='<%# Eval("SignalDate","{0:yyyy-MM-dd}") %>' />
<br />
Direction:
<asp:Label ID="DirectionLabel" runat="server" Text='<%# Eval("Direction") %>' />
<br />
StrikePrice:
<asp:Label ID="StrikePriceLabel" runat="server" Text='<%# Eval("StrikePrice") %>' />
<br />
ExpiryPrice:
<asp:Label ID="ExpiryPriceLabel" runat="server" Text='<%# Eval("ExpiryPrice") %>' />
<br />
StartTime:
<asp:Label ID="StartTimeLabel" runat="server" Text='<%# Eval("SignalDate") %>' />
<br />
ExpiryTime:
<asp:Label ID="ExpiryTimeLabel" runat="server" Text='<%# Eval("ExpiryTime") %>' />
<br />
Result:
<asp:Label ID="ResultLabel" runat="server" Text='<%# Eval("Result") %>' />
<br />
ResultID:
<asp:Label ID="ResultIDLabel" runat="server" Text='<%# Eval("ResultID") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
</td>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="Assets" runat="server" ConnectionString="<%$ ConnectionStrings:Signals2UConnectionString %>" SelectCommand="SELECT [AssetID], [Asset], [AssetType], [Symbol] FROM [Assets]"></asp:SqlDataSource>
<asp:SqlDataSource ID="times" runat="server" ConnectionString="<%$ ConnectionStrings:Signals2UConnectionString %>" SelectCommand="SELECT DISTINCT [TimeID], [TimeValues] FROM [Times]"></asp:SqlDataSource>
<asp:SqlDataSource ID="Outcome" runat="server" ConnectionString="<%$ ConnectionStrings:Signals2UConnectionString %>" SelectCommand="SELECT [ResultID], [Result] FROM [Results]"></asp:SqlDataSource>
</div>
I try to update the Results value which is a Foreign key stored in the Binary Signals table coming from the Results table. Result field is used as the display text and the ResultId as the value for the dropdown list
Solved. The below was missing from the EditItemTemplate
<asp:Label ID="SignalIDLabel" runat="server" Text='<%# Bind("SignalID")%>'/>

To view dropdownlist value in gridview (visual studio 2012)

i have two tables dbo.rank( rankID,rankName) and dbo.tbStaff (Staff ID, RankID, StaffName). And dropdownlist which diplays rankName. But in gridview it is showing rankID. How do I make it show rankName as in dropdownlist. Pls HELP!!!
Here is my dropdownlist code:
<asp:DropDownList ID="DropDownList1" runat="server" Height="29px" style="margin-left: 53px" Width="422px"
DataSourceID="SqlDataSource1" DataTextField="RankName" DataValueField="RankID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ProjectSPConnectionString %>"
SelectCommand="SELECT [RankID],[RankName] FROM [tbRank] ORDER BY [RankID]"
ProviderName="<%$ ConnectionStrings:ProjectSPConnectionString.ProviderName %>">
</asp:SqlDataSource>
And my gv:
you have to customize your grid try this
<asp:GridView ID="gvStaff" runat="server" AutoGenerateColumns ="false">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label Text='<%# Eval("RankID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label Text='<%# Eval("RankName") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Grid view won't show

I'm creating a website with two pages using a master page. I want to display all the content of my table in gridview on the second page upon logging in.
I don't know what went wrong and why my gridview on the second page won't display. But if I do it on the first page, it's working fine.
Imports System.Data
Imports System.Xml
Partial Class Default2
Inherits System.Web.UI.Page
Dim conn2 As New OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=" & Server.MapPath("~/App_Data/Tony.mdb"))
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("uname") = "" Then
iduser.Text = "Sorry you are not logged-in to view this page."
ElseIf Session("uname") = "Wrong username and/or password. Please try again." Or Session("uname") = "Successfully signed-up! Go back to the home page to log-in" Then
iduser.Text = Session("uname")
Else
Dim cload As New OleDb.OleDbDataAdapter("select * from sample where username='" & Session("uname") & "'", conn2)
Dim tester As New DataSet
iduser.Text = "Welcome " & Session("uname") & "!"
tester.Clear()
cload.Fill(tester)
Session("valid") = tester.Tables(0).DefaultView.Item(0).Item(9)
If Session("valid") = "y" Then
results.DataSource = tester.Tables(0).DefaultView
results.DataBind()
lfn.Visible = True
lln.Visible = True
lag.Visible = True
End If
End If
End Sub
Next is the aspx only now I have added a work around though I still want a clear explaination why wont the gridview show. As you can see I added a data source to the object as a result I didnt need to code the datasource or to databind anymore. Still, I prefer everything to be hard-coded.
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<p style="height: 39px">
<asp:Label ID="iduser" runat="server"></asp:Label>
<asp:GridView ID="results" runat="server" AutoGenerateColumns="False"
Width="249px" EmptyDataText="No Records" DataSourceID="AccessDataSource1">
<Columns>
<asp:BoundField DataField="fname" HeaderText="fname" SortExpression="fname" />
<asp:BoundField DataField="lname" HeaderText="lname" SortExpression="lname" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
<asp:BoundField DataField="gender" HeaderText="gender"
SortExpression="gender" />
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/Tony.mdb"
SelectCommand="SELECT [fname], [lname], [age], [gender] FROM [sample] WHERE ([username] = ?)">
<SelectParameters>
<asp:SessionParameter Name="username" SessionField="uname" Type="String" />
</SelectParameters>
</asp:AccessDataSource>
<asp:Button ID="bsearch" runat="server" Text="Search:" Visible="False" />
<asp:TextBox ID="search" runat="server" Visible="False"></asp:TextBox>
</p>
<br />
<asp:ListBox ID="listsearch" runat="server" Width="216px"></asp:ListBox>
<br /><h2>
<asp:Label ID="Label1" runat="server" Text="Edit Profile Here:" Visible="False"></asp:Label>
</h2>
<p>
<asp:Label ID="lfn" runat="server" Text="First Name: "
Visible="False"></asp:Label>
<asp:TextBox ID="fname" runat="server" Enabled="False" Visible="False"></asp:TextBox>
<asp:Label ID="fn" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
<asp:Label ID="lln" runat="server" Text="Last Name: "
Visible="False"></asp:Label>
<asp:TextBox ID="lname" runat="server" Enabled="False" Visible="False"></asp:TextBox>
<asp:Label ID="ln" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
<asp:Label ID="lag" runat="server" Text="Age:" Visible="False"></asp:Label>
<asp:TextBox ID="age" runat="server" Enabled="False" Visible="False"></asp:TextBox>
<asp:Label ID="ag" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
<asp:Label ID="lgen" runat="server" Text="Gender: " Visible="False"></asp:Label>
<asp:DropDownList ID="gender" runat="server" Enabled="False" Visible="False">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
</p>
<p>
<asp:Label ID="lem" runat="server" Text="Email:" Visible="False"></asp:Label>
<asp:TextBox ID="email" runat="server" Enabled="False" Visible="False"></asp:TextBox>
<asp:Label ID="em" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
<asp:Label ID="lun" runat="server" Text="Username:" Visible="False"></asp:Label>
<asp:TextBox ID="username" runat="server" Enabled="False" Visible="False"></asp:TextBox>
<asp:Label ID="un" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
<asp:Label ID="lpw" runat="server" Text="Password:" Visible="False"></asp:Label>
<asp:TextBox ID="pass" runat="server" TextMode="Password" Enabled="False"
Visible="False"></asp:TextBox>
<asp:Label ID="pw" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
<asp:Label ID="lpw2" runat="server" Text="Re-type Pass:" Visible="False"></asp:Label>
<asp:TextBox ID="pass2" runat="server" TextMode="Password" Enabled="False"
Visible="False"></asp:TextBox>
<asp:Label ID="pw2" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
<asp:Label ID="ladm" runat="server" Text="Admin?" Visible="False"></asp:Label>
<asp:DropDownList ID="admin" runat="server" Visible="False">
<asp:ListItem>n</asp:ListItem>
<asp:ListItem>y</asp:ListItem>
</asp:DropDownList>
</p>
<p>
<asp:Button ID="bedit" runat="server" Text="Edit" Visible="False" />
<asp:Button ID="badd" runat="server" Text="Add Record" Visible="False" />
<asp:Button ID="bdelete" runat="server" Text="Delete Record" Visible="False" />
</p>
</asp:Content>
You might have forgot to open the connection? Can you edit this
conn2.Open
Before this line:
Dim cload As New OleDb.OleDbDataAdapter("select * from sample where username='" & Session("uname") & "'", conn2)