2009/10/09

C# - Sys.webforms.pagerequestmanagerparsererrorexception: the message received from the server could not be parsed

Error: Sys.webforms.pagerequestmanagerparsererrorexception: the message received from the server could not be parsed

Problem: if you are using updatepanel (ajax) then you have to follow the list below:

(To start with, don't do anything from the preceding list! Here's a matching list of how to avoid a given error (when possible):

  1. Calls to Response.Write():
    Place an or similar control on your page and set its Text property. The added benefit is that your pages will be valid HTML. When using Response.Write() you typically end up with pages that contain invalid markup.
  2. Response filters:
    The fix might just be to not use the filter. They're not used very often anyway. If possible, filter things at the control level and not at the response level.
  3. HttpModules:
    Same as response filters.
  4. Server trace is enabled:
    Use some other form of tracing, such as writing to a log file, the Windows event log, or a custom mechanism.
  5. Calls to Server.Transfer():
    I'm not really sure why people use Server.Transfer() at all. Perhaps it's a legacy thing from Classic ASP. I'd suggest using Response.Redirect() with query string parameters or cross-page posting.

Another way to avoid the parse error is to do a regular postback instead of an asynchronous postback. For example, if you have a button that absolutely must do a Server.Transfer(), make it do regular postbacks. There are a number of ways of doing this:

  1. The easiest is to simply place the button outside of any UpdatePanels. Unfortunately the layout of your page might not allow for this.
  2. Add a PostBackTrigger to your UpdatePanel that points at the button. This works great if the button is declared statically through markup on the page.
  3. Call ScriptManager.RegisterPostBackControl() and pass in the button in question. This is the best solution for controls that are added dynamically, such as those inside a repeating template.

Enjoy!

2009/10/07

SQL- Split string function

Create this function:
CREATE FUNCTION dbo.Split(@String nvarchar(4000), @Delimiter char(1))
returns @Results TABLE (Items nvarchar(4000))
as
begin
declare @index int
declare @slice nvarchar(4000)

select @index = 1
if @String is null return

while @index != 0

begin
select @index = charindex(@Delimiter,@String)
if @index !=0
select @slice = left(@String,@index - 1)
else
select @slice = @String

insert into @Results(Items) values(@slice)
select @String = right(@String,len(@String) - @index)
if len(@String) = 0 break
end return
end


you can try the line below:

select * from dbo.split('a,b,c,d,e,f,g,h,i,j,k,l', ',')

SQL- clone table instantly (SQL)

Sometimes we want to clone a table. How to do it quick? Look at the code below:

select t.* into [#temp]
from TABLENAME t
where 1 = 0

The above code will create a temp table [#temp] with the same properties of table TABLENAME. Some of you might get confused why there is a line "where 1 = 0" what is that means? Actually this means that we do not want to clone the data, we just want to clone the table. So in here 1 = 0 will always false and so there will be no data insert into the temp table.

Enjoy

2009/10/06

C# - Load Attachment

Sometimes when you load your file, the filename might include some special character. Look at the code below:

HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename=" + HttpUtility.UrlEncode(filenameArry[0], System.Text.Encoding.UTF8)));

When the system try to load the file with special character in the filename (like &, ;, etc) then there will be problem while reading it. You have to append the following to avoid the problem:

HttpUtility.UrlEncode(filename)

HttpUtility.Encode can solve the problem.

Enjoy.