March 19, 2016

Message Box in C#

We can call MessageBox.Show with just one argument. This is a string argument. An OK button is provided to dismiss the dialog.

MessageBox.Show("Welcome to askSudhir.");


Now let's add a second argument to our Show method call. The second argument is also a string. Sorry about the silly dialog text.

  MessageBox.Show("Welcome to askSudhir","Hi this is the header");


I like icons. Here we use an exclamation on our dialog. We specify MessageBoxIcon.Exclamation in the fourth method call. Again sorry for the message text.


March 18, 2016

International Scores

Email Validation in Windows Application C#.Net 

Today email is have becomes most important part of our life. So whenever we are developing any application in either in windows or web ad asking user for email id of user, so before saving the data we must validate that email id entered by user must be valid.

So in this article I will show you how you can validate the email id in a windows application using C#.Net  
 
 
private void txtUserName_Validating(object sender, CancelEventArgs e)
        {
            System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (txtUserName.Text.Length > 0)
            {
                if (!rEMail.IsMatch(txtUserName.Text))
                {
                    MessageBox.Show("E-Mail expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtUserName.SelectAll();
                    e.Cancel = true;
                }
            }
          
        }

 


Tommarow is the great match


March 15, 2016

India Started its world Cup T20 Journey today

T20 Cricket world cup 2016 (IND VS NZ)

India

Shikhar Dhawan, Rohit Sharma, Virat Kohli, Ajinkya Rahane, MS Dhoni, Yuvraj Singh, Suresh Raina, Ravindra Jadeja, Ravichandran Ashwin, Pawan Negi, Mohammed Shami, Ashish Nehra, Jasprit Bumrah, Harbhajan Singh, Hardik Pandya


New Zealand

Kane Williamson, Martin Guptill, Trent Boult, Nathan McCullum, Colin Munro, Luke Ronchi, Ish Sodhi, Ross Taylor, Corey Anderson, Grant Elliott, Mitchell McClenaghan, Adam Milne, Henry Nicholls, Mitchell Santner, Tim Southee


March 3, 2016

gdfg



DateTime dateAndTime = DateTime.Now;
Console.WriteLine(dateAndTime.ToString("dd/MM/yyyy")); // Will give you smth like 25/05/2011
  1. vc
  2. vcxvxcvxcv
  3. vxcvxvcxvxvcvcvcxv
  4. vxvxvcxv    

March 2, 2016

How to delete the top 30 rows from a table using Sql Server

For those wondering why you can't do DELETE TOP (30) FROM table ORDER BY column, the answer is

 "The rows referenced in the TOP expression used with INSERT, UPDATE, MERGE, or DELETE are not arranged in any order."

For a specific ordering criteria deleting from a CTE or similar table expression is the most efficient way.
;WITH CTE AS
(
SELECT TOP 30 *
FROM [My_Table]
ORDER BY a1
)
DELETE FROM CTE

Login Page in C#.Net Using Stored Procedure

I have often read forum posts asking how to create the login page using a Stored Procedure. So by considering the above requirement I have decided to write this article to help beginners, students and who wants to learn how to create a Login page using a Stored Procedure.

walk-through


Step -1 : Create a table


CREATE TABLE [dbo].[Login] ( [UserName] NVARCHAR (50) NULL, [Password] NVARCHAR (50) NULL, [SecurityQuestion] NVARCHAR (MAX) NULL, [SecurityAnswer] NVARCHAR (MAX) NULL );

Step -2 : Now let us create a Stored Procedure named sp_Validateuser as follows:

CREATE PROCEDURE sp_ValidateUser ( @User_name VARCHAR(50) ,@User_password VARCHAR(50) ) AS BEGIN SELECT UserName ,Password FROM Login_Details WHERE UserName=@User_name AND Password=@User_password END

Step -3 : Create a Login Form

Write the following code in the Button1_click function as:

if (txtUserName.Text == "") { MessageBox.Show("Please enter the user name", "OOPPS", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (txtPwd.Text == "") { MessageBox.Show("Please enter the password", "OOPPS", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { DataTable dt = new DataTable(); SqlDataAdapter adp = new SqlDataAdapter(); try { SqlCommand cmd = new SqlCommand("sp_ValidateUser", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@User_name ", txtUserName.Text.Trim()); cmd.Parameters.AddWithValue("@User_password ", txtPwd.Text.Trim()); adp.SelectCommand = cmd; adp.Fill(dt); cmd.Dispose(); if (dt.Rows.Count > 0) { // Perform the action if username and password is correct } else { MessageBox.Show("User Name or Password is wrong", "Please check", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); txtUserName.Text = ""; txtPwd.Text = ""; txtUserName.Focus(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { dt.Clear(); dt.Dispose(); adp.Dispose(); } }