Wednesday, August 20, 2008

DataTable C# Primary Key column .Contains

I needed to do something like use DataTable in C#, set Primary Key column, and later on maybe a "contains" ... to see if my set of ID is inside the DataTable
Anyways: this is how I did it:





DataTable myFilteredTerms = objSomethn.getresultsinDT ...
DataColumn[] myPrimaryKeyColumn = new DataColumn[1];
myPrimaryKeyColumn.SetValue(myFilteredTerms.Columns[0], 0);
myFilteredTerms.PrimaryKey = myPrimaryKeyColumn;

...

if (myFilteredTerms.Rows.Contains(intTheIdYouAreLookingFor))
{
...
}





(first column is the one with the ID-s ...)

as always, if you got pro or contra ... tell me bout it

Friday, August 15, 2008

How to count enum elements in C#?

If you need to count the enum elements ...

this is how you do it:

int nYourNumber = Enum.GetValues(typeof(YourEnumName)).Length;

as always: if you have something neater ;) comment ... I'll update the post if needed ;)

Thursday, July 31, 2008

Parse html table cells

I wanted to parse a html table and color the TD - s (background).
All of them.
I have a column that has links in it. My basic Idea was to change the background color of the TD when you click the link inside that. That's easy, but without some global (js?) variable, you don't know what was previously clicked, and you might want to turn that "off" :)
Anyways: this is how I did it:





function ParseTableAndColorTheBackground(objTbl, WhatColorShouldItBe)
{
var intNrOfTrs = objTbl.tBodies[0].rows.length;
for (var i=0; i < intNrOfTrs; i++)
{
objTbl.tBodies[0].rows[i].cells[1].style.backgroundColor = WhatColorShouldItBe;
}
}




you have a better way to do this?

comment me pls
;)

Friday, July 11, 2008

Ajax Databind – javascript error “unknown runtime error”

I had this error a few minutes ago. It was the weirdest thing. On a Save button (linkbutton) I had a gridview repopulated using Ajax. The Save linkbutton was also added as a trigger for the Ajax’s UpdatePanel. The save went well, saved in the database, but when it came to refresh the gridview, this javascript error popped up.

The fix was move the stuff around. Scriptmanager, UpdatePanel, ObjectDataSource … all of them inside the form … Anyways … if you have this error, you don’t necessarily have a big problem, you just need to figure out what did you put in the wrong place.





< form id="form1" runat="server">

< ajax:ScriptManager runat="server" ID="ScriptManager1" EnablePartialRendering="true">
< Services>
< ajax:ServiceReference Path="../Include/WebService.asmx" InlineScript="true" />
< /Services>
< /ajax:ScriptManager>
< ajax:UpdatePanel ID="up2" runat="server">
< ContentTemplate>
< asp:GridView>
...
< /asp:GridView>
< /ContentTemplate>
< Triggers>
< ajax:AsyncPostBackTrigger ControlID="lbnSave" EventName="Click" />
< /Triggers>
< /ajax:UpdatePanel>

< asp:ObjectDataSource ID="ods" ...
< SelectParameters>
< asp:QueryStringParameter ...
< /SelectParameters>
< /asp:ObjectDataSource>

< /form>






see ya

Thursday, July 3, 2008

C# convert mm/dd/yyyy string to "5 minute(s) ago"

this is how utube does it:



For a forum-kinda thing I wanted to (C#) convert mm/dd/yyyy string to "5 minute(s) ago" - like Youtube does.

Here's how u I did it:
(please feel free to comment, get me a better way to do this :D)





/// < summary>
/// transform into "9 hours ago" or "8 days ago" etc ...
/// < /summary>
/// < param name="makeThisNiceDateFormat">some date string ... "mm/dd/yyyy"< /param>
/// < returns >example: "5 minute(s) ago"< /returns>
public static string ConvertDateToHoursAgo(string makeThisNiceDateFormat)
{
if (!IsDate(makeThisNiceDateFormat))
{
return makeThisNiceDateFormat;
}
else
{
DateTime dtOriginal = DateTime.Parse(makeThisNiceDateFormat);
//check if we need to display minutes
if (Math.Abs(DateDiff(DateInterval.Minute, dtOriginal, DateTime.Now)) < 60)
{
if (DateDiff(DateInterval.Minute, dtOriginal, DateTime.Now) == 0)
{
return "just now";
}
return (Math.Abs(DateDiff(DateInterval.Minute, dtOriginal, DateTime.Now)) + " minute(s) ago");
}
//check if we need to display hours
if (Math.Abs(DateDiff(DateInterval.Hour, dtOriginal, DateTime.Now)) < 24)
{
return Math.Abs(DateDiff(DateInterval.Hour, dtOriginal, DateTime.Now)) + " hour(s) ago";
}
//check if we need to display days
if (Math.Abs(DateDiff(DateInterval.Day, dtOriginal, DateTime.Now)) < 31)
{
return Math.Abs(DateDiff(DateInterval.Day, dtOriginal, DateTime.Now)) + " day(s) ago";
}
//check if we need to display months
if (Math.Abs(DateDiff(DateInterval.Month, dtOriginal, DateTime.Now)) < 12)
{
return Math.Abs(DateDiff(DateInterval.Month, dtOriginal, DateTime.Now)) + " month(s) ago";
}
return Math.Abs(DateDiff(DateInterval.Year, dtOriginal, DateTime.Now)) + " year(s) ago";
}
}





thoughts?
Note: I got an IsDate function in there which returns true/false ... in case someone tries to send me something that is not a date ...

Tuesday, June 24, 2008

nHibernate - a thing i like

I have a table: Act, and an other table Service Name
between them, there's an other table, that has actId and ServicenameId too ...

I needed somehow for the act object to see all the servicenames. somehow ... in a list maybe? or an ilist?

Here's how u do it:




< bag name="ServiceNameList" lazy="false" table="BOMMSS_ServiceNameAct">
< key column="TARFSS_ActId"/>
< many-to-many class="Atlas.Tariff.DataAccess.ServiceName,Atlas.Tariff.DataAccess" column="ServiceNameId"/>
< /bag>




in the mapping file (xml), and add something like this:




public IList ServiceNameList
{
get { return m_servicenamelist; }
set { m_isChanged |= (m_servicenamelist != value); m_servicenamelist = value; }
}




in the class def.

Tuesday, June 10, 2008

SQL and Underscore

Recently one of our QA told me that searching for a record with _ in it, won't work.
Actually it works, but the thing is that the '_' ( underscore ) is a reserved SQL character, and you have to know how to use it.

Originally the '_' ( underscore ) was designed to:
Let's say you want to find the word 'synopsys' in your table. But you don't if it was 'synopsys' or 'sinopsys' :)
In this case you should do this:




select Title from YourTable where title like 's_nopsys'




And this will find them both. (if any)

Back to Our case.
What our code looked like was something like this:




select Title from YourTable where title like '%_%'




Of course it returned everything from the table ... so QA was kinda right ... filter was not working.
But if QA would've known to search for '[_]' ... then it would've worked ...


It is doable, of course, not to have to add the '[' and the ']' in the search box ... but ... does it worth it?
If you can explain your 'client', how to use the '_' ( underscore ), and how to use the % sign ... in the search query ...
well maybe you don't have to change any of your code, because your 'client' might be happy to use these extra features.

If you can't talk your 'client' into this, you can try something like SQL's escape:




select Title from YourTable where title like '%\_%' escape '\'




or, add some extra character, because next time they'll search for '\' :)
so maybe do something like this:




select Title from YourTable where title like '%' + char(13) + '_%' escape char(13)




If you have some thoughts on this ... tell me about it ...