Wednesday, May 23, 2012
If you have a GridView that uses BoundFields but does not display line breaks
or a lot of spaces quite right here is a way to get around that. This example
uses a GridView with one BoundField that uses a DataField of comments. The
comments are stored in a database and entered from another page which displays
the comments properly in a textbox with all the spaces and line breaks you
intended. The trick to get a BoundField to show those items is to take the item
and replace the \r\n with a <br /> and the spaces with  . Also you
need to set the HtmlEncode property of the BoundField to false.
This example shows the data being returned to a DataView which is the
DataSource for the GridView.
The GridView:
<asp:GridView ID="gvComments" runat="server" AutoGenerateColumns="false"
>
<Columns>
<asp:BoundField DataField="comments"
HtmlEncode="false" />
</Columns>
</asp:GridView>
The code (assumes the DataView dv is already filled):
int i = 0;
foreach (DataRow dr in dv.Table.Rows)
{
string comment
= dr.Field<string>(6);
comment = comment.Replace(" ",
" ");
comment = comment.Replace("\r\n", "<br />");
dv.Table.Rows[i][6] = comment;
i++;
}
Tags: ASP.Net, CSharp
Tuesday, May 15, 2012
I came across this the other day. Using SQL 2000 I could
not get a Top statement to work with a variable. I had forgotten that only with
SQL 2005 and higher could you do this trick. I found a good way around it, you
can read
about it here. Anyway this works on SQL 2000:
declare @top int
set @top = 5
BEGIN
set rowcount @top
select Some_ID
from tblStuff
set
rowcount 0
END
The code above would return the first five records from tblStuff. For any of
the newer SQL Server version you can simply use the @top variable in Top
like:
select top (@top) Some_ID
from tblStuff
Tags: SQL
Tuesday, May 08, 2012
I came across this tool the other day and found it to be
very useful. In the past if I needed to search within all the stored procedures
or views in a database I would use some SQL to run a query using sysobjects and
syscomments. That worked well but this is a bit slicker. The tool is redgate's
SQL Search and it is available (for now at least) as a free stand alone tool.
You can download it here.
It supports SQL 2005 and later and I found it to
integrate with the SSMS IDE well. It will add a red binocular SQL Search icon in
your tool bar. Clicking that will open a nice tab with the searching fields. You
can easily search for some value within a view then switch to a stored
procedure. I found it to be really useful when setting up a test database and I
needed to search for names of objects that might have changed for the new test
environment. You search for a value then double click the item on the list which
opens the item within Object Explorer.
Tags: SQL
Friday, April 13, 2012
Here is a really easy fix for some ORA-00900 errors.
Well at least the one I saw the other day. This was something that I did not
come across when searching either. I found lots of other ideas on what the
problem might be but not the fix. Since I am fairly new to PL/SQL (TSQL only for
a long time) this one stumped me for a while. Until I asked someone and they saw
the error in about two seconds.
When using the Command Window to add a view I was receiving an ORA-00900
error. So I checked that everything the view was referencing was there and that
the permissions looked OK. The code for the view was fairly simple and it ran
just fine in a regular SQL Window. It ended up that the Command Window did not
like the space I had between the list of items in the select before the
from.
Bad:
col1,
col2,
<--- does not like
the empty line
from tblSomething
Good:
col1,
col2,
from tblSomething
I will just chalk that up to my familiarity with PL/SQL.
Tags: PLSQL
Wednesday, March 28, 2012
This post is pretty specific to an issue I had but still has some ideas that could be applied in other scenarios. The problem I had was updating a few buttons so their Text values could be set in the code behind which had a method...
Tuesday, March 06, 2012
Recently I ran into an issue where the paging text in a
RadGrid control needed to be customizable. In my case I was specifically looking
at the "Page size" and "xx items in yy pages" text. Getting around it for the
"Page size" was a snap. If your RadGrid is called rgTest one line would do
it:
rgTest.PagerStyle.PageSizeLabelText = "your text
here";
The "xx items in yy pages" was a bit trickier, but still
handled in one line of code.
rgTest.PagerStyle.PagerTextFormat = "{4} {5} things in
{1} my pages";
The tricky part was determining which items to use,
there are five in total ({1} through {5}). The {4} needs to be first otherwise
the rest will not show up. A good guide (but not for my particular situation) is
found here.
Tags: ASP.Net, CSharp
Friday, January 20, 2012
Here is an easy way to retrieve the user ID from who ever
is logged on to a PC using ASP.Net. This is handy for ASP.Net applications where
you want to put a "Welcome joe user" label at the top, auto populate a form with
the user's ID or add the user ID to some data you are storing back into a
database if you keep track of who edited a record last.
The code below assumes you have an aspx page with two labels on it, lblFName
and lblLName. It grabs the user ID with HttpContext and then splits the ID into
two values for the two names. It also assumes the user ID would be along the
lines of yourdomain\joe.user. You can do more string manipulation on the two
values if you want (i.e. like capitalizing the first letter) once you have the
values in the string array.
protected void Page_Load(object sender, EventArgs e)
{
if
(!Page.IsPostBack)
{
lblFName.Text = "";
lblLName.Text =
"";
try
{
if
(HttpContext.Current.User.Identity.Name.IndexOf(".") >
0)
{
string uname;
uname =
HttpContext.Current.User.Identity.Name.Split('\\')[1].ToString();
string[]
splitname = new string[1];
splitname =
uname.Split('.');
lblFName.Text = splitname[0];
lblLName.Text =
splitname[1];
}
}
catch
{
//found no name, put your
error code here
}
}
}
Tags: ASP.Net, CSharp
Wednesday, January 18, 2012
This post is an example of how to write a WCF Service using a class. The example uses Visual Studio 2010, written in C#, SQL Server 2008 and hosted in IIS. So lets have at it.
Friday, December 23, 2011
Here is one way to access a control in a GridView right after a user clicks a button to change from view to edit mode. This example shows a TextBox that gets populated with today's date. The GridView is setup like this:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="TestID" DataSourceID="SqlDataSource1" onrowdatabound="GridView2_RowDataBound" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblTest1" runat="server" Text='<%# BIND("TestID") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblTest4" runat="server" Text='<%# BIND("TestID") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblTest2" runat="server" Text='<%# BIND("Value1") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTest1" runat="server" Text="test" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblTest3" runat="server" Text='<%# BIND("Value2") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTest2" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The txtTest2 TextBox is the one that we want to put today's date in. To enter that information during the switch into edit you can add the following:
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState.HasFlag(DataControlRowState.Edit))
{
TextBox txtBox2 = (TextBox)e.Row.FindControl("txtTest2");
txtBox2.Text = DateTime.Now.ToShortDateString();
}
}
The little trick lies in the DataControlRowState.Edit line. When that is true you can use the normal FindControl methods to grab the control you need. Probably not a common thing to do with a GridView but you never know.
Technorati Tags:
ASP.Net,
CSharp,
GridView
Thursday, November 03, 2011
In SQL Server 2008 you can use table valued parameters which can be pretty useful. In the example I use a very simple one to overcome SQL Server’s lack of having a parameter as an array. The example goes through creating the new type, using it in a stored procedure and calling it from an application (VB.Net in this example). You start off with creating the new type. Under Programmability/Types/User-Defined Table Types create a new table. Here I created a table with a single column to hold an int