Thursday, July 5, 2012

Generate half hour timeslots lookup table in SQL Server

One of the projects required a lookup table with half hour timeslots. For example
Id       Start time       End time
1        12:00 AM         12:30 AM
2        12:30 AM          1:00 AM
3        1:00 AM           1:30 AM

and so on...
Below SQL lets you generate the half hour timeslots for 90 days

declare @dt_date datetime
set @dt_date = '2012-04-30 00:00:00.000' -- Set the start date 

declare @dt_end_date  datetime

set @dt_end_date = (select dateadd(d,90,@dt_date))

declare @counter int =1

while (@dt_date < @dt_end_date )
begin
insert into dbo.your_table_name
select @counter,@dt_date,dateadd(mi,30,@dt_date)
set @dt_date = (select dateadd(mi,30,@dt_date))
set @counter = @counter + 1
end 

IE9-IE8 Images rendering blurred,gradient issues

The images on IE9-IE8 browser may not render with smooth edges.The same images will have no issues on FireFox or Chrome.See below







I disabled one of the css entry in the css file that I recieved from the UX designer to fix the issue
filter: progid:DXImageTransform.Microsoft.gradient

After I commented out the above css entry the images rendered correctly on IE9-IE8





Online Image Editor

I wanted to create some images for a webpage and I did not want to wait for a UX designer to create one for me so I found this really cool free image editor http://pixlr.com/editor/

Label for dynamically generated html input CheckBox Inside a ListView

Recently while testing one of the web pages on a Android phone the dynamically generated checkboxes inside a ListView was not working.This was happening because the ID of the checkbox did not match the for= attribute in the label tag.

The html when generated dynamically  will look something like this. Note the ID of the control ListView i.e “MobileTimeSlotListView” appended to the checkbox ID i.e. TimeSlotCheckBox below.
<span class="custom-time">
     <input name="MobileTimeSlotListView$ctrl0$TimeSlotCheckBox" type="checkbox" id="MobileTimeSlotListView_ctrl0_TimeSlotCheckBox" value="5/23/2012 10:30:00 PM" />
     <label for="TimeSlotCheckBox">May 23 - 10:30 PM - 11:00 PM</label>
<span class="box"><span class="tick"></span></span></span>

So to get around this I used a server variable for the label and tagged the checkbox ID as the associatedcontrolid

<span class="custom-time">
      <input type="checkbox" id="TimeSlotCheckBox" name="TimeSlotCheckBox" value='<%# Eval("PreviousTimeSlotsDate") %>' runat="server"/>
      <asp:label AssociatedControlID="TimeSlotCheckBox" runat="server" Text='<%# Eval("PreviousTimeSlotsString") %>'></asp:label>
<span class="box"><span class="tick"></span></span></span>

This will generate the below html wherein the ID of the checkbox will match the label for attribute value.

<span class="custom-time">
     <input name="MobileTimeSlotListView$ctrl0$TimeSlotCheckBox" type="checkbox" id="MobileTimeSlotListView_ctrl0_TimeSlotCheckBox" value="5/23/2012 10:30:00 PM" />
     <label for="MobileTimeSlotListView_ctrl0_TimeSlotCheckBox">May 23 - 10:30 PM - 11:00 PM</label>
<span class="box"><span class="tick"></span></span></span>

Create Windows Service on Windows Server 2008

Make sure to open the command prompt with "Run as Administrator"

C:\Windows\system32>sc create SERVICE_NAME binpath= D:\FOLDER_NAME\EXE_NAME.exe

Note: There is a space after binpath=

c# - Storing Dictionary/Hastable in HttpCookie.Workaround for Serialization/Deserialization

I came across a situation where I had to store a Dictionary<string,string> in a HttpCookie.When I searched for achieving this the solution that I came across was to serialize the dictionary to a string to store it and then deserialize when needed.

The workaround is to prefix the Dictionary keys with a constant string that is not a part of any key in the HttpCookie and then use it retrieve the Dictionary keys.
The code is below//Store Dictionary in HttpCookieprivate void SetCookieValue(Dictionary<string,string> o_Dictionary)
{
        foreach (KeyValuePair<string, string> oKeyValuePair in o_Dictionary)                                                      
                oCookie.Values[oKeyValuePair.Key + "CONSTANT_STRING"] = oKeyValuePair.Value;
}

//Retrieve Dictionary Key/Values
private Dictionary<string, string> GetDictionaryCookieValue()
{
       Dictionary<string, string> oDictionary = new Dictionary<string, string>();           
       try
       {
             string[] saAllKeys = oCookie.Values.AllKeys;                  
 
              for (int iIndex = 0; iIndex < saAllKeys.Length; iIndex++) 

            {
                    if (saAllKeys[iIndex].Contains(" CONSTANT_STRING "))                               oDictionary.Add(saAllKeys[iIndex].Replace("CONSTANT_STRING",string.Empty), oCookie.Values.Get(saAllKeys[iIndex]));
            }              }
       catch { //Catch exception  }
       return oDictionary;
}


The same workaround can be used for a Hashtable as well.


Friday, June 29, 2012

Check INDEX exists and DROP index SQL Server

declare @index_name varchar(50)

set @index_name = (select name from sys.indexes where name='index_name' AND object_id = object_id('table_name'))

if(@index_name) is not null

begin 
     drop index table_name.index_name
end

Wednesday, September 5, 2007

Accessing Formview controls Dynamically ASP.Net code behind file

Ever tried to access the formview controls programmatically?Well I did and had to spend quite sometime to figure out because I am no expert in ASP.net.
The requirement was to loop or iterate through all the controls on the page and set some of the properties of the controls dynamically.Since I wanted to do this before I could tie or bind the data in Page_Load I put the code in Page_Init. To add to my woes the technology chosen for the middle tier does not allow me to step through and I have been using logging tool send the values and manually check.
foreach(Control ctrl in this.Page.Controls)
{
//Do something
}
The above code does not work for formview because the formview controls are not instanstiated till data is bound to it unless the default mode is insert mode.So in order to access the formview controls loop through formview controls after the data is bound to the controls i.e after Page_Load.Page_PreRender is a better option compared to Page_Init.

protected void Page_PreRender(object sender,EventArgs e)
{
foreach(Control ctrl in this.Page.Controls)
{
if(ctrl.GetType().ToString() == "FormView")
foreach(Control fctrl in ctrl.Controls)
{
//Do Something
}
}
}

Multiple Lines in a Label Control

Recently when I was designing an ASP.Net form I came across a strange requirement where in static text had to be displayed in multiple lines. When I looked at it looked really simple.Just have as many Label controls as the number of lines.

But then I did not want to waste multiple controls.Well...err..I tried something silly and trivial and it served the purpose.

Lets say "This is the sample text I want to display in a Label on an ASP.Net form in a project" and I want this to be displayed in multiple lines.

The silly method goes like this.


Drag and drop a Label on to the ASP.net form.
Choose the Text property for the Label in the Designer.
This is the sample text <(br)>I want to display in a Label <(br)> on an ASP.Net form in a project.
Note: Remove the brackets ( and ) which will result in adding a line break to the text.


When the page renders the label will display the above text as below



This is the sample text
I want to display in a Label
on an ASP.Net form in a project.

Shift + Delete

One of the most useful shortcuts that I use frequently is the Shift + Delete to delete a line.Often as a programmer I found it really annoying to delete the lines using either Backspace or Delete iteself, sometimes just to clear out the commented code or to clear out the extra lines in a program.
Shift + Delete allows to delete a line,empty line and with the up,down arrow keys it can be used to delete multiple lines.
Another useful shortcut used to delete an entire word is Shift + Control + Delete in combination with the arrow keys.