Sunday, March 29, 2009

HttpWebRequest and HttpWebResponse

Following example I am trying to explain how we can request to a URL from our coding and read the Header content of the response.

Create a web application and add a Textbox and a Button to Default.aspx and double click on the button and add following code.

protected void Button1_Click(object sender, EventArgs e)
{
try
{
//Create a object for HttpWebRequest using this request object we will
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(TextBox1.Text);
//request a response from the URL
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
//Reading all header content of response
for (int i = 0; i < res.Headers.Count; i++)
{
Response.Write(res.Headers.AllKeys[i] + " : " + res.Headers[i].ToString() + "
");
}
}
catch (Exception ex)
{
//Catch if there are any exception
Response.Write(ex.Message);
}
}

Now we are ready with code, lets build, run and test

Hope this helps.

SatishKumar J
Microsoft MVP(ASP.NET)
Tags : HttpWebRequest, HttpWebResponse, Headers

Wednesday, March 25, 2009

Error Logging Windows Application VB.NET

Logging is very important for debugging exception at run time; normally logging will help us finding out problem in live environment.

I am post here a logging function using VB.NET, Just add this function in you class and create a directory in C: called Logs.

Public Sub Log(ByVal message As String)
             ' Check whether log.txt is already present or not
               If Not File.Exists("C:\Logs\log.txt") Then
                         'Create if not present
                       Dim fs As FileStream = New FileStream("C:\Logs\log.txt",   FileMode.OpenOrCreate, FileAccess.ReadWrite)
                      Dim s As StreamWriter = New StreamWriter(fs)
                           s.Close()
                            s.Dispose()
fs.Close()
fs.Dispose()
End If
'Open log.txt in append mode to log errors
Dim fs1 As FileStream = New FileStream("C:\Logs\log.txt", FileMode.Append, FileAccess.Write)
Dim s1 As StreamWriter = New StreamWriter(fs1)
'Wrting a line with error message
s1.Write("Message: " & message & vbCrLf)
'Logging the Date and Time of error
s1.Write("Date/Time: " & DateTime.Now.ToString() & vbCrLf)
s1.Close()
s1.Dispose()
fs1.Close()
fs1.Dispose()
End Sub


Now testing the above function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim j As Integer = 10
Dim i As Integer = 0
Dim k As Integer = j / i
Catch ex As Exception
Log(ex.Message)
End Try
End Sub


Hope this helps

ASP.NET AJAX and Calendar Control

Normally when we use calendar control in asp.net we see that calendar control occupies lot of space and selecting a date will cause a post back.

To avoid that problem we can use AJAX to select a date from calendar control with out any page post back and hiding the calendar control will save lot of space.

Now lets create a web application and add a script manager and a update panel control, now under update panel add text box, button and a calendar control as shown the below screen shot and make calendar control visible property to false

image

Now lets write logic in .cs file for that double click on Button and add following code

Calendar1.Visible = true;

and double click on calendar control and add following code

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    TextBox1.Text = Calendar1.SelectedDate.ToShortDateString();
    Calendar1.Visible = false;
}

 

Now we are ready with our code, lets run build and test.

 

image

 

Hope this helps.

 

ASP.NET MVC 1.0 is now Live

ASP.NET MVC 1.0 is now live and you can download that from http://go.microsoft.com/fwlink/?LinkId=144444 and you can access the documentation from http://go.microsoft.com/fwlink/?LinkId=145989.

Tags : ASP.NET MCV, .NET C#, ASP.NET 3.5

Thursday, March 19, 2009

Internet Explorer 8

Latest version of Microsoft Internet Explorer v8.0 is released today, and you can download this at following location.

http://www.microsoft.com/ie8

Some of the new features are:

Accelerator on the fly you can email, search with the selected content

Web Slices will fetch you latest information on your favourite sites which you browse frequently

You can easily find content on the page with Smart search and find on page tool bar.

Smart Page Filters  gives you more security from internet threats.

It also include lot more features I feel it will be great experience browsing with IE8.

Tags : Internet Explorer 8, IE 8, download Internet Explorer 8.  

Tuesday, March 17, 2009

System Driver Information : Windows Application

System.Management namespace has lot of classes which will help us in reading Operating system information, Driver information etc.

Lets build a sample application which retrieves what all drivers installed in system.

Create a Windows Application and add one Combo Box and One Button and One List box as shown in below screen shot

image

What we are going to do here is, first load all the driver names in the Combo Box and after selecting any driver from Combo Box and click on Show Driver Details button we will show all details regarding that driver.

For loading driver names into Combo Box initially we write following code in Form_Load event

image

Once a driver is selected and clicked on Show Driver Details button then we will show all the details of the driver in list box for that we write following code in Button1_Click event

image

Now we are ready with our code please build run and test

image

Hope this helps.

Tags: Driver Information, C#, .NET, Windows Application, System.Management

Monday, March 16, 2009

External Images in .rdlc Reports : ASP.NET

In this example I will explain how we can use external images in .rdlc reports.

Create a Web Application.

Right click on Project in Solution Explorer –> Add New Item – Select a .rdlc Report

image

Now Drag and Drop a image control in Header section in the Report.rdlc

Go to Report Menu in Visual Studio and Select Report Parameters –> and Add a parameter with name Path

image

Now we will set this path parameter to our image’s value property and provide the value at run time, select image and properties(F4) and set following values to Source and Value properties

Source = External and Value = !Path.value

image

Now create a Image folder in your solution and Add some images to that folder.

As I am only concentration on showing images on the report in this example I am not loading any data on to the report.

Now open Default.aspx page and add MicrosoftReportViewer Control and set MicrosoftReportViewer control source to Report1.rdlc

image

Now we are ready with design part,

One of the most important thing while displaying external images is to set EnableExternalImages perperty of MicrosoftReportViewer.LocalReport’s to True, this will allow us to show external images on the .rdlc reports

And also we need to set the Path parameter value so that image will be displayed on the report for that we need to all following code to Page_Load event

image 

Now we are ready with our code, just build and run and test

image

Hope this helps.

Tags : .rdlc report, external Images, ASP.NET