Monday, May 28, 2012

RadGrid bind detail table in code behind

protected void RadGrid1_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e) { GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem; switch (e.DetailTableView.Name) { GetDataByParentId(dataItem.GetDataKeyValue("Id").ToString()); } }

Thursday, May 17, 2012

c# .net web application add server control into literal using stringbuilder

StringBuilder sb = new StringBuilder();
TextBox TextBox1 = new TextBox ();
TextBox1.ID = "TextBox1";
TextBox1.Text = "Hello World";
using (StringWriter sw = new StringWriter(sb))
{
using (HtmlTextWriter tw = new HtmlTextWriter(sw))
{
TextBox1.RenderControl(tw);
}
}

Wednesday, May 16, 2012

tsql update datetime value

datetime value can be updated using the following query:
e.g. I want to replace the year value with '2012'

update table1

set date = convert(datetime,'2012'+'-'+convert(varchar(3),MONTH(date))+'-'+convert(varchar(3),DAY(date))+' '+ convert(varchar(12),convert(time,date))),

  CompletedDate = convert(datetime,'2012'+'-'+convert(varchar(3),MONTH(CompletedDate))+'-'+convert(varchar(3),DAY(CompletedDate))+' '+ convert(varchar(12),convert(time,CompletedDate)))

where [condition]

Tuesday, May 08, 2012

tsql add primary key column on existing table

I can add a new column to an existing table no problem, but i had problem making it an autoincrement int primary key. After some research I found the solution: "Identity" keyword:

if not exists(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table1' AND COLUMN_NAME = 'ID')
begin

    ALTER TABLE dbo.table1
    add ID int identity not null
       
end
go

Wednesday, May 02, 2012

solution to: c# asp.net usercontrol javascript only executes first time

I have this line to run a javascript from code behind:
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "sbScript", sbScript.ToString(), false);

This line is inside pageload event of the usercontrol that I used my my aspx page. The user control is used multiple times on the aspx page. The javascript is therefore called multiple times. However the script only works the first time.

The reason being is because the script name specified is always "sbScript" which has been assigned value when first run. The solution is to make sure the script name is set to different values everytime the line is executed.

Tuesday, May 01, 2012

Attempt by security transparent method 'xxxxxxxxx' to access security critical method 'xxxxxxxx' failed.


I converted my website to a web application and upgraded .net framework from v3.5 to v4. I also setup a class library to host entity framework models for the web app to consume. At run time when the business logic calls the entity framework class library I got this error below.

Attempt by security transparent method 'xxxxxxxxx' to access security critical method 'xxxxxxxx' failed.

Assembly 'xxxxxxxxxxx' is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model.  Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception.

 The solution is to specify the security level in the business logic project AssemblyInfo.cs, add the following line:
[assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]


 

Monday, April 30, 2012

asp.net web application: assembly generation failed referenced assembly does not have a strong name

in my asp.net web application I got this problem when added a new class library into my solution and trying to build the solution:
"assembly generation failed referenced assembly does not have a strong name"

Apparently the solution is to go to the class Project properties: Solution Explorer > right click > properties, click on Signing tab, select "Sign the assembly", then browse the key shared among other projects within the same solution.

Tuesday, April 24, 2012

Tuesday, March 13, 2012

jquery get asp.net label text

var labelText = $('#<%= lblabc.ClientID %>').html();

jquery find server control checkbox checked state

if ($('#isAgeSelected').is(':checked')){
//your code here
}

Friday, March 09, 2012

tsql get part of a datetime value

DATEPART(portion, datetimevalue)

the options for portion are:

Ms for Milliseconds
Yy for Year
Qq for Quarter of the Year
Mm for Month
Dy for the Day of the Year
Dd for Day of the Month
Wk for Week
Dw for the Day of the Week
Hh for Hour
Mi for Minute
Ss for Second

e.g.
select startdate,DATEPART(Hh,a.startdate)

this returns the hour of startdate

Wednesday, February 29, 2012

mvc format databound datetime column

if you want something like "22/11/2012 15:00" then use
columns.Bound(o => o.MyDateTimeValue).Format("{0:dd/MM/yyyy HH:mm}").Width(120);

if you want something like "22/11/2012 3:00 PM" then use
columns.Bound(o => o.MyDateTimeValue).Format("{0:dd/MM/yyyy hh:mm tt}").Width(120);

Tuesday, February 28, 2012

t-sql add new line in code

i keep forgetting this simple solution:
char(13)
now I save it here so that I never forget again

Tuesday, February 14, 2012

c# split multi-line postal address

I have a multi-line postal address (string PostalAddressString) like this:

Address1:12 Garden Street
Address2:
Suburb:Abc
PostCode:123
State:BC
Country:ABCDE


In order to separate details and access them individually i need to do this:



NameValueCollection lines = new NameValueCollection();
string[] TempArray = PostalAddressString.Split(Environment.NewLine.ToCharArray());
if (TempArray.Count() > 5 || TempArray[0].ToLower() != "n/a")
{
foreach (string line in TempArray)
{
string[] parts = line.Split(':');
if (line.Length > 0)
lines.Add(parts[0].Trim(), parts[1].Trim());
}

string PostalAddressLine1String = lines.Get("Address Line 1");
string PostalAddressLine2String = lines.Get("Address Line 2");
string PostalSuburbString = lines.Get("Suburb");
string PostalPostCodeString = lines.Get("PostCode");
string PostalStateString = lines.Get("State");
string PostalCountryString = lines.Get("Country");
}

Thursday, February 09, 2012

Telerik RadGrid how to bind detailtable from code behinde

Use this method:

OnDetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)

The ParentKeyValues can be accessed this way:

e.DetailTableView.ParentItem.GetDataKeyValue("StoreId")

Tuesday, February 07, 2012

t-sql update table with join


update d
set d.dname = "HR"
from Department d
inner join employee e
on e.employeeID = d.employeeID
where d.departmentID = 1

Tuesday, November 22, 2011

Stimulsoft relax Master Detail table relationships

When I setup master detail table relationship in my report i noticed that the master records which do not have any related detail records will not be shown.

The trick is to set "Print if Detail Empty" property of the master data band to "true"

Friday, November 11, 2011

Asp.Net retrieve Web.config AppSettings in c# code behind

have a reference:

using System.Web.Configuration;

then use this code to retrieve the value:
WebConfigurationManager.AppSettings["propertyName"]

Thursday, October 20, 2011

Telerik RadGrid databinding throws error when open template edit form in insert mode with boolean controls e.g. checkbox

solution is to have a corresponding GridBoundColumn, and set DefaultInsertValue="false" in the column definition

Telerik RadGrid shows second page after opening the insert form

my radgrid uses Tempalte in EditFormSettings. It current contains 2 pages of data.
When I click on Add New Record the command button on top of the grid, the edit form opens correctly in insert mode. However when I look closely at the data, it actually shows the second page, and the page number on the bottom of the grid shows "2".


The solution is to set