Posts Tagged ‘asp.net’

Export Datagrid Data to Excel error ‘DataGridLinkButton’ must be placed inside a form tag with runat=server.

I have been browsing on web to find the exact solution. Unfortunately, lots of different suggestions which also not working, and finally I have found an article that’s very interesting.

One reason this error comes up is when the datagrid contains a server control, so you need to verify it before complaining.

There are 2 (maybe more) ways to solve this issue that I have proven and tested.

1. Disable features of the datagrid like sorting and remove unnecessary server controls.

2. If the above solution is not possible because you really need it. So, what you need to do is to clear the server controls on the fly, means you have to implement a sort of mechanism to work, but don’t worry about it there’s already a script that will help you to ease the work.

  1.  
  2.     Public Sub ClearControls(ByVal control As Control)
  3.         'This function is used to Clear all the controls in the datagrid
  4.         'Will Clear all the ServerControls Which are in the Datagrid
  5.         'Such as edit command, paging, checkboxes in datagrid and all
  6.         'the server controls
  7.         'it will replace the server control with simple text for that control
  8.         'Where Can this be Used:
  9.         'Exporting Datagrid and Clearing Datagrid
  10.         Dim i As Integer
  11.         For i = control.Controls.Count - 1 To 0 Step -1
  12.             ClearControls(control.Controls(i))
  13.         Next i
  14.  
  15.         If Not TypeOf control Is System.Web.UI.WebControls.TableCell Then
  16.             If Not (control.GetType().GetProperty("SelectedItem") Is Nothing) Then
  17.                 Dim literal As New LiteralControl
  18.                 control.Parent.Controls.Add(literal)
  19.                 Try
  20.                     literal.Text = CStr(control.GetType().GetProperty("SelectedItem").GetValue(control, Nothing))
  21.                 Catch
  22.                 End Try
  23.                 control.Parent.Controls.Remove(control)
  24.             Else
  25.                 If Not (control.GetType().GetProperty("Text") Is Nothing) Then
  26.                     Dim literal As New LiteralControl
  27.                     control.Parent.Controls.Add(literal)
  28.                     literal.Text = CStr(control.GetType().GetProperty("Text").GetValue(control, Nothing))
  29.                     control.Parent.Controls.Remove(control)
  30.                 End If
  31.             End If
  32.         End If
  33.         Return
  34.     End Sub 'ClearControls
  35.  
  36.     Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownload.Click
  37.         Response.Clear()
  38.         Dim filename As String = "filename_" & Strings.Format(DateTime.Today, "MM-dd-yyyy") & ".xls"
  39.  
  40.         Response.Clear()
  41.         Response.AddHeader("content-disposition", "attachment; filename=" & filename)
  42.         Response.Charset = ""
  43.         Response.ContentType = "application/vnd.xls"
  44.         Me.EnableViewState = False
  45.         Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter()
  46.         Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
  47.         Response.Buffer = False
  48.         ClearControls(DataGrid1)
  49.         Me.DataGrid1.RenderControl(htmlWrite)
  50.         Response.Write(stringWrite.ToString())
  51.         'stringWrite.Dispose()
  52.         'htmlWrite.Dispose()
  53.  
  54.         Response.End()
  55.  
  56.         'Download()
  57.  
  58.     End Sub

, , ,

No Comments


BC30456: ‘InitializeCulture’ is not a member of

1. If you use Visual studio to publish your site, during the publishing stage on framework 2.0 uncheck the “allow this precompiled site to be updatable”.

2. Ensure ASP.Net is installed correctly, I found that my Web Server root was configured to use ASP.Net 1.1 by default so ran the following line to fix it to 2.0 even though my site was configured for 2.0 at site level, eliminating this glitch seems logical.

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -i

This will also fix any mapping/installation problems.

Also ran aspnet_regiis -r

This will replace all mappings recursively to 2.0 regardless.

3. Make sure page directives at the top of aspx files are correct and ‘inherits’ is pointing to your class correctly. I could not see any problems with mine, so did not explore down this path to thoroughly, but noted others saying issues with ambiguous inheritance maybe related.

4. Declare culture in your web.config, example

<globalization uiCulture=”en” culture=”en-NZ” />

OR

<globalization uiCulture=”auto” culture=”auto” />

5. Change debug=”true” to “false” in web.config and any pages which have it set (I recommend removing it entirely from pages and just using web.config)

Source: MSDN Forum

,

No Comments


How to return the Last Inserted ID in Datatable

Public Function AddToSubGroupTable(ByVal ParentID As Int32, ByVal masterID As Int32, ByVal FieldName As String, ByVal fieldAlias As String, ByVal Value As String, ByVal ValueText As String) As Long

Try

Dim dr As DataRow

With dtSubGroup.Rows

dr = dtSubGroup.NewRow

dr(“ParentID”) = ParentID

dr(“FieldName”) = FieldName

dr(“MasterFileID”) = masterID

dr(“Fieldalias”) = fieldAlias

dr(“Value”) = Value

dr(“ValueText”) = ValueText

.Add(dr)

End With

dtSubGroup.AcceptChanges()

Return dr(“PKID”).ToString

Catch ex As Exception

Throw New System.Exception(ex.Message, ex.InnerException)

End Try

End Function

, ,

No Comments


Get Client IP Address

To get client IP address in ASP.NET

Function getIP() as String

Dim ip as string

ip = Request.UserHostAddress()

return ip

End Function

, , ,

No Comments


Connection TimeOut vs CommandTimeOut

Some programmers are confused the difference between of the ConnectionTimeout and CommandTimeout, so here is the distinction between the two.

ConnectionTimeout is the one specified in your connection string and it is the time it takes for a connection.Open() invocation to wait until it gets a connection reference from the connection pool.  (Default value is 15 seconds)

CommandTimeout is the maximum time for a specific sql command to execute.  (Default value is 30 seconds)

, , , ,

No Comments



SetPageWidth