Recent Posts

Thursday, December 10, 2009

How to post data to other site via "Post" in Asp.net?

If you need to post data to some other website or URL via post method then this article is the perfect solution.
This article references to the article provided in

I just followed the article and did it in vb. You can click on above link to get the c# code.

As per the above article




Possible Solutions

  1. One possible solution to this problem is to Create your own form control and use it on page this will allow you to change action of form, but again what if you do not want some existing input elements in current page to go to post.
  2. There is good way to post form data using HttpWebResponse & HttpWebRequest class if you want to post data behind the scenes, but if you want to post data using user browser then you are stuck

We have solved this by following way:

Create a RemotePost class (responsible for doing the job) as below:

Public Class RemotePost
Private Inputs As System.Collections.Specialized.NameValueCollection = _
New System.Collections.Specialized.NameValueCollection

Public URL As String = ""
Public Method As String = "post"
Public FormName As String = "form1"

Public Sub Add(ByVal name As String, ByVal value As String)
Inputs.Add(name, value)
End Sub

Public Sub Post()

Dim i As Int16
'System.Web.HttpContext.Current.Response.Clear()
'System.Web.HttpContext.Current.Response.Write("")
'System.Web.HttpContext.Current.Response.Write(String.Format("", FormName))
'System.Web.HttpContext.Current.Response.Write(String.Format("", FormName, Method, URL))

System.Web.HttpContext.Current.Response.Clear()

System.Web.HttpContext.Current.Response.Write("")

System.Web.HttpContext.Current.Response.Write(String.Format(""))
System.Web.HttpContext.Current.Response.Write(String.Format("
"))

Dim str As String
For i = 0 To Inputs.Keys.Count - 1
str = Inputs.Keys(i)
str = Inputs(Inputs.Keys(i))
str = "input name="" type="" value=""" '(Note: put the <> tags)

System.Web.HttpContext.Current.Response.Write(String.Format(str))
Next
'System.Web.HttpContext.Current.Response.Write("")
'System.Web.HttpContext.Current.Response.Write("")

System.Web.HttpContext.Current.Response.Write("")
System.Web.HttpContext.Current.Response.Write("")

System.Web.HttpContext.Current.Response.End()

End Sub


End Class

Now The posting Page which has a linkbutton

asp:LinkButton id="lnkbtnSmartTickets" Runat="server" >test post< /asp:LinkButton

And the code looks like below:

Private Sub lnkbtnPostThis_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim myRemotePost As New RemotePost
myRemotePost.URL = "Receiver.aspx"
myRemotePost.Add("param1", "this is prateek 1")
myRemotePost.Add("param2", "this is regmi2")
myRemotePost.Post()

End Sub



Now Receiver.aspx will have following code on its form load

If Not Request.Form("param1") = Nothing Then
Response.Write("param1: " & Request.Form("param1"))
End If
If Not Request.Form("param2") = Nothing Then
Response.Write("param2: " & Request.Form("param2"))
End If








Read more!

Monday, December 7, 2009

How to fix Error while trying to run project: Unable to start debugging on the web server

I'm running 2 frameworks 2.0 and 1.1. I had to do the following to switch to 1.1.In a dos prompt do the following:
cd c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
aspnet_regiis.exe -u
cd c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322
aspnet_regiis.exe -i

Its hectic but its better to create a batch file to run this when ever required

thanks todymcgee


Read more!

Thursday, December 3, 2009

Problems converting string date to Date or Date time in .net

I was having issue publishing my web site in the Server its regional settings were different to mine and my Asp .net website was giving me following error

" String was not recognized as a valid DateTime "

Since it was working fine in my computer and in debugging mode.

It wasted my 2 hours and finally i changed the code to following to solve the problem

dt = New DateTime(Convert.ToInt16(strYr), Convert.ToInt16(strMon), Convert.ToInt16(strDay))

or if date and time both is required then

dt = New DateTime(Convert.ToInt16(strYear), Convert.ToInt16(strMonth), Convert.ToInt16(strDay), Convert.ToInt16(strHr), Convert.ToInt16(strMin), 0)

This would run without error no matter what regional settings is in the server so it is simpe code but pretty handy. Enjoy coding


Read more!

Monday, October 26, 2009

Simple steps listed to create a ruby on rails application

This article lists all the steps required to create a simple web site in ruby on rails.
This article a fast track approach in developing a ruby on rails website.

First download and install the ruby on rails. If you haven't yet then you can go to Ruby on rails website and then download and install and start mysql and Apache web server that can be done by starting InstantRails.exe in the Instant rails folder.

Now go to command prompt and enter use_ruby

This will take you to rails application path if ruby on rails has been installed properly in the drive.
Then follow the following steps to create a complete website:

1 . rails AppName

2. Change database.yml file to point to required database

3. rake db:create:all Creates the database
4. ruby script/generate scaffold Table1 column1:string column2:text column3:integer

5. ruby script/generate scaffold Table2 column1:string column2:text column3:integer

6. change db/migrate/ 001_create_table1.rb file specify the limit and add t.references :table2 for foreign key

7. rake db:migrate

8. Open up modals and setup Active record base for recipie and category.

9. put has_many :recipes in category ActiveRecord:: base

10. put belongs_to:category in reciepe ActiveRecord:: base

11. go to public folder and delete index.html

12. open routes.rb file from config folder and find map.root and enable that portion.

13. Set map.root :controller => "categories"

14. Now open C:\InstantRails-2.0\rails_apps\astroun\app\views\categories\new.html.erb and
C:\InstantRails-2.0\rails_apps\astroun\app\views\recipie\new.html.erb

15. delete the portion between <% form_for(@recipe) do |f| %>
<% end %> from recipie.

16. create a new file called _form.html.erb which is partial and it renders the form.

17. Paste the portion deleted in new.html.erb into this new file _form.html.erb and

18. Now put the render code in the deleted portion of new.html.erb :
<%= render :partial => "form", :locals => { :f => f, :button => "Create" } %>

19. And similarly in edit.html.erb put
<%= render :partial => "form", :locals => { :f => f, :button => "Update" } %>

20. Now run and see by doing ruby script/server and run http://localhost:3000/

21. Scaffold creates layouts in views that can be delted and modified as per need.

22. Lets delete these files in layout folder and create application.html.erb as application wide layout.

23. Now it works fine but with one problem if there are reciepies in category but if we destroy the category then when we try to list the recipies that throws error because category is not there.

24. To fix this make changes in categories_controller saying if it has items in it then don't destroy the category.








Read more!

Tuesday, September 22, 2009

How to use Json objects and Jqeury in Asp.net? Part -3

This is continuation of the tutorial how to use JSON objects and JQuery in Asp.net part -2
which explains the aspx page and how to use JQuery to render the data into the aspx page. This part of the tutorial explains how to use a handler to communicate between the Java script and the business logic layer and return back the json objects data. If you want to go to the starting of this article then it can be found in How to use JSON objects and JQuery in Asp.net part -1.

Lets add the handler in the Asp .net web project and name it jqAirlines.ashx.
My whole code for jqAirlines.ascx can be found here:





Now in sub ProcessRequest check the parameters received (action).

I have declared the business layer as bll and lstAir as list of my TarrifAirlines

and similarly set the contextType = "application/json" that means we will be returning json format data back

Similarly get the pageSize and startRecord and put them in variables.

Then it calls the function bll.CountAirlinesJQ that gives the count of the records and then calls the function bll.GetAirlinesJQ that gets the list of TariffAirlines which is explained in How to use Json objects and Jqeury in Asp.net? Part -1.

Now the lstAir is the list of TariffAirlines and we need to convert it to json format, so we can get stuck here and try to find the ways or even plan to write the function to convert it. It is a lengthy process so why not use opensource dll (Newtonsoft.Json.dll) by Newtonsoft which can be downloaded from Json.Net
strJsonAir = Newtonsoft.Json.JavaScriptConvert.SerializeObject(lstAir)

Now strJsonAir would have list of TariffAirlines as a serilized string in json format.

We now declare a stringBuilder named writer and lets append with count, pagesize, startRecord and data that contains the collection of TarrifAirlines ie strJsonAir in the JSON format and write it back which is then used by our javascript explained in How to use Json objects and Jqeury in Asp.net? Part -2

Now hope you understand the now how to use JQuery, and render the JSON objects in three tier ASP. net web applications. If any confusion or suggestions please feel free to comment or directly email me.




Read more!