Getting this error message and I am not able to figure out why. Here is my code:
<asp:TextBox ID="searchParam" Width="250px" runat="server"></asp:TextBox><asp:button ID="btnSearch" runat="server" Text="Search" />
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="getCOMDLs" TypeName="NewEmployee">
<SelectParameters>
<asp:FormParameter FormField="searchParam" Type="String" DefaultValue="" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:GridView ID="dgSearchDLs" runat="server" DataKeyNames="cn" AllowPaging="false" DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="cn" HeaderText="DL Name"/>
<asp:BoundField DataField="managedBy" HeaderText="Managed By"/>
<asp:BoundField DataField="info" HeaderText="Notes"/>
<asp:ButtonField ButtonType="Button" text="Add" HeaderText = "Select DL" CommandName="AddDL" />
</Columns>
</asp:GridView>
NewEmployee Class:
Function getCOMDLs(ByVal searchParam As String) As DataTable
...
End Function
After researching this issue/trial and error, the solution was adding Name="searchParam" to the form parameter control.
Related
I'm trying to read the datetime of every row and write it to the screen but I keep getting System.NullReferenceException: Object reference not set to an instance of an object. There is data in the grid so I'm unsure why it doesn't find anything in column 'orderDate'.
For Each r As GridViewRow in gdvOrders.Rows
If r.RowType = DataControlRowType.DataRow Then
Dim orderDate As datetime
orderDate = r.Cells(7).FindControl("orderDate").ToString
Dim test as string=orderDate.ToString("yyyy-MM-dd HH:mm:ss")
response.write(test)
End If
Next r
<asp:GridView ID="gdvSentOrders" width="100%" runat="server" style="font-size:1.5em" ShowHeaderWhenEmpty="True" EmptyDataText="No orders" AllowPaging="True" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="orderID" DataSourceID="DSSentOrders" PageSize="20" AllowSorting="True">
<AlternatingRowStyle CssClass="alt" />
<Columns>
<asp:TemplateField HeaderText="Order">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("stallMessage") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="price" HeaderText="Price" />
<asp:BoundField DataField="city" HeaderText="Address" />
<asp:BoundField DataField="postcode" HeaderText="Postcode" />
<asp:BoundField DataField="phoneNumber" HeaderText="Phone No" />
<asp:BoundField DataField="tblNo" HeaderText="Table" />
<asp:BoundField DataField="type" HeaderText="Type" />
<asp:BoundField DataField="orderDate" DataFormatString="{0: H:mm:ss}" HeaderText="Order Time" SortExpression="orderDate" />
<asp:BoundField DataField="timeFor" DataFormatString="{0: H:mm:ss}" HeaderText="Time For" SortExpression="timeFor" />
<asp:BoundField DataField="paid" HeaderText="Paid" />
</Columns>
</asp:GridView>
Looking to convert dozens of pages from aspx AccessDataSource to vb.net code behind. I'm using OleDB and ADO on other pages. wanted to know if there is any canned code to bind a simple select statement to the GridView1
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="AccessDataSource1" BackColor="White" BorderColor="#999999"
BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
GridLines="Vertical" AllowSorting="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="RowLevelCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UsersDataTbl.LastName" HeaderText="Last Name" SortExpression="LastName" />
<asp:BoundField DataField="UsersDataTbl.FirstName" HeaderText="First Name" SortExpression="FirstName" />
<asp:BoundField DataField="UsersDataTbl.Affiliation" HeaderText="Affiliation" SortExpression="Affiliation" />
<asp:BoundField DataField="UsersDataTbl.UID" HeaderText="UID" SortExpression="UID" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#6699CC" />
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="<%$ ConnectionStrings:AccessSubSiteString %>"
SelectCommand="SELECT [UsersDataTbl.LastName], [UsersDataTbl.FirstName], [UsersDataTbl.Affiliation], [UsersDataTbl.UID], [ALSProviders.ALS]
FROM [UsersDataTbl] LEFT JOIN [ALSProviders] ON (UsersDataTbl.UID = ALSProviders.UID) WHERE [UsersDataTbl.Career] = True
AND [UsersDataTbl.Active] = True AND ([UsersDataTbl.UID] NOT IN (SELECT UID FROM [ALSProviders]))
ORDER BY [UsersDataTbl.LastName]" >
<SelectParameters>
</SelectParameters>
</asp:AccessDataSource>
This was working correctly, but now its inserting duplicate rows everytime I perform an insert from the page. Both rows are good with all data inserted correctly, but its putting the record in the database twice each time. How do I stop this?
VB.net code:
Protected Sub gvOnCallSchedule_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Handles gvOnCallSchedule.RowCommand
If e.CommandName = "Insert" AndAlso Page.IsValid Then
Try
dsOncallGroup.Insert()
Catch ex As Exception
MsgBox(Err.Description, MsgBoxStyle.Critical, "SQL Error")
End Try
End If
If e.CommandName = "Delete" Then
Try
dsOncallGroup.Delete()
Catch ex As Exception
MsgBox(Err.Description, MsgBoxStyle.Critical, "SQL Error")
End Try
'Response.Redirect("Default.aspx")
End If
End Sub
Protected Sub dsOnCallGroup_Inserting(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs) Handles dsOncallGroup.Inserting
Dim strddlGroupName As New DropDownList
Dim txtStartOnCall As New TextBox
Dim txtEndOnCall As New TextBox
Dim txtRank As New TextBox
Dim strddlEmpName As New DropDownList
Dim strddlOCPreferredContact As New DropDownList
Dim txtEmpWorkPhone As New TextBox
Dim txtEmpHomePhone As New TextBox
Dim txtEmpHomeCellPhone As New TextBox
Dim txtEmpAltPhone As New TextBox
strddlGroupName = CType(gvOnCallSchedule.FooterRow.FindControl("ftrddlOCGroup"), DropDownList)
txtStartOnCall = CType(gvOnCallSchedule.FooterRow.FindControl("txtStartOnCallFtr"), TextBox)
txtEndOnCall = CType(gvOnCallSchedule.FooterRow.FindControl("txtEndOnCallFtr"), TextBox)
txtRank = CType(gvOnCallSchedule.FooterRow.FindControl("txtCallOrderFtr"), TextBox)
strddlEmpName = CType(gvOnCallSchedule.FooterRow.FindControl("ddlAnalystFtr"), DropDownList)
strddlOCPreferredContact = CType(gvOnCallSchedule.FooterRow.FindControl("ftrddlOCPreferredContact"), DropDownList)
txtEmpWorkPhone = CType(gvOnCallSchedule.FooterRow.FindControl("txtWorkPhoneFtr"), TextBox)
txtEmpHomePhone = CType(gvOnCallSchedule.FooterRow.FindControl("txtHomePhoneFtr"), TextBox)
txtEmpHomeCellPhone = CType(gvOnCallSchedule.FooterRow.FindControl("txtHomeCellPhoneFtr"), TextBox)
txtEmpAltPhone = CType(gvOnCallSchedule.FooterRow.FindControl("txtAltPhoneFtr"), TextBox)
e.Command.Parameters("#fldOnCallGroup").Value = strddlGroupName.Text
e.Command.Parameters("#fldStartOnCall").Value = txtStartOnCall.Text
e.Command.Parameters("#fldEndOnCall").Value = txtEndOnCall.Text
e.Command.Parameters("#fldRank").Value = txtRank.Text
e.Command.Parameters("#fldEmpName").Value = strddlEmpName.Text
e.Command.Parameters("#fldOCPreferredContact").Value = strddlOCPreferredContact.Text
e.Command.Parameters("#fldEmpWorkPhone").Value = txtEmpWorkPhone.Text
e.Command.Parameters("#fldEmpHomePhone").Value = txtEmpHomePhone.Text
e.Command.Parameters("#fldEmpHomeCellPhone").Value = txtEmpHomeCellPhone.Text
e.Command.Parameters("#fldEmpAltPhone").Value = txtEmpAltPhone.Text
End Sub
ASP code
Datasource:
<asp:SqlDataSource ID="dsOncallGroup" runat="server"
ConnectionString="<%$ ConnectionStrings:DiscussSQLConnectionString %>"
SelectCommand="SELECT * FROM tblOnCallSchedule WHERE (tblOnCallSchedule.fldOnCallGroup = #fldOnCallGroup)
AND (tblOnCallSchedule.fldEndOnCall > GETDATE())
ORDER BY tblOnCallSchedule.fldOnCallGroup, tblOnCallSchedule.fldEndOnCall, tblOnCallSchedule.fldRank"
OldValuesParameterFormatString="original_{0}"
DeleteCommand="DELETE FROM tblOnCallSchedule WHERE fldOCID = #original_fldOCID"
InsertCommand="INSERT INTO [tblOnCallSchedule] ([fldOnCallGroup], [fldStartOnCall], [fldEndOnCall], [fldRank], [fldEmpName], [fldOCPreferredContact], [fldEmpWorkPhone],
[fldEmpHomePhone],[fldEmpHomeCellPhone],[fldEmpAltPhone])
VALUES (#fldOnCallGroup, #fldStartOnCall, #fldEndOnCall, #fldRank, #fldEmpName, #fldOCPreferredContact, #fldEmpWorkPhone, #fldEmpHomePhone,
#fldEmpHomeCellPhone, #fldEmpAltPhone)"
UpdateCommand="UPDATE [tblOnCallSchedule] SET [fldOnCallGroup] = #fldOnCallGroup, [fldStartOnCall] = #fldStartOnCall,
[fldEndOnCall] = #fldEndOnCall, [fldRank] = #fldRank, [fldEmpName] = #fldEmpName, [fldOCPreferredContact] = #fldOCPreferredContact,
[fldEmpWorkPhone] = #fldEmpWorkPhone,[fldEmpHomePhone] = #fldEmpHomePhone,[fldEmpHomeCellPhone] = #fldEmpHomeCellPhone,
[fldEmpAltPhone] = #fldEmpAltPhone
WHERE [fldOCID] = #original_fldOCID">
<SelectParameters>
<asp:ControlParameter ControlID="cboOncallGroup" Name="fldOnCallGroup"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name= "original_fldOCID" Type="Int16" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="fldOnCallGroup" Type="String" ConvertEmptyStringToNull="false" />
<asp:Parameter Name="fldStartOnCall" DbType ="Date" />
<asp:Parameter Name="fldEndOnCall" DbType ="Date" />
<asp:Parameter Name= "fldRank" Type="Int16" />
<asp:Parameter Name= "fldEmpName" Type="String" />
<asp:Parameter Name= "fldOCPreferredContact" Type="String" />
<asp:Parameter Name= "fldEmpWorkPhone" Type="String" />
<asp:Parameter Name= "fldEmpHomePhone" Type="String" />
<asp:Parameter Name= "fldEmpHomeCellPhone" Type="String" />
<asp:Parameter Name= "fldEmpAltPhone" Type="String" />
<asp:Parameter Name="original_fldOnCallGroup" Type="String" ConvertEmptyStringToNull="false" />
<asp:Parameter Name="original_fldStartOnCall" DbType="Date" />
<asp:Parameter Name="original_fldEndOnCall" DbType="Date" />
<asp:Parameter Name="original_fldRank" Type="Int16" />
<asp:Parameter Name="original_fldEmpName" Type="String" />
<asp:Parameter Name="original_fldOCPreferredContact" Type="String" />
<asp:Parameter Name="original_fldEmpWorkPhone" Type="String" />
<asp:Parameter Name="original_fldEmpHomePhone" Type="String" />
<asp:Parameter Name="original_fldEmpHomeCellPhone" Type="String" />
<asp:Parameter Name="original_fldEmpAltPhone" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="fldOnCallGroup" Type="String" />
<asp:Parameter DbType="Date" Name="fldStartOnCall" />
<asp:Parameter DbType="Date" Name="fldEndOnCall" />
<asp:Parameter Name="fldRank" Type="Int16" />
<asp:Parameter Name="fldEmpName" Type="String" />
<asp:Parameter Name="fldOCPreferredContact" Type="String" />
<asp:Parameter Name="fldEmpWorkPhone" Type="String" />
<asp:Parameter Name="fldEmpHomePhone" Type="String" />
<asp:Parameter Name="fldEmpHomeCellPhone" Type="String" />
<asp:Parameter Name="fldEmpAltPhone" Type="String" />
<asp:Parameter Name="original_fldOnCallGroup" Type="String" ConvertEmptyStringToNull="false" />
<asp:Parameter Name="original_fldStartOnCall" DbType="Date" />
<asp:Parameter Name="original_fldEndOnCall" DbType="Date" />
<asp:Parameter Name="original_fldRank" Type="Int16" />
<asp:Parameter Name="original_fldEmpName" Type="String" />
<asp:Parameter Name="original_fldOCPreferredContact" Type="String" />
<asp:Parameter Name="original_fldEmpWorkPhone" Type="String" />
<asp:Parameter Name="original_fldEmpHomePhone" Type="String" />
<asp:Parameter Name="original_fldEmpHomeCellPhone" Type="String" />
<asp:Parameter Name="original_fldEmpAltPhone" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
Gridview Code:
<asp:Gridview ID="gvOnCallSchedule" runat="server" AllowSorting="True" AllowPaging="True" showfooter="true"
OnRowUpdating="GvOnCall_Update" onRowCommand="gvOnCallSchedule_RowCommand" DataKeyNames="fldOCID"
BackColor="Aqua" DataSourceID="dsOncallGroup" Font-Bold="True"
Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" ForeColor="#333333" HorizontalAlign="Justify"
CellPadding="4" GridLines="None" PageSize="20" AutoGenerateColumns ="false"
OnSelectedIndexChanged="cboOnCallGroup_SelectedIndexChanged">
<Columns>
<asp:TemplateField ShowHeader="false">
<FooterTemplate>
<asp:ImageButton ID="AddButton" runat="server" CommandName="Insert" ImageURL="~/images/New.png"
Text="Add" ToolTip="Add New On Call Record" />
</FooterTemplate>
<EditItemTemplate>
<asp:ImageButton ID="UpdateButton" runat="server" CausesValidation="False" CommandName="Update"
ImageUrl="~/images/Save.png" Text="Update" ToolTip="Update" />
<asp:ImageButton ID="CancelButton" runat="server" CausesValidation="false" CommandName="Cancel"
ImageURL="~/images/Cancel.png" Text="Cancel" ToolTip="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
ImageUrl="~/images/Edit.gif" Text="Edit" ToolTip="Edit" />
<asp:ImageButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
ImageUrl="~/images/Delete.png" Text="Delete" ToolTip="Delete" />
</ItemTemplate>
<ItemStyle Wrap="false" />
</asp:TemplateField>
<asp:Boundfield headertext="fldOCID" datafield="fldOCID" sortexpression="fldOCID" visible="false" InsertVisible="False" />
<asp:TemplateField headertext="On Call Group">
<ItemTemplate>
<asp:DropDownList ID="ddlOCGroup" width="200px" runat="server" datavaluefield="fldOnCallGroup" Enabled="false"
DataSourceID= "dsListbox" SelectedValue="<%# Bind('fldOnCallGroup') %>">
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ftrddlOCGroup" width="200px" runat="server" datavaluefield="fldOnCallGroup"
DataSourceID= "dsListbox" SelectedValue="<%# Bind('fldOnCallGroup') %>">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Start Date/Time">
<ItemTemplate>
<asp:Textbox ID="txtStartOnCall" runat="server" Text='<%# Bind("fldStartOnCall") %>'>
</asp:Textbox>
<asp:Image ID="calpopup" runat="server" ImageUrl="~/images/calendar2.png" />
<asp:CalendarExtender ID="StartOnCallCal" runat="server" TargetControlID="txtStartOnCall"
PopupButtonID="calpopup" PopupPosition="Right" format="MM/dd/yyyy">
</asp:CalendarExtender>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtStartOnCallFtr" runat="server" Text='<%# Bind("fldStartOncall") %>'>
</asp:Textbox>
<asp:Image ID="calpopupFtr" runat="server" ImageUrl="~/images/calendar2.png" />
<asp:CalendarExtender ID="StartOnCallCalFtr" runat="server" TargetControlID="txtStartOnCallFtr"
PopupButtonID="calpopupFtr" PopupPosition="Right" format="MM/dd/yyyy">
</asp:CalendarExtender>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="End Date/Time">
<ItemTemplate>
<asp:Textbox ID="txtEndOnCall" runat="server" Text='<%# Bind("fldEndOnCall") %>'>
</asp:Textbox>
<asp:Image ID="calpopup2" runat="server" ImageUrl="~/images/calendar2.png" />
<asp:CalendarExtender ID="EndOnCallCal" runat="server" TargetControlID="txtEndOnCall"
PopupButtonID="calpopup2" PopupPosition="Right" format="MM/dd/yyyy">
</asp:CalendarExtender>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtEndOnCallFtr" runat="server" Text='<%# Bind("fldEndOnCall") %>'>
</asp:Textbox>
<asp:Image ID="calpopup2Ftr" runat="server" ImageUrl="~/images/calendar2.png" />
<asp:CalendarExtender ID="EndOnCallCalFtr" runat="server" TargetControlID="txtEndOnCallFtr"
PopupButtonID="calpopup2Ftr" PopupPosition="Right" format="MM/dd/yyyy">
</asp:CalendarExtender>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Call Order">
<ItemTemplate>
<asp:Textbox ID="lblCallOrder" runat="server" width="100" Text='<%# Bind("fldRank") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtCallOrderFtr" runat="server" width="100" Text='<%# Bind("fldOnCallGroup") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OnCall Analyst">
<ItemTemplate>
<asp:DropDownList ID="ddlAnalyst" runat="server" datavaluefield="fldEmpName"
datasourceID="dsEmp" SelectedValue='<%# Bind("fldEmpName") %>'>
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlAnalystFtr" runat="server" datavaluefield="fldEmpName"
DataSourceID= "dsEmp" SelectedValue='<%# Bind("fldEmpName") %>'>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Preferred Contact">
<ItemTemplate>
<asp:DropDownList ID="ddlOCPreferredContact" Width="150px" runat="server" datavaluefield="fldOCPreferredContact"
datasourceID="dsPref" SelectedValue='<%# Bind("fldOCPreferredContact") %>'>
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ftrddlOCPreferredContact" width="150px" runat="server" datavaluefield="fldOCPreferredContact"
DataSourceID= "dsPref" SelectedValue='<%# Bind("fldOCPreferredContact") %>'>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Work Phone">
<ItemTemplate>
<asp:Textbox ID="lblWorkPhone" runat="server" width="90" Text='<%# Bind("fldEmpWorkPhone") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtWorkPhoneFtr" runat="server" width="90" Text='<%# Bind("fldEmpWorkPhone") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Home Phone">
<ItemTemplate>
<asp:Textbox ID="lblHomePhone" runat="server" width="90" Text='<%# Bind("fldEmpHomePhone") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtHomePhoneFtr" runat="server" width="90" Text='<%# Bind("fldEmpHomePhone") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Personal Cell">
<ItemTemplate>
<asp:Textbox ID="lblHomeCellPhone" runat="server" width="95" Text='<%# Bind("fldEmpHomeCellPhone") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtHomeCellPhoneFtr" runat="server" width="95" Text='<%# Bind("fldEmpHomeCellPhone") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Alternate Phone">
<ItemTemplate>
<asp:Textbox ID="lblAltPhone" runat="server" width="105" Text='<%# Bind("fldEmpAltPhone") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtAltPhoneFtr" runat="server" width="105" Text='<%# Bind("fldEmpAltPhone") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:Gridview>
Put a break point on the call to dsOncallGroup.Insert() and see if it is called twice, if so then it is your code doing it and you can figure out where the second call is happening by examining the Call Stack window (Debug -> Windows -> Call Stack).
If there are not two calls in the code, then you need to investigate if a trigger has been added to your database that is somehow causing extra data to be inserted.
I'm a newbie, but need some assistance. I have researched the request but only found one response, which looks like it can be done, but I'm not sure how to do it physically in vb and VS2010.
Basically, I have a webgrid with a download link along with a filename. I want the download link to be disabled if there is no filename as there is nothing to download (obviosuly).
This resolution I found was this - Here - However, don't really follow it, specifically because im doing it in vb too.
Any assistance on this would be great.
Thanks
My code for the gridview.
<asp:GridView ID="SearchErrorGridView" runat="server" AutoGenerateColumns="False" AllowPaging="True"
AllowSorting="True" DataKeyNames="FileName" DataSourceID="SearchHelpDeskErrorMessages"
PageSize="10" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
EmptyDataText="No Applicable Data" SelectedRowStyle-BackColor="#99CCFF">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:BoundField DataField="RequestID" HeaderText="RequestID" ReadOnly="True" SortExpression="RequestID" />
<asp:BoundField DataField="Name" HeaderText="Client Name" SortExpression="Name" />
<asp:BoundField DataField="EmpName" HeaderText="Employee Name" SortExpression="EmpName" />
<asp:BoundField DataField="Module" HeaderText="Module" SortExpression="Module" />
<asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" />
<asp:BoundField DataField="ErrorMessage" HeaderText="Error Message" SortExpression="Error Message" />
<asp:BoundField DataField="ShortDescription" HeaderText="ShortDescription" SortExpression="ShortDescription" />
<asp:BoundField DataField="Resolution" HeaderText="Resolution" SortExpression="Resolution" />
<asp:BoundField DataField="DateOfCall" HeaderText="Date Of Call" SortExpression="DateOfCall" />
<asp:BoundField DataField="FileName" HeaderText="File Name" />
<asp:TemplateField HeaderText="File Download">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="Select" Text="Select" />
</Columns>
<EmptyDataRowStyle CssClass="mGrid" />
<PagerStyle CssClass="pgr"></PagerStyle>
</asp:GridView>
I have a DetailsView that looks just about perfect. The only thing is, there is one field that actually gets its information from another table. It is an ID field that is linked to another table in the database and I was hoping that there was some way to get that one field in the DetailsView to pull the information from the other table.
Is there a way to maybe add a + (plus sign) next to the PicklistID and then have it show the results of the other query? (SELECT TEXT FROM PICKLIST WHERE PicklistID = #PicklistID???)
UPDATE 1/11/12: The PICKLISTID in the table PICKLIST is not unique or a primary key so I cannot reference it from the SURVEY table. Is there a way to give these 2 columns a relationship so that I can just change my SELECT statement in my DetailsView to include the PICKLISTID?
UPDATE 1/23/12: I'm still stuck on this. I am not getting how to make this work at all. Help would be appreciated. I am still new....
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="Label2" runat="server" Text="<h3>Manage Questions</h3>"></asp:Label>
<table style="width: 100%">
<tr>
<td align="right">
<asp:Label ID="Label1" runat="server" Text="Select Survey:"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="dsSurvey" DataTextField="SurveyName" DataValueField="SurveyID">
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
<asp:DetailsView ID="dvSurveyQuestions" runat="server" AllowPaging="True"
AutoGenerateRows="False" CellPadding="4" DataKeyNames="QuestionID"
DataSourceID="dsSurveyQuestions" ForeColor="#333333" GridLines="None"
Height="50px" Width="100%">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:BoundField DataField="QuestionID" HeaderText="QuestionID"
InsertVisible="False" ReadOnly="True" SortExpression="QuestionID" />
<asp:TemplateField HeaderText="Question" SortExpression="Question">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"
Text='<%# Bind("Question") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"
Text='<%# Bind("Question") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblQuestion" runat="server" Text='<%# Bind("Question") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Answer Type" SortExpression="AnswerType">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server"
SelectedValue='<%# Bind("AnswerType") %>'>
<asp:ListItem Value="S">Single Choice (radio button)</asp:ListItem>
<asp:ListItem Value="M">Multiple Choices (checkboxes)</asp:ListItem>
<asp:ListItem Value="T">Text (textbox)</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server"
SelectedValue='<%# Bind("AnswerType") %>'>
<asp:ListItem Value="S">Single Choice (radio button)</asp:ListItem>
<asp:ListItem Value="M">Multiple Choices (checkboxes)</asp:ListItem>
<asp:ListItem Value="T">Text (textbox)</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblAnswerType" runat="server"
Text='<%# Bind("AnswerType") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Question Type" SortExpression="QType">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList4" runat="server"
SelectedValue='<%# Bind("QType") %>'>
<asp:ListItem Value="Picklist">Picklist</asp:ListItem>
<asp:ListItem Value="Text">Text</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList5" runat="server"
SelectedValue='<%# Bind("QType") %>'>
<asp:ListItem Value="Picklist">Picklist</asp:ListItem>
<asp:ListItem Value="Text">Text</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblQType" runat="server" Text='<%# Bind("QType") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Answer" SortExpression="TEXT">
<EditItemTemplate>
</EditItemTemplate>
<InsertItemTemplate>
</InsertItemTemplate>
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("PicklistID") %>' />
<asp:SqlDataSource ID="dsPicklist" runat="server"
ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>"
SelectCommand="SELECT DISTINCT p.TEXT, p.PICKLISTID
FROM PICKLIST p JOIN C_Survey_Questions c
ON c.PicklistID = p.PICKLISTID
AND c.SurveyID = #SurveyID
ORDER BY p.TEXT">
<SelectParameters>
<asp:ControlParameter ControlID="HiddenField1" Name="PicklistID"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Label ID="lblTEXT" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="QuestionNum" HeaderText="Question Number"
SortExpression="QuestionNum" />
<asp:BoundField DataField="Subsequence" HeaderText="Subsequence"
SortExpression="Subsequence" />
<asp:CheckBoxField DataField="Active" HeaderText="Active"
SortExpression="Active" />
<asp:BoundField DataField="Script" HeaderText="Script"
SortExpression="Script" />
<asp:CheckBoxField DataField="Question_Locked" HeaderText="Question Locked"
SortExpression="Question_Locked" />
<asp:BoundField DataField="QHelp" HeaderText="Question Help"
SortExpression="QHelp" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowInsertButton="True" />
</Fields>
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" Width="10%" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:DetailsView>
</td>
</tr>
</table>
<asp:SqlDataSource ID="dsSurvey" runat="server"
ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>"
SelectCommand="SELECT [SurveyID], [SurveyName]
FROM [C_Survey]
ORDER BY [SurveyName]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsSurveyQuestions" runat="server"
ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>"
DeleteCommand="DELETE FROM [C_Survey_Questions] WHERE [QuestionID] = #QuestionID"
InsertCommand="INSERT INTO [C_Survey_Questions] ([SurveyID], [Question], [QType],
[PickListID], [QuestionNum], [Subsequence], [Active], [Script],
[Question_Locked], [QHelp], [Createdate],
[Modifydate], [AnswerType])
VALUES (#SurveyID, #Question, #QType, #PickListID, #QuestionNum,
#Subsequence, #Active, #Script, #Question_Locked, #QHelp,
getdate(), getdate(), #AnswerType)"
SelectCommand="SELECT * FROM [C_Survey_Questions]
WHERE ([SurveyID] = #SurveyID)"
UpdateCommand="UPDATE [C_Survey_Questions] SET [SurveyID] = #SurveyID,
[Question] = #Question, [QType] = #QType,
[PickListID] = #PickListID, [QuestionNum] = #QuestionNum,
[Subsequence] = #Subsequence, [Active] = #Active,
[Script] = #Script, [Question_Locked] = #Question_Locked,
[QHelp] = #QHelp, [Modifydate] = getdate(),
[AnswerType] = #AnswerType
WHERE [QuestionID] = #QuestionID">
<DeleteParameters>
<asp:Parameter Name="QuestionID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="SurveyID" Type="Int32" />
<asp:Parameter Name="Question" Type="String" />
<asp:Parameter Name="QType" Type="String" />
<asp:Parameter Name="PickListID" Type="String" />
<asp:Parameter Name="QuestionNum" Type="Int32" />
<asp:Parameter Name="Subsequence" Type="Int32" />
<asp:Parameter Name="Active" Type="Boolean" />
<asp:Parameter Name="Script" Type="String" />
<asp:Parameter Name="Question_Locked" Type="Boolean" />
<asp:Parameter Name="QHelp" Type="String" />
<asp:Parameter Name="Createdate" Type="DateTime" />
<asp:Parameter Name="Modifydate" Type="DateTime" />
<asp:Parameter Name="AnswerType" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="SurveyID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="SurveyID" Type="Int32" />
<asp:Parameter Name="Question" Type="String" />
<asp:Parameter Name="QType" Type="String" />
<asp:Parameter Name="PickListID" Type="String" />
<asp:Parameter Name="QuestionNum" Type="Int32" />
<asp:Parameter Name="Subsequence" Type="Int32" />
<asp:Parameter Name="Active" Type="Boolean" />
<asp:Parameter Name="Script" Type="String" />
<asp:Parameter Name="Question_Locked" Type="Boolean" />
<asp:Parameter Name="QHelp" Type="String" />
<asp:Parameter Name="Modifydate" Type="DateTime" />
<asp:Parameter Name="AnswerType" Type="String" />
<asp:Parameter Name="QuestionID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</asp:Content>
Partial Class Admin_ManageQuestions
Inherits System.Web.UI.Page
Protected Sub dsPicklist_ItemSelecting(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim myControl1 As Control = FindControl("lblTEXT")
If (Not myControl1 Is Nothing) Then
myControl1.DataBind()
Else
'Control not found
End If
End Sub
Protected Sub dvSurveyQuestions_ItemInserting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles
dvSurveyQuestions.ItemInserting
'The DetailsView does not include SurveyID column...we need to set this column
during INSERT operations because each question must belong to some survey.
e.Values("SurveyID") = DropDownList1.SelectedValue
End Sub
Protected Sub dvSurveyQuestions_ItemUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles
dvSurveyQuestions.ItemUpdating
'The DetailsView does not include SurveyID column...we need to set this column
during UPDATE operations because each question must belong to some survey.
e.NewValues("SurveyID") = DropDownList1.SelectedValue
End Sub
Protected Sub dvSurveyQuestions_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dvSurveyQuestions.DataBound
'The event handler checks the row count of the DetailsView control.
If it is zero then the mode of the DetailsView is changed to Insert using
ChangeMode() method.
If dvSurveyQuestions.Rows.Count = 0 Then
dvSurveyQuestions.ChangeMode(DetailsViewMode.Insert)
End If
End Sub
End Class
You can do that, but maybe not in the way you want to.
The simplest way would be to use the DataBinding event on the DetailsView itself. Then use code to run the query.
Another way would be to create your own control as a subclass of BoundField and override the OnDataBindField method
The most performant way would be to join the data at the source in a DataView or another construct. Ideally the joining of data should be done as close to the actual source of the data (e.g. in a SQL query or view) to avoid unnecessary trips across the network to the database. For a single row it wouldn't be a big issue, but that is generally what you should do.