Recent Posts

Thursday, July 31, 2008

XML with C#/.NET 2.0 and XPathNavigator

I had a bit tricky kind of xml shown as follows:

data
customer
id1/id
fullname John Smith /fullname
/customer
custaddress
streetname 100 George St /streetname
suburb Sydney /suburb
/custaddress
customer
id 2 /id
fullname Paul Smith /fullname
/customer
customer
id 3 /id
fullname Mary Smith /fullname
/customer
custaddress
streetname 100 George St /streetname
suburb Sydney /suburb
/custaddress
/data

(Note: I have removed "<" and ">" in the xml data because this blogger is giving me funny results while posting i don't want to go through it now)


Here customerAddress lies outside the customer tag and it exists only to those cutomers who has address.
Now to find which customer is related to what address it was a bit of a trick so I posted my query in msdn forum and found the following answer which worked quiet well for me

Guys facing the same or similar problems can solve this in the following way:

foreach (XPathNavigator customer in new XPathDocument(@"..\..\XMLFile1.xml").CreateNavigator().Select("data/Customer"))
{
Console.WriteLine("Id: {0}, full name: {1}", customer.SelectSingleNode("ID").Value, customer.SelectSingleNode("FullName").Value);
XPathNavigator address = customer.SelectSingleNode("following-sibling::*[1][self::CustAddress]");
if (address != null)
{
Console.WriteLine("Address: {0}, {1}", address.SelectSingleNode("StreetName").Value, address.SelectSingleNode("Suburb").Value);
}
else
{
Console.WriteLine("No address found.");
}
Console.WriteLine();
}


Read more!

Tuesday, July 22, 2008

Search a specific column in Sql Server Database

To find a specific column in a huge database system is pretty hectic and you miss out the column
at all sometimes and later decide to give it up or trapped in some hectic process. Therefore to get a better solution to this just try this query and it will show you the tables where to look at for that specific column:


SELECT name FROM sysobjects WHERE id IN ( SELECT id FROM syscolumns WHERE name = 'ColumnName' )


Read more!

Monday, July 21, 2008

How to screen scrap data from Asp.Net application

In the following example it gets all the data from the website and displays it in a text box.
You can later manupulate that data for your any other use.

First on any .aspx page just put one text box named "myWebText" and copy and paste the
following code in your code behind page:

Then run the application it will get all the data from the url to the text box for further use


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;


public partial class ScreenScrap : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
myWebText.Text = readHtmlPage("http://rprateek.blogspot.com");
}
private String readHtmlPage(string url)
{
String result;
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objResponse = objRequest.GetResponse();
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return result;
}
}


Read more!

Thursday, July 17, 2008

Keyboard Shortcuts for .Net Programming

Here are the keyboard shortcuts that can be used to quickly access or perform certain
tasks in .Net :

File
1. New Project (Ctrl+Shift+N )
2. Open Project/Solution (Ctrl+Shift+O)
3. Open File (Ctrl+O)
4. Save (Ctrl+S)
5. Save As (Ctrl+Shift+S)
6. Print (Ctrl+P)

Edit
7. Undo (Ctrl+Z)
8. Redo (Ctrl+Y)

9. Cut (Ctrl+X)
10. Copy (Ctrl+C)
11. Paste (Ctrl+V)
12. Cycle Clipboard Ring (Ctrl+Shift+V)
13. Select All (Ctrl+A)
14. Quick Find (Ctrl+F)

15. Quick Replace (Ctrl+H)
16. Find in Files (Ctrl+Shift+F)
17. Replace in Files (Ctrl+Shift+H)
18. Find Symbol (Alt+F12)

19. Format Document (Ctrl+E,D)
20. Format Selection (Ctrl+E,F)
21. Make Uppercase (Ctrl+Shift+U)
22. Make Lowercase (Ctrl+U)
23. Delete Horizontal White Space (Ctrl+E, \)
24. View White Space (Ctrl+E,S)
25. Word Wrap (Ctrl+E,W)
26. Incremental Search (Ctrl+I)
27. Comment Selection (Ctrl+K,C)
28. Uncomment Selection (Ctrl+K,U)

29. Toggle Bookmark (Ctrl+K,K)
30. Enable Bookmark (Ctrl+B,E)
31. Previous Bookmark (Ctrl+K,P)
32. Next Bookmark (Ctrl+K, N)
33. Clear Bookmarks (Ctrl+K,K)
34. Add Task List Shortcut (Ctrl+E,T)

35. Hide Selection (Ctrl+M, Ctrl+H)
36. Toggle Outlining Expansion (Ctrl+M,M)
37. Toggle All Outlining (Ctrl+M,L)
38. Stop Outlining (Ctrl+M,P)
39. Stop Hiding Current (Ctrl+M, Ctrl+U)

40. Generate Method Stub (Ctrl+K,M)
41. List Members (Ctrl+K,L)
42. Parameter Info (Ctrl+K,P)
43. Complete Word (Ctrl+K,W)
44. Insert Snippet (Ctrl+K,X)
45. Surround With (Ctrl+K,S)

View
46. Code (F7)
47. Designer (Shift+F7)
48. Server Explorer (Ctrl+W,L)
49. Class View (Ctrl+W,C)
50. Code Definition Window (Ctrl+W,D)
51. Object Browser (Ctrl+W,J)
52. Error List (Ctrl+W,E)
53. Output (Ctrl+W,O)
54. Properties Window (Ctrl+W,P)
55. Task List (Ctrl+W,T)
56. Toolbox (Ctrl+W,X)

57. Find Symbol Results (Ctrl+W,Q)
58. Bookmark Window (Ctrl+W,B)
59. Command Window (Ctrl+W,A)
60. Document Outline (Ctrl+W,U)
61. Resource View (Ctrl+W,R)
62. Macro Explorer (Alt+F8)
63. Web Browser (Ctrl+W,W)

64. Full Screen (Shift+Alt+Enter)
65. Pending Checkins (Ctrl+W,G)
66. Navigate Backward (Ctrl+-)
67. Navigate Forward (Ctrl+Shift+-)
68. Property Pages (Shift+F4)

Refactor
69. Rename (F2)
70. Extract Method (Ctrl+R,M)
71. Encapsulate Field (Ctrl+R,E)
72. Promote Local Variable to Parameter (Ctrl+R,P)
73. Remove Parameters (Ctrl+R,V)
74. Reorder parameters (Ctrl+R,O)

Website
75. Add New Item (Ctrl+Shift+A)
76. Add Existing Item (Shift+Alt+A)

Build
77. Build Solution (F6)
78. Build Web Site (Shift+F6)

Debug
79. Breakpoints (Ctrl+D,B)
80. Immediate (Ctrl+D,I)

81. Start Debugging (F5)
82. Start without Debugging (Ctrl+F5)
83. Exceptions (Ctrl+D,E)
84. Step Into (F11)
85. Step Over (F10)
86. Break at Function (Ctrl+D,N)
87. Delete All Breakpoints (Ctrl+Shift+F9)

Tools
88. Attach to Process (Ctrl+Alt+P)
89. Code Snippets Manager (Ctrl+K, Ctrl+B)

90. Run TemporaryMacro (Ctrl+Shift+P)
91. Record TemporaryMacro (Ctrl+Shift+R)

92. Macro Explorer (Alt+F8)
93. Macros IDE (Alt+F11)

Help
94. How Do I (Ctrl+F1,H)
95. Search (Ctrl+F1,S)
96. Contents (Ctrl+F1,C)
97. Index (Ctrl+F1,I)
98. Help Favourites (Ctrl+F1,F)
99. Dynamic Help (Ctrl+F1,D)
100. Index Results (Ctrl+F1,T)


Read more!

Monday, July 14, 2008

How can we use the same class obj for sending and receiving the data from one function in VB

1. Open a project in vb and in the form1 put two text boxes and one command button
name them txtOldDog,txtNewDog and cmdSend

2. Add one class named Dog.cls with the following code:

'local variable(s) to hold property value(s)
Private mvarDogName As String 'local copy
Private mvarDogColor As String 'local copy

Public Property Let DogColor(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.Barks = 5
mvarDogColor = vData
End Property


Public Property Get DogColor() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.Barks
DogColor = mvarDogColor
End Property



Public Property Let DogName(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.DogName = 5
mvarDogName = vData
End Property


Public Property Get DogName() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.DogName
DogName = mvarDogName
End Property


3. Add a module name modPublic.bas and add the following code:

Option Explicit


Public Sub CheckIfDogReturns(ByRef obj As Dog, ByRef msg As String)
msg = "The name of Dog is "
With obj
msg = msg + .DogName
msg = msg + " and its color is "
msg = msg + .DogColor

.DogName = "NewName Pony"
.DogColor = "New color yello"
End With

End Sub

4. Now in your form1 double click cmdSend button and add the following code:

Private Sub cmdSend_Click()
Dim objDog As New Dog
Dim showMsg As String


objDog.DogName = "Tony"
objDog.DogColor = "Red"

Call CheckIfDogReturns(objDog, showMsg)
Me.txtOldDog = showMsg
Me.txtNewDog = objDog.DogName & " " & objDog.DogColor


End Sub


5. Now you can see that the objDog is carrying Tony and red and getting back Pony and yello so that
means in your application you can carry the object to data logic and update the database and
come back with the updated record on the same object and display it on your User interface.


Read more!

Sunday, July 13, 2008

Simple way of Error Handling in ASP.Net

To Redirect your page to more appropriate error page for the Client

Go to Web.config file and add redirection location in customErrors tag
For example:

"<"customErrors defaultRedirect="http://hostName/applicationName/errorStatus.htm" mode="On" "/>"
"
Although you can reference a default error page in the value of the defaultRedirect
attribute in the section, you can also specify a particular page to
redirect to based on the HTTP error code that is raised.
The child element allows for this option.

For example:

"<"customErrors defaultRedirect="http://hostName/applicationName/errorStatus.htm" mode="On" "/>"
"<"error statusCode="404" redirect="filenotfound.htm" "/>"


"<"customErrors : section includes a mode attribute that is set to On by default.

The mode attribute includes the following settings:
• On: Unhandled exceptions redirect the user to the specified defaultRedirect page. This mode is used mainly in production.

• Off: Users receive the exception information and are not redirected to the defaultRedirect page. This mode is used mainly in development.
• RemoteOnly: Only users who access the site on the local computer (by using localhost) receive the exception information. All other users are redirected to the defaultRedirect page. This mode is used mainly for debugging.

"


Read more!

Block user from right clicking your web site

Just copy and paste the java script code given below to block the users from right
clicking on your web site.

function right(e) {
if (navigator.appName == 'Netscape' &&
(e.which == 3 e.which == 2))
return false;
else if (navigator.appName == 'Microsoft Internet Explorer' &&
(event.button == 2 event.button == 3)) {
alert("Please don't right click , Thanks Prateek!!");
return false;
}
return true;
}

document.onmousedown=right;
document.onmouseup=right;
if (document.layers) window.captureEvents(Event.MOUSEDOWN);
if (document.layers) window.captureEvents(Event.MOUSEUP);
window.onmousedown=right;
window.onmouseup=right;
// End -->


Read more!

Removing unwanted programs that start when starting up windows xp (avoid startup errors and slow pc startup)

Most of the people might be facing your computer running slow and wondering how to fix windows xp and speed up windows xp then this can be one of the solution.

Instructions for removing unwanted programs that execute when starting up Windows and avoid pc strartup errors:

1) Look in the "Programs" group for any folders labeled "Startup". These programs will run when you log in. Remove any unwanted shortcuts.

2) Start regedit and open the following key:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

and look in the panel on the right for a list of programs that run on startup. Delete any that you don't want to run WARNING: Make sure that you know what you are deleting. Some programs may be needed.


3) Start regedit and open the following key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

and look in the panel on the right for a list of programs that run on startup. Delete any that you don't want to run WARNING: Make sure that you know what you are deleting. Some programs may be needed.


Read more!

Thursday, July 10, 2008

Send Mail through .Net Application

Get free code to send mail through .net application, provides all the required functionality to send email from vb net through smtp. It can be modified into c# and can be used for all your .net desktop and asp .net applications.


' First import system web mail
Imports System.Web.Mail

Private Function SendMail()

Try

Dim oMsg As MailMessage = New MailMessage

' TODO: Replace with sender e-mail address.
oMsg.From = "fromtest@mail.com"
' TODO: Replace with recipient e-mail address.
oMsg.To = "Tomailto:Totest@mail.com"
oMsg.Subject = "Test Mail"

' Send email in html format (comment this line to send plain text email).
oMsg.BodyFormat = MailFormat.Html

'HTML Body (remove HTML tags for plain text email).
oMsg.Body = "Hello World!"

' Add an attachment and send email
' TODO: Replace with path to attachment.
Dim sFile As String = "C:\Hello.txt"
Dim oAttch As MailAttachment = New MailAttachment(sFile, MailEncoding.Base64)

oMsg.Attachments.Add(oAttch)

' TODO: Replace with the name of your remote SMTP server.
SmtpMail.SmtpServer = "your SMTP Server" 'put your smtp server name here

SmtpMail.Send(oMsg)

oMsg = Nothing
oAttch = Nothing

MessageBox.Show("Email sent successfully")

Catch ex As Exception
Dim strMsg As String
strMsg = (("The following exception occurred: " + ex.ToString()))
'check the InnerException
While Not (ex.InnerException Is Nothing)
strMsg = strMsg + vbCrLf + ("--------------------------------")
strMsg = strMsg + vbCrLf + (("The following InnerException reported: " + ex.InnerException.ToString()))
ex = ex.InnerException
End While
MessageBox.Show(strMsg)
End Try

End Function


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call SendMail1()
End Sub


Read more!

Mulit Threaded Application from VB 6.0

I have designed the small application in VB 6.0 that runs in a mulit threaded environment. This application will be very helpful while designing the application with COM objects.



Below I have provided the link to download the Step by Step process of threading from Vb 6.0 application:
http://prateek.regmi.googlepages.com/ThreadManager.doc

If any comments or add - Ins please comment on this post. Every comment is appreciated.


Read more!

Tuesday, July 1, 2008

Download Sql Server Interview Questions

Facing an interview is a bit out of normal. We can't explain things in words, but we know how to do it. Also the interview questions can be trikier but only little bit of knowledge prior to the interview might help.

I found these interview questions and answers might be helpful to you?

Download the questions from the following link provided:
http://prateek.regmi.googlepages.com/sqlinterview.pdf


Read more!

Removing viruses boot.vbs or virusremoval.vbs or wscript.exe

If you see boot.vbs or virusremoval.vbs or wscript.exe viruses showing up in your computer or poping up on start up then do the following.

You can remove boot.vbs or virusremoval.vbs by doing the following:


1. Go to the System32 folder C:\WINDOWS\system32 and search for the file 'wscript.exe'.

2. Then delete the file wscript.exe. If you can't delete it, just use unlocker from http://ccollomb.free.fr/unlocker/

Note:
Remember don't delete any other files it might be your windows files that might lead you to other windows related problems.


Read more!