My Personal Blog's
[Asp.net] Convert datatable to Json
Feb 22nd
This code is used to convert datatable to json format. It also handle double quote and backslash issue.
public static string Convert(DataTable dt)
{
string sb = "";
sb += "[";
foreach (DataRow dr in dt.Rows)
{
sb += "{";
foreach (DataColumn dc in dt.Columns)
{
//Column Name
sb += "\"";
sb += dc.ColumnName;
sb += "\":";
//Column Value
sb += "\"";
//Value contain " must be replace with \"
sb += dr[dc].ToString().Replace(@"\", @"\\").Replace("\"", "\\\"");
sb += "\",";
}
if (sb.EndsWith(","))
{
sb = sb.Substring(0, sb.Length - 1);
}
sb += "},";
}
if (sb.EndsWith(","))
{
sb = sb.Substring(0, sb.Length - 1);
}
sb += "]";
return sb;
}
Collapsible Drag & Drop Panels like WordPress Dashboard using Jquery and ASP.NET
Apr 25th
I found a great article that teach us how to create drag and drop panel like dashboard in wordpress panel.
In this sites: http://webdeveloperplus.com/jquery/saving-state-for-collapsible-drag-drop-panels/ shows us how to create the drag and drop panel and save the state in PHP and mysql. In this article, I will show the code in ASP.NET using SQL Server 2005.
VB.NET – ASP.NET communication between user control and web page using event handler
Apr 21st
In this post, we want to show how button in user control communicate with web page in asp.net
First create user control called button.ascx.
Add 3 button, name it btnSave, btnEdit and btnCancel.
in button.ascx back code, add this code:
Public Partial Class button
Inherits System.Web.UI.UserControl
Public Event btnSaveHandler As System.EventHandler
Public Event btnEditHandler As System.EventHandler
Public Event btnCancelHandler As System.EventHandler
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
RaiseEvent btnSaveHandler(Me, New EventArgs())
End Sub
Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnEdit.Click
RaiseEvent btnEditHandler(Me, New EventArgs())
End Sub
Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
RaiseEvent btnCancelHandler(Me, New EventArgs())
End Sub
End Class
In Default.aspx, add 1 label called label1, then drag the button.ascx to the default.aspx.
Add code in default.aspx as below:
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = Request.QueryString("label")
AddHandler button1.btnCancelHandler, AddressOf cancelClick
AddHandler button1.btnEditHandler, AddressOf editClick
AddHandler button1.btnSaveHandler, AddressOf saveClick
End Sub
Private Sub cancelClick(ByVal sender As Object, ByVal e As EventArgs)
Response.Redirect("default.aspx?label=Cancel")
End Sub
Private Sub editClick(ByVal sender As Object, ByVal e As EventArgs)
Response.Redirect("default.aspx?label=Edit")
End Sub
Private Sub saveClick(ByVal sender As Object, ByVal e As EventArgs)
Response.Redirect("default.aspx?label=Save")
End Sub
End Class
Run it, you will see that button that we create in user control can be manipulate and used in default.aspx
See other example for communication between user control and page in:
http://www.codeproject.com/KB/user-controls/Page_UserControl.aspx
ASP.NET – GridView – Get Hidden Field Value in RowCommand
Sep 21st
There is a problem when we set BoundField visibility to false, the column isn’t rendered to the client. A work around would be to use a HiddenField within a TemplateField instead.
<asp:TemplateField HeaderText="MemberID">
<ItemTemplate>
<asp:LinkButton ID="lbtn_memberid" runat="server" CommandName="GetMemberID" CommandArgument='<%# DataBinder.Eval(Container, "DataItem.memberid") %>'
Text='<%# DataBinder.Eval(Container, "DataItem.memberid") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="fieldid" runat="server" Value='<%# Eval("fieldname") %>' />
</ItemTemplate>
</asp:TemplateField>
Back Code:
Protected Sub gridview1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridview1.RowCommand
If e.CommandName = "GetMemberID" Then
Session("GetMemberID") = e.CommandArgument.ToString()
Dim gv As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim i As Integer = gv.RowIndex
Dim row As GridViewRow = gridview1.Rows(i)
Dim hidden As HiddenField = DirectCast(row.Cells(0).FindControl("fieldid"), HiddenField)
Dim fieldid As Integer = CInt(hidden.Value)
End If
End Sub
Ask for username and password when calling reporting service
Jun 22nd
These code allow us to set username and password for reporting services to avoid popup request authentication.
This code tested in Visual Studio 2008 and Reporting Services from SQL Server 2005
First create a class called ReportServerCredentials.vb
Imports Microsoft.VisualBasic
Imports Microsoft.Reporting.WebForms
Imports System.Security.Principal
Public NotInheritable Class ReportServerCredentials
Implements IReportServerCredentials
Public ReadOnly Property ImpersonationUser() As WindowsIdentity _
Implements IReportServerCredentials.ImpersonationUser
Get
'Use the default windows user. Credentials will be
'provided by the NetworkCredentials property.
Return Nothing
End Get
End Property
Public ReadOnly Property NetworkCredentials() As Net.ICredentials _
Implements IReportServerCredentials.NetworkCredentials
Get
'Read the user information from the web.config file.
'By reading the information on demand instead of storing
'it, the credentials will not be stored in session,
'reducing the vulnerable surface area to the web.config
'file, which can be secured with an ACL.
'User name
Dim userName As String = _
ConfigurationManager.AppSettings("UserName")
If (String.IsNullOrEmpty(userName)) Then
Throw New Exception("Missing user name from web.config file")
End If
'Password
Dim password As String = _
ConfigurationManager.AppSettings("Password")
If (String.IsNullOrEmpty(password)) Then
Throw New Exception("Missing password from web.config file")
End If
'Domain
Dim domain As String = _
ConfigurationManager.AppSettings("SERVERNAME")
If (String.IsNullOrEmpty(domain)) Then
Throw New Exception("Missing domain from web.config file")
End If
Return New Net.NetworkCredential(userName, password, domain)
End Get
End Property
Public Function GetFormsCredentials(ByRef authCookie As System.Net.Cookie, _
ByRef userName As String, _
ByRef password As String, _
ByRef authority As String) _
As Boolean _
Implements IReportServerCredentials.GetFormsCredentials
authCookie = Nothing
userName = Nothing
password = Nothing
authority = Nothing
'Not using form credentials
Return False
End Function
End Class
Second, add parameter key to your web.config
<appSettings> <add key="UserName" value="UserName"/> <add key="Password" value="Password"/> <add key="SERVERNAME" value="SERVERNAME"/> </appSettings>
And then add this line of code before calling reportserverURL in the report view page:
ReportViewer1.ServerReport.ReportServerCredentials = New ReportServerCredentials()
Get first day of the month and last day of the month in VB.NET
Jun 3rd
This code show how to get first date of the month and last day of the month date.
Private Function GetFirstDayOfMonth(ByVal dtDate As DateTime) As DateTime
Dim dtFrom As DateTime = dtDate
dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1))
Return dtFrom
End Function
Private Function GetLastDayOfMonth(ByVal dtDate As DateTime) As DateTime
Dim dtTo As New DateTime(dtDate.Year, dtDate.Month, 1)
dtTo = dtTo.AddMonths(1)
dtTo = dtTo.AddDays(-(dtTo.Day))
Return dtTo
End Function
'if you put code like:
GetFirstDayOfMonth("2010-05-05") ' It will return = 2010-05-01
GetLastDayOfMonth("2010-05-05") ' It will return = 2010-05-31







