Recent Posts

Tuesday, March 31, 2009

How to merge columns of a gridview in Asp net 2.0?

I was playing around gridview to find out the ways to merge the cells in asp net 2.0 gridview as we do in Microsoft excel. After working few hours on it with the help of google i got a function ready for the new programmers to learn how to merge cells in asp net 2.0 gridview which has got identical data.
You can just use the following function to merge the cells of asp net 2.0 gridview as in Microsoft excel:

Public Shared Sub MergeCells(ByVal gridView As GridView)
        For rowIndex As Integer = gridView.Rows.Count - 2 To 0 Step -1
            Dim row As GridViewRow = gridView.Rows(rowIndex)
            Dim previousRow As GridViewRow = gridView.Rows(rowIndex + 1)

            For cellIndex As Integer = 0 To row.Cells.Count - 1
                If row.Cells(cellIndex).Text = previousRow.Cells(cellIndex).Text Then
                    If previousRow.Cells(cellIndex).RowSpan < 2 Then
                        row.Cells(cellIndex).RowSpan = 2
                    Else
                        row.Cells(cellIndex).RowSpan = previousRow.Cells(cellIndex).RowSpan + 1
                    End If
                    previousRow.Cells(cellIndex).Visible = False
                End If
            Next cellIndex
        Next rowIndex
    End Sub


Read more!

Monday, March 23, 2009

How to find the week number for a date of a month .net?

If you want to show the report in .net or asp .net application for a month and divide the data in weekly basis then you need to identify the week number to show the data accordingly. The following .net code allows you to find the week number for the given date of a month.

Dim userDate As DateTime
       Dim remain As Integer
       Dim noOfWeek As Integer

       userDate = DateTime.Parse("01/02/2010")

       noOfWeek = Math.DivRem(userDate.Day, 7, remain)

       If remain > 0 Then
           noOfWeek = noOfWeek + 1

       End If
       MessageBox.Show("The week no is :" & noOfWeek.ToString())


Enjoy coding


Read more!

Sunday, March 15, 2009

How to use Session in asp.net 2.0 web site design?

This article can also be defined as " How pass an object from one page to another using Session in asp.net 1.0 or asp.net 2.0 application?"
This asp.net tutorial shows how to use session for passing data from one page to other page using asp.net session.
This article is very helpful for validating the user name and password while asp net 2.0 login or asp.net 1.0 login using session. This process validates the user on every page of asp.net 2.0 or asp.net 1.0 website.
The data passed is only valid till the asp net session has not timed out. If the asp net session is timed out then the website redirects it to the asp net login page where the user has to enter the username and password again. The asp net session timeout information can be set as per required by the business.
Ok Lets go to the actual process on how to use asp net session in asp.Net web site development?


First open the asp.net web site or Learn how to develop an web site in .Net

Click on Ok or Submit button on Login.aspx page and include the following code:

Protected Sub cmdOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdOk.Click
       Dim strUser As String = Me.txtUserName.Text.Trim()
       Dim strPass As String = Me.txtPassword.Text.Trim()
       If strUser = "" Or strPass = "" Then Exit Sub

       Dim usr As User
       Try

       
           usr = BUser.AuthenticateUser(strUser, strPass)

           If usr Is Nothing Then
               lblMsg.Text = "Login failed"
           ElseIf usr.SecurityLevel = 0 Then
               lblMsg.Text = "You are not allowed in this site"
           Else
               Session("SFUser") = usr
               'Response.Redirect("Default.aspx")
               Response.Redirect("~\Forms\AgentAccess.aspx")
           End If
       Catch ex As Exception
           lblMsg.Text = ex.ToString()
       End Try
   End Sub


In the above code you can see I have picked up username, password from the text box and declared a User Class and got the user information from BUser.AuthenticateUser function. BUser is my Business Layer and Authenticate user function returns me the user information.
If you didn't get the clue about Business Layer then you can Learn How to Develop three tier asp.net Application

Then I have said if the user is nothing then that means the user has failed the login or security level is 0 then the user is prohibited.
Now if the login is successfull then I have put the user in session named "User"

Now lets go to the default.aspx page where the user will be retrieved from the session and check if it is valid.
Declare user on the top if you want to use the user details on this page.
Protected usr As User

On Page_Load event of Default.aspx write the following code:

usr = Session("SFUser")
      If usr Is Nothing Then Response.Redirect("~/login.aspx")

      Me.lblUser.Text = "Logged in as " & usr.UserID


Here I retrieved the user from the session and checked if it is nothing or not if it is nothing that means the user's session has timed out or the user is not coming from the login page, the user is redirected to the login page.

If the user is valid then I have included user name a Label control.

Enjoy the code and do not forget to comment.



Read more!