Tuesday, April 27, 2010

Context Menu Live Example

Context Menu in vb.net
----------------------
Public Class Form1
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
If e.Button = MouseButtons.Right Then
ContextMenuStrip1.Show(Me, New Point(e.X, e.Y))
End If
End Sub

Private Sub MoveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MoveToolStripMenuItem.Click
MsgBox("Item Moved")
End Sub
End Class

Display Javascript Alert in ASP.NET

Alert.aspx
----------

Public Sub MyMsgBox(ByVal Message As String)
System.Web.HttpContext.Current.Response.Write("[SCRIPT LANGUAGE=""JavaScript""]" & vbCrLf)
' System.Web.HttpContext.Current.Response.Write("alert(""Hello Sir"")" & vbCrLf)
System.Web.HttpContext.Current.Response.Write("alert(""" & message & """)" & vbCrLf)
System.Web.HttpContext.Current.Response.Write("[/" & "SCRIPT" & "]")
End Sub
---------------------------------------------------------------------------------
Private Sub Button1_click(ByVal o As Object, ByVal e As EventArgs)
MyMsgBox("hello")
End Sub
---------------------------------------------------------------------------------
[asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_click"/]

Monday, April 19, 2010

KyeDown N KeyPress Event in VB.NET

KyeDown N KeyPress Event in VB.NET
-----------------------------------

Private Sub TextBox1_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown
If e.KeyCode = Keys.Tab Then
If sender.Name = "TextBox1" Then
TextBox2.Focus()
e.Handled = True
ElseIf sender.Name = "TextBox2" Then
TextBox1.Focus()
e.Handled = True
End If
End If
End Sub

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
If sender.name = "TextBox1" Then
TextBox2.Focus()
e.Handled = True
ElseIf sender.name = "TextBox2" Then
TextBox1.Focus()
e.Handled = True
End If
End If
End Sub

Send Email in asp.net using Gmail SMTP with file attachment

web.config
-----------
[configuration]
[system.net]
[mailSettings]
[smtp from="xxxx@gmail.com"]
[network host="smtp.gmail.com" port="587" userName="xxxx@gmail.com" password="xxxx" defaultCredentials="false" /]
[/smtp]
[/mailSettings]
[/system.net]
[/configuration]


Default.aspx
------------

Private Sub SendMail(ByVal source As Object, ByVal e As EventArgs)
Dim FileName As String = Server.MapPath("./files/twitter_logo.png")
Dim smtp As New System.Net.Mail.SmtpClient()
Dim smail As New System.Net.Mail.MailMessage
smail.To.Add(New System.Net.Mail.MailAddress("xxxx@gmail.com"))
smail.From = New System.Net.Mail.MailAddress("xxxx@gmail.com", "Support System")
smail.Subject = "Alert by Support System"
smail.IsBodyHtml = "true"
smail.Body = "This is test mail by xxxx"
smtp.EnableSsl = True
smail.Attachments.Add(New System.Net.Mail.Attachment(FileName))
smtp.Send(smail)
Response.Write("Email Sent Successfully!")
End Sub

Additional Helpful Code
-----------------------

This is the C# code to send emails in HTML format
--------------------------------------------------
MailAddress SendFrom = new MailAddress(txtFrom.Text);
MailAddress SendTo = new MailAddress(txtTo.Text);
MailMessage MyMessage = new MailMessage(SendFrom, SendTo);

MyMessage.Subject = "Subject here";
MyMessage.IsBodyHtml = true;
MyMessage.Priority = MailPriority.Normal;
MyMessage.Body = "Some Html Body here";
SmtpClient emailClient = new SmtpClient();
emailClient.UseDefaultCredentials = true;
emailClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
emailClient.Host = "localhost";
emailClient.Send(MyMessage);

-------------------------------------------------------------------------

System.Web.Mail.MailMessage objMail = new System.Web.Mail.MailMessage();
objMail.BodyFormat = System.Web.Mail.MailFormat.Html;
objMail.From = "admin@fodra.org.in";
objMail.To = "";
objMail.Subject = "test";
objMail.Priority = System.Web.Mail.MailPriority.High;
string l_body;

l_body="Test";

objMail.Body = l_body;
System.Web.Mail.SmtpMail.Send(objMail);

Sunday, April 18, 2010

How to check EOF with SqlDataReader?

VB.NET
-------
Dim myconnection As SqlConnection
Dim mycmd As SqlCommand
Dim strSql As String
Dim myReader As SqlDataReader

myconnection = New SqlConnection("Server=localhost;uid=sa;password=;database=northwind;")
strSql = "Select count(*) from employees;Select * from employees"
mycmd = New SqlCommand(strSql, myconnection)
myconnection.Open()

Dim count As Integer = CInt(mycmd.ExecuteScalar())
myReader = mycmd.ExecuteReader(CommandBehavior.CloseConnection)
If count = 0 Then
Response.Write("No records found")
Else
myReader.NextResult()
While myReader.Read()
Response.Write(myReader("Employeeid").ToString() + "
")
End While
End If

-------------------------------------------------------------------------------

C#
----

SqlConnection myconnection ;
SqlCommand mycmd ;
string strSql ;
SqlDataReader myReader ;
myconnection = new SqlConnection("Server=localhost;uid=sa;password=;database=northwind;");
strSql = "Select count(*) from employees;Select * from employees";
mycmd = new SqlCommand(strSql, myconnection);
myconnection.Open();
int count=(int) mycmd.ExecuteScalar() ;
myReader = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
if (count==0 )
{
Response.Write("No records found");
}
else
{
myReader.NextResult ();
while(myReader.Read ())
{
Response.Write(myReader["Employeeid"].ToString () + "
");
}
}