Wednesday, December 9, 2009
Formatting Currency in a GridView
< %# Server.HtmlEncode( Eval("InvoiceAmount", "{0:$#,#.#0}").ToString() ) % >"
I got this from a great article http://blog.stevex.net/string-formatting-in-csharp/
Using the decimal value 4219.60 as my number from the database for a product.
{0}
4219.60
{0:c}
$4,219.60
{0:c2}
$4,219.60
{0:c4}
$4,219.6000
{0:$#.#}
$4219.6
{0:$#,#.#}
$4,219.6
{0:#,#.##}
$4,219.6
{0:$#,#.#0}
$4,219.60
Tuesday, December 8, 2009
The Value of Datakeys in GridView RowDataBound
protected void gvSummary_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = new DataTable();
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find the invoice number - which is the Datakeys
int inv = Convert.ToInt32(((GridView)sender).DataKeys[e.Row.RowIndex].Value);
//find the charges grid - populated based on the invoice number
GridView gvCh = (GridView)e.Row.FindControl("gvCharges");
dt = maData.TrapRetrievalInvoiceChargesGet(inv);
//populate a nested grid
gvCh.DataSource = dt;
gvCh.DataBind();
}
}
ASP Code
asp:GridView ID="gvSummary" runat="server" DataKeyNames="Invoice"
...
Thursday, October 29, 2009
T-SQL scalar function looping through a cursor
http://www.sql-server-performance.com/articles/per/operations_no_cursors_p2.aspx
To call this function in a select:
select csl.dbo.testfn()
Function code:
CREATE Function dbo.testfn()
returns varchar(5000)
AS
BEGIN
/*
** Cursor method to cycle through the table
to retrieve data for each row
*/
-- declare all variables!
DECLARE @iLic int,
@nLicNum nvarchar(10),
@nLictype nvarchar(15) ,
@return nvarchar(2000)
-- declare the cursor
DECLARE Lic CURSOR FOR
SELECT TOP 10 pk_license,
LicenseNumber,
FK_LicenseType
FROM CSL.dbo.License
OPEN Lic
FETCH Lic INTO @iLic,
@nLicNum,
@nLictype
-- start the main processing loop.
set @return=''
WHILE @@Fetch_Status = 0
BEGIN
-- This is where you perform your detailed row-by-row processing.
--concat fields
set @return += '*' + convert(nvarchar, @iLic)
set @return += '*' + @nLicNum
set @return += '*' + @nLictype
-- Get the next row.
FETCH Lic INTO @iLic,
@nLicNum,
@nLictype
END
CLOSE Lic
DEALLOCATE Lic
RETURN @return
END
GO
Friday, October 16, 2009
Search SQL Server Trigger Code
SELECT OBJECT_NAME(id)
FROM syscomments
WHERE OBJECTPROPERTY(id, 'IsTrigger') = 1
and lower(text) like '%wordtosearchfor%'
Finding Table Columns in SQL Server
select o.name
from sys.all_objects o, sys.all_columns c
where o.object_id = c.object_id
and o.type = 'U'
and lower(c.name) = 'thecolumnnameyouwant'
type = 'U' is for user tables
Searching SQL Server TSQL Procedure code
SELECT OBJECT_NAME(id)
FROM syscomments
WHERE lower(text) LIKE '%the code i am trying to find%'
AND OBJECTPROPERTY(id, 'IsProcedure') = 1
GROUP BY OBJECT_NAME(id)
In Oracle we would have queried user_source for this information.
Tuesday, September 8, 2009
.Net - write output text file
.Net method to format phone numbers
Friday, September 4, 2009
T-SQL procedure to increment an alpha character
Populating nested gridviews
Sometimes it’s useful to nest gridviews within another gridview, especially for traditional master/detail output. Unfortunately the .NET code behind pages do not play well with nested objects (they are not always accessible to refer to in the code), so we have to write code to get around this.
We need, for each outer grid row, to find the inner grid and populate it, based on the data in the outer grid. Of course there are two components, the aspx source (using grid templates), and the code behind. Some important things to notice: gvReport is the outer grid, gvReport_RowDataBound is the method that populates the inner grid, and gvLicenses is the nested grid.
ASPx source:
<asp:GridView ID="gvReport"
HeaderStyle-CssClass="Grid_HeaderRowStyle"
runat="server" AutoGenerateColumns="False"
onrowdatabound="gvReport_RowDataBound"
RowStyle-CssClass="Grid_RowStyle"
AlternatingRowStyle-CssClass="Grid_AlternateRowStyle" AllowSorting="True"
Width="100%" RowStyle-BackColor="#CCCCCC">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<b>Authority: <%# Eval("citation") %><%# Eval("subchapter") %> - <%# Eval("title") %></b> <br />
<b>Exemption: <%# Eval("shortdescription") %></b>: <%# Eval("longdescription") %>
<%—nested grid -->
<asp:GridView ID="gvLicenses"
runat="server" DataKeyNames="licensepermitid" AutoGenerateColumns="False"
AllowSorting="True"
GridLines="None" RowStyle-CssClass="Grid_RowStyle" Width="100%" RowStyle-BackColor="White">
<Columns>
<asp:TemplateField ItemStyle-Width="3%" HeaderStyle-BackColor="White" HeaderText="" ShowHeader="False">
<ItemTemplate> </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Licenses For Authority/Exemption" HeaderStyle-HorizontalAlign="Left" HeaderStyle-BackColor="White">
<ItemTemplate>
<%# Eval("name") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
<%-- hide these but include them so they are available in the code-->
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:TextBox id="subchapter" Visible="false" Text='<%# Bind("subchapter")%>' runat="server" />
<asp:TextBox id="authorityID" Visible="false" Text='<%# Bind("authorityID")%>' runat="server" />
<asp:TextBox id="exemptionid" Visible="false" Text='<%# Bind("exemptionid")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
protected void gvReport_RowDataBound(object sender, GridViewRowEventArgs e)
{
//e.Row is the current row
if (e.Row.RowType == DataControlRowType.DataRow)
{ //find the nested objects
TextBox aTb = (TextBox)e.Row.FindControl("authorityid");
TextBox eTb = (TextBox)e.Row.FindControl("exemptionid");
TextBox scTb = (TextBox)e.Row.FindControl("subchapter");
//nested grid
GridView gv = (GridView)e.Row.FindControl("gvLicenses");
//populate with data from the outer/master grid
SqlDataReader dr = ReportUtility.GetExemptionAuthoritySubchapterLicenses(Convert.ToInt32(eTb.Text), Convert.ToInt32(aTb.Text), scTb.Text, lpID, "");
gv.DataSource = dr;
gv.DataBind();
dr.Close();
}
}