How to use current item id in SPGridview - sharepoint-2010

Can you please tell me how do i use current item id in the NavigateURL in SPGridView.
Below is the code sample. If I use Eval it is giving run time error.
Please help me in this case..
<SharePoint:SPGridView runat="server" ID="gdvSearchResults" width="50%" AllowSorting="True"
AutoGenerateSelectButton="false" AutoGenerateColumns="false">
<Columns>
<SharePoint:SPBoundField runat="server" DataField="Attachments" HeaderText="Attachments" SortExpression="Attachments" />
<SharePoint:SPBoundField runat="server" DataField="Practice" HeaderText="Practice" />
<asp:HyperLinkField DataTextField="ID" HeaderText="Require_x0020_Details_x0020__x00" NavigateUrl="http://server/sites/TestingCollection/TestLists/Send%20Mail/EditForm.aspx?ID="+ID/>
</Columns>
</SharePoint:SPGridView>
Thanks in advance!

Resolved it using the below changes in my grid.
<SharePoint:SPGridView runat="server" ID="gdvSearchResults" width="50%" AllowSorting="True"
AutoGenerateSelectButton="false" AutoGenerateColumns="false">
<Columns>
<SharePoint:SPBoundField runat="server" DataField="Attachments" HeaderText="Attachments" SortExpression="Attachments" />
<SharePoint:SPBoundField runat="server" DataField="Practice" HeaderText="Practice" />
<asp:HyperLinkField DataNavigateUrlFields="ID" DataNavigateUrlFormatString="~/EditForm.aspx?ID={0}" DataTextField="ID" HeaderText="ID" />
</Columns>
</SharePoint:SPGridView>

Related

Multi-parameter SqlDatasource search not returning any records

I have an odd problem. I have a gridview attached to a sqldatasource that I send three possible parameters to. The search works fine if I search the item number but if I search for the first name or last name without the item number it does not return anything. The SQL works fine when I test it in SQL management studio but does not when I test it in visual studio in the search form.
Any assistance in solving this will be appreciated. Thanks for your time.
<table style="width: 100%;">
<tr>
<td>Caller Search</td>
<td>
<asp:Label ID="ITEMsearchlable" runat="server" Text="ITEM#"></asp:Label><br />
<asp:TextBox ID="ITEM_NUMsearch" runat="server"></asp:TextBox></td>
<td>
<asp:Label ID="ofname_label" runat="server" Text="First name"></asp:Label><br />
<asp:TextBox ID="search_fname" runat="server" CssClass="uppercase"></asp:TextBox></td>
<td><asp:Label ID="olname_label" runat="server" Text="Last name"></asp:Label><br />
<asp:TextBox ID="search_lname" runat="server" CssClass="uppercase"></asp:TextBox></td>
<td>
<asp:Button ID="search_btn" runat="server" Text="Offender Caller" /></td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" Width="771px" EmptyDataText="No record found">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="ITEM_NUM" HeaderText="ITEM_NUM" SortExpression="ITEM_NUM" />
<asp:BoundField DataField="NAME_FIRST" HeaderText="NAME_FIRST" SortExpression="NAME_FIRST" />
<asp:BoundField DataField="NAME_LAST" HeaderText="NAME_LAST" SortExpression="NAME_LAST" />
<asp:BoundField DataField="CALLER_ID" HeaderText="CALLER_ID" SortExpression="CALLER_ID" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testdbConnectionString %>"
SelectCommand="SELECT DISTINCT
ORDERS.ITEM_NUM,
ORDERS.NAME_FIRST,
ORDERS.NAME_LAST,
VICTIM_CALL_LOG.CALLER_ID
FROM ORDERS LEFT OUTER JOIN
VICTIM_CALL_LOG ON ORDERS.ITEM_NUM = VICTIM_CALL_LOG.ITEM_NUM WHERE ((ORDERS.ITEM_NUM = #ITEM_NUM)
OR (ORDERS.NAME_FIRST LIKE '%' + #NAME_FIRST + '%') OR (ORDERS.NAME_LAST LIKE '%' + #NAME_LAST + '%'))">
<SelectParameters>
<asp:ControlParameter ControlID="ITEM_NUMsearch" Name="ITEM_NUM" PropertyName="Text" Type="Int32" />
<asp:ControlParameter ControlID="search_fname" DefaultValue="NULL" Name="NAME_FIRST" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="search_lname" DefaultValue="NULL" Name="NAME_LAST" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
You need to give the numeric parameter a default value too:
<asp:ControlParameter ControlID="ITEM_NUMsearch" DefaultValue="-1" Name="ITEM_NUM" PropertyName="Text" Type="Int32" />
I used "-1" because I guessed it would never be an actual value in the database, so that needs to be checked.
Note that having a DefaultValue of "NULL" in the string parameters is actually the string "NULL", not the database version of NULL. To do that properly, I suspect that you would have to use code-behind instead of <asp> controls. In the meantime, a default value of "12345" could be a better choice, just in case someone has a name with "null" in it - Null is an actual last name for some people: How does a surname of Null cause problems in many databases?

get Sum of values in column from sqlDataSource

I have a SqlDataSource dsDetails that selects from the DB table.
<asp:SqlDataSource ID="dsDetails" runat="server" SelectCommand="spGetDetails"
OnSelected="dsDetails_Selected" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="mode" QueryStringField="mode" Type="String" DefaultValue="NULL" />
</SelectParameters>
</asp:SqlDataSource>
The result is bound to gridview in the aspx page
<asp:GridView CssClass="content" ID="gridDetails" runat="server" DataSourceID="dsDetails" AllowSorting="True" AllowPaging="True" PagerSettings-Position ="TopAndBottom" DataKeyNames="ID" AutoGenerateColumns="False" >
<PagerSettings Position="TopAndBottom" Mode="NumericFirstLast" />
<EmptyDataTemplate>
<b>There are no records to display.</b>
</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="empNum" HeaderText="Employee Number" SortExpression="empnum" ApplyFormatInEditMode="True" HtmlEncode="False" ReadOnly="True" />
<asp:BoundField DataField="empName" HeaderText="Emplopyee Name" SortExpression="empname" ApplyFormatInEditMode="True" HtmlEncode="False" ReadOnly="True" />
<asp:BoundField DataField="empDoj" HeaderText="Emplopyee Date of joining" SortExpression="empdoj" ApplyFormatInEditMode="True" HtmlEncode="False" ReadOnly="True" />
<asp:TemplateField HeaderText="Salary" SortExpression="sal">
<EditItemTemplate>
<asp:TextBox ID="txtSal" runat="server" Text='<%# Eval("sal", "{0:F}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("sal", "{0:C2}") %>'></asp:Label>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:Button ID="btnSave" runat="server" CausesValidation="True" CommandName="cmdSave"
Text="Save" />
<asp:Button ID="btnCancel" runat="server" CausesValidation="False" CommandName="cmdCancel"
Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" CausesValidation="False" CommandName="cmdApprove"
Text="Approve" />
<asp:Button ID="btnEdit" runat="server" CausesValidation="False" CommandName="cmdEdit"
Text="Edit" />
<asp:Button ID="btnDelete" runat="server" CausesValidation="false" CommandName="cmdDelete"
Text="Delete" />
<ajaxToolkit:ConfirmButtonExtender ID="cbeDelete" runat="server"
TargetControlID="btnDelete"
ConfirmText="Are you sure?" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now what i have to achieve is Find the Sum of the Salary Column of the grid view. How to achieve this using the Data Source in the page load method? I have to get the sum of Salary for the entire result set (avg records returned 60-80)
Iterating through the rows and computing the sum in gridDetails_RowDataBound wont be effective since the rows.count will be max of 10 since the page size is 10. When the result set has more than 10 records say 55, It will compute the sum for the current gridview page alone (first 10 records). I have to get the sum for all 55 records and display it in a label in the pageload method.
Please provide suggestion..
The SQLDataSource provides a Selected event that fires after data retrieval has finished. See SqlDataSource.Selected Event. You should be able to do a sum on the rows in the grid in this event.
This worked for me
In PageLoad method add
dvSql = DirectCast(dsDetails.Select(DataSourceSelectArguments.Empty), DataView)
For Each drvSql As DataRowView In dvSql
amtSum += CDbl(drvSql("sal"))
Next
lblTotalAmt.Text = CStr(recoveryAmtSum)
In dsDetails_Selected method add this line
e.Command.Parameters("#approvaltype").Value = mode

GridView SelectParameter from code behind

I am using VB.NET webforms.
I have a GridView which brings data from Database when the page loads. The SQLDataSource with Select SQL is as follows:
<asp:SqlDataSource
ID="ScopeDataSource"
runat="server"
ConnectionString="<%$ ConnectionStrings:SQLServerConnectionString %>"
SelectCommand="SELECT [SCO_LINENUM], [SCO_LINETEXT], [SCO_SCOPEID], [SCO_TAB1LINK], [SCO_TAB2LINK], [SCO_TAB3LINK] FROM [SCOPE] WHERE [SCO_SCOPEID] = 'AAAREN02'"></asp:SqlDataSource>
My GridView is as follows:
<asp:GridView
ID="ScopeGrid"
runat="server"
DataSourceID="ScopeDataSource"
AutoGenerateColumns="False"
GridLines="None"
DataKeyNames="SCO_SCOPEID, SCO_LINENUM">
<Columns>
<asp:BoundField DataField="SCO_LINENUM" HeaderText="Sr#" SortExpression="SCO_LINENUM">
<HeaderStyle Height="40px" />
<ItemStyle Width="30px" />
</asp:BoundField>
<asp:BoundField DataField="SCO_LINETEXT" HeaderText="Parameter.." SortExpression="SCO_LINETEXT">
<HeaderStyle Height="40px" />
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Operator Values...........">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="DropDownMargin">
<asp:ListItem>BETWEEN</asp:ListItem>
<asp:ListItem>EQUALTO</asp:ListItem>
<asp:ListItem>GREATERTHAN</asp:ListItem>
<asp:ListItem>LESSTHAN</asp:ListItem>
</asp:DropDownList>
<asp:TextBox runat="server" ID="RangeStart" Text="None" Width="50px" CssClass="TextBoxMargin"></asp:TextBox>
<asp:TextBox runat="server" ID="RangeEnd" Text="None" Width="50px"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Height="40px" CssClass="WhiteSpacePreserve" />
</asp:TemplateField>
<asp:BoundField DataField="SCO_SCOPEID" HeaderText="ScopeID" SortExpression="SCO_SCOPEID">
<HeaderStyle CssClass="hidegrid" />
<ItemStyle CssClass="hidegrid" />
</asp:BoundField>
<asp:BoundField DataField="SCO_TAB1LINK" HeaderText="Tab1" SortExpression="SCO_TAB1LINK" />
<asp:BoundField DataField="SCO_TAB2LINK" HeaderText="Tab2" SortExpression="SCO_TAB2LINK" />
<asp:BoundField DataField="SCO_TAB3LINK" HeaderText="Tab3" SortExpression="SCO_TAB3LINK" />
</Columns>
</asp:GridView>
As seen in above SQLDataSource, the WHERE clause is hard-coded. I want to make this dynamic by passing a variable to the WHERE clause from code-behind. Also My Page_Load event is empty. So far, since the SQLDataSource WHERE clause is hard-coded in my aspx I can see the data in gridview but I don't know how to send a variable to it from code-behind?
Something like this would work.
Change your select command to
SELECT [SCO_LINENUM], [SCO_LINETEXT], [SCO_SCOPEID], [SCO_TAB1LINK], [SCO_TAB2LINK], [SCO_TAB3LINK] FROM [SCOPE] WHERE [SCO_SCOPEID] = #SCO_SCOPEID
Then do like
ScopeDataSource.SelectParameters("SCO_SCOPEID").DefaultValue = Your value

very strange issue; different results from same table

I am binding a gridview with some column from a database table, but it shows different results in Pane and in Gridview. see the attachted images.
see the marked records why they are different, although they are comming from the same table.
But yes for binding gridview I am using this query
SELECT mtblbuilding_budget_data.building_id,
mtblbuilding_budget_data.building_name,
mtblbuilding_budget_data.sanctioned_dpr,
mtblbuilding_budget_data.sanctioned_contract,
mtblblock.block_name
FROM mtblbuilding_budget_data
LEFT JOIN mtblblock
ON mtblblock.block_id = mtblbuilding_budget_data.block_id
WHERE mtblbuilding_budget_data.sfti_id = #SFTI_Id
AND mtblbuilding_budget_data.spmu_id = #SPMU_Id
ORDER BY mtblbuilding_budget_data.block_id
aspx code
<asp:TemplateField HeaderText="Sanction (DPR)">
<ItemTemplate>
<%#GetQTY1(float.Parse(Eval("Sanctioned_DPR").ToString())).ToString("N2")%>
</ItemTemplate>
<FooterTemplate>
<%#GetTotalQTY1().ToString("N2")%>
</FooterTemplate>
<FooterStyle HorizontalAlign="Right" />
<HeaderStyle HorizontalAlign="Right" />
<ItemStyle HorizontalAlign="Right" VerticalAlign="Top" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Sanction (Contract)">
<ItemTemplate>
<%#GetQTY2(float.Parse(Eval("Sanctioned_Contract").ToString())).ToString("N2")%>
</ItemTemplate>
<FooterTemplate>
<%#GetTotalQTY2().ToString("N2")%>
</FooterTemplate>
<FooterStyle HorizontalAlign="Right" />
<HeaderStyle HorizontalAlign="Right" />
<ItemStyle HorizontalAlign="Right" VerticalAlign="Top" />
</asp:TemplateField>
Your grigview is rounding the values, check the next column on the same lines, it also as different results.
check the properties of your gridview e disable the round option.
check the properties of your gridview e disable the round option. That's the solution..

Where parameters in EntitydataSource from dropdown control

Using the following markup, my details view is not populating when the dropdown is selected.
The id for the where parameter is to come from the dropdwon selected value.It appears that the control parameter is not functioning properly.
<asp:UpdatePanel ID="updtEditContact" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlContacttoEdit" runat="server" CssClass="dropdowns"
AutoPostBack="True" ClientIDMode="Static"/>
<asp:DetailsView ID="dvEditContacts" runat="server" Height="50px" Width="125px"
AutoGenerateEditButton="True" CssClass="mGrid"/>
<asp:EntityDataSource ID="edsSelectedContact" runat="server" ConnectionString="name=webEntities"
DefaultContainerName="webEntities" EnableFlattening="False"
EntitySetName="contacts">
<WhereParameters>
<asp:ControlParameter ControlID="ddlContactToEdit" Name="Id"
PropertyName="SelectedValue" Type="Int32" />
</WhereParameters>
</asp:EntityDataSource>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rblAddEditContact" EventName="SelectedIndexChanged"/>
<asp:AsyncPostBackTrigger ControlID="ddlContacttoEdit" EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
Upon further research, which was tedious as there isn't a lot of the actual EntityDataSource control since all us probably do most of our EF work in the code behind, there were three issues
I did not have a where attribute in the EDS markup
The "Type" in the WhereParameter needs to be DBType
The Name Attribute of the WhereParameter much match the variable in the Where statement in the EDS markup
Here is the code that works:
<asp:UpdatePanel ID="updtEditContact" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlContacttoEdit" runat="server" CssClass="dropdowns"
AutoPostBack="True" ClientIDMode="Static"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rblAddEditContact" EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
<br />
<asp:UpdatePanel ID="updatEditContactDetail" runat="server">
<ContentTemplate>
<asp:DetailsView ID="dvEditContacts" runat="server" Height="50px" Width="300px"
DataSourceID="edsSelectedContact" DataKeyNames="Id" Visible="False"
CssClass="mDetail" FieldHeaderStyle-CssClass="fieldheader"
ItemStyle-CssClass="itemvalues" CommandRowStyle-CssClass="cmdRow"
EditRowStyle-CssClass="editvalues" ClientIDMode="Static"
InsertRowStyle-CssClass="insertvalues" RowStyle-CssClass="rowvalues"/>
<asp:EntityDataSource ID="edsSelectedContact" runat="server" ConnectionString="name=webEntities"
DefaultContainerName="webEntities" EnableFlattening="False"
EntitySetName="contacts" Where="it.Id = #ID">
<WhereParameters>
<asp:ControlParameter ControlID="ddlContactToEdit" Name="ID" PropertyName="SelectedValue" DbType="Int32"/>
</WhereParameters>
</asp:EntityDataSource>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlContacttoEdit" EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>