Recent Posts

Monday, June 1, 2009

Solve undefined method `render_text' in Ruby on rails

I have just started to work on Ruby on Rails and was going through step by step Ruby on rails tutorial. 

As i went through I got on to undefined method `render_text' .... Now what ???

so looking into various web sites I Finally got it

You can use

render :text =>"Hello World" Instead of render_text


Enjoy learning ruby


Read more!

Thursday, April 30, 2009

How to open a child window on row command, pass value to child window and return values to parent form in asp.net 2.0?

This is a very good asp net tutorial to address the issues of passing the values "to and from" a parent and child window in asp net 2.0.

In this asp net tutorial i have passed values from parent window to child window to tick check boxes and get values back from checkboxes from child and
pass it back in the text box in asp net gird view.
I got this working getting help from different websites and was hassle doing it.

So I thought of putting it together in one article so that it helps other programmers to save their time programming asp net 2.0 web sites.

I will explain my case:
I have a asp net 2.0 gridview where I bind the data from microsoft sql server 2005 database. In the grid I have a text box in one column and I have a button on column next to the text box column.
Now when I press the button then it has to pick the values from the textboxes delimited by some character, and open a new child window which has a grid populated from the database and has the check box. Now those check box has to be ticked reading the values passed from the textbox from the grid of the parent form.
Also when the user unticks the checkboxes or ticks additional checkboxes and closes the child form then it should pass back the checked values to the appropriate textbox on the grid of the parent form.

Ok Now lets Start the tutorial.

Here for this asp net 2.0 tutorial i have created two tables in my sql server database
Table Customers
Columns
ID int
Name varchar(50)
Cars varchar(100)


Table Cars
Columns
CD varchar(10)
CarName varchar(100)


Now in this application I will have a customer who has different cars and car CDs are recorded in column cars delemitated by "/".

This has to be done in some situations where you have no access to cars table.

Ok Now lests start a website in asp net 2.0 containing master and detail pages
(Learn how to design Master Detail pages in asp net 2.0)

Add two web forms in the website, first is Default.aspx (parent form) and link it to the Master page. Similarly add another web form cars.aspx but don't link it to the master page as it will act as child window.

Now add the database classes to connect to the database
(Learn how to connect to the database from asp net web site) and write the classes to retrieve the data from the customers table and Cars table from the database.
(Learn how to retrieve data from the database)

In my case I have class DBObj for connection to the database and I have a class RetrieveData for retrieving the data where I have three functions
I am planning to use object as a datasource in this tutorial so I am returning objects from the functions below:

(No clue about object data source in asp net 2.0 then Learn how to use object as data source in asp .net applications )

1. Public Function GetCars(ByVal strCars As String) As List(Of Cars) (returns list of cars)
2. Public Function GetCustomers(ByVal strCars As String) As List(Of Customer) (returns list of customers)
3. Public Function CheckCars(ByVal CD As String, ByVal strCars As String) As Boolean (checks if the car has to be ticked or not)

In Cars object I have a property called checked that determines if the car is in the customer list or not. So this function determines if this property has to be set true or not by getting stringCars as input.

I have added a Java script file in the Default.aspx page called Test.js
Now Lets bind a asp net 2.0 grid view named gvCustomers in Default.aspx page and asp.net grid view named gvCars in Cars page.

My Default.aspx page has a asp net 2.0 gridview with following columns :
ID, 
Customer Name,
Customer Cars (Text Box Field which is bound to CustCars field),
cmdCar (Command button to open up the Cars Window),


Note: And on cmdCar Under OnClientClick I have called a Java Script Function called openPage(this);

My default.aspx code looks like below:



And my Test.js has the following code:



Similarly my CarsFrm.aspx has a asp net 2.0 gridview with following columns:
CD
CarName
chkOk (Check box which can be selected by user)

And it has a command button called cmdOk

My CarsFrm.aspx code looks like the following:



Now when you run the application then you are able to see the gridview with data and command button and when the command button is pressed a new window for cars opens up. Now lets transfer data "to and from" Cars page.

Now here I will explain step by step which makes it easier to understand:

Step 1 .
On Default.aspx in grid gvCustomers cmdCar put the following code:
OnClientClick="openpage(this);return false;"

Step 2.
openpage(obj) on the java script will be called where it will open up cars.aspx page.
Also sends the data from adjacent text box to the cars page.

step 3.
put a hidden field "hdTest" in cars.aspx
asp:HiddenField ID="hdTest" runat="server"

and one label to test the hidden field is working or not and name it "lblTest"

Step 4.
in cars page load retrieve the data arrived with following code
strCars = Request.QueryString("target")
and bind the grid
gvCars.DataSource = obj.GetCars(strCars)
gvCars.DataBind()

GetCars retrives all the cars from the table and checks the ones that are on the string strCars

Step 5.
Now in cars.aspx page you need to set the databinder for checked as follows
asp:CheckBox ID="chkOK" checked=''

and on gvCars_DataBound put the following code to see if the code is passed ok :
For Each gvr As GridViewRow In gvCars.Rows

Dim cb As CheckBox = CType(gvr.FindControl("chkOK"), CheckBox)

If cb.Checked = True Then
strCarCode = strCarCode.Trim() & "/" & gvr.Cells(0).Text.Trim()

End If
Next

If strCarCode.Length > 0 Then
hdTest.Value = strCarCode.Substring(1, strCarCode.Length - 1)
lblTest.Text = strCarCode.Substring(1, strCarCode.Length - 1)

End If

(Now try to run the application and enter the car code in the text box now you can see the checkbox checked in cars Grid)

Step 6.

Now in Default.aspx register a javascript function called update to update the textbox in the grid.
like folows
scr = ""

' register the javascript into the Page
ClientScript.RegisterClientScriptBlock(Me.GetType, "update", scr)


Step 7.
similarly in cars.aspx page register a function to send it to Default.aspx page
like follows:

strTextId = Request.QueryString(1)
scr = ""

ClientScript.RegisterClientScriptBlock(Me.GetType, "done(fName,strId)", scr)

and
cmdOk.OnClientClick = "Done('" & str & "','" & strTextId & "')"

Step 8.
Now we are ready to send the data to the main form from the child form in asp .net website.
Now we have to get the data and put it in correct format and send it to the main form:

in cars.aspx page just assign the event to check box as OnCheckedChanged="chkOk_CheckedChanged"
and write the following code to get the value of the checkbox and format the data:
Dim chkbox As CheckBox
       Dim strText As String = ""

       chkbox = CType(sender, CheckBox)

       Dim row As GridViewRow = CType(chkbox.NamingContainer, GridViewRow)

       strText = hdTest.Value

       If chkbox.Checked = True Then
           If strText = "" Then
               strText = strText & row.Cells(0).Text.Trim

           Else
               strText = strText & "/" & row.Cells(0).Text.Trim

           End If

       Else
           If InStr(strText, "/") > 0 Then
               strText = strText.Replace("/" & row.Cells(0).Text.Trim, "")
               strText = strText.Replace(row.Cells(0).Text.Trim & "/", "")
           Else
               strText = strText.Replace(row.Cells(0).Text.Trim, "")
           End If
       End If

       hdTest.Value = strText
       lblTest.Text = hdTest.Value
   End Sub


Note : remember to do AutoPostBack="true" for checkbox so that the hidden field is written at the time when we check the box
The checkbox in Cars page will look like follows:
asp:CheckBox ID="chkOK"
checked=''
runat="server"
AutoPostBack="true"
OnCheckedChanged="chkOk_CheckedChanged" /

Now run the website it should be passing data to and from the parent and child forms.

Download Full surce code for "How to open a child window on row command, pass value to child window and return values to parent form in asp.net 2.0?"


Read more!

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!