November 21, 2009

Bind data from Sql Server to dataGridView

How To: Bind data from Sql Server to dataGridView

DataGridView displays the contents of a data source. It is a control in Windows Forms. It uses a grid format. There are many ways to improve its default configuration. We show some improvements to usability and appearance.

DataTable GetData(){            DataTable dt = new DataTable();            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))            {                con.Open();                using (SqlCommand cmd = new SqlCommand("select Name from SampleTable ", con))                {                    SqlDataAdapter adpt = new SqlDataAdapter(cmd);                    adpt.Fill(dt);                }            }            return dt;        }

Call this function anywhere

Related Post :



November 18, 2009

Office 2007 Ribbon Control

This Control, will put a Office 2007 Ribbon to your application. No OCX, No DLL, No Office 2007 Required.


Download 

Contact me if link is broken

Regards,
Sudhir Singh
 

November 11, 2009

How to change column width in DataGridView?

 dataGridView1.Columns[0].Width = 400;

Creating a login screen in C# using Stored Procedures

Table

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

Stored Procedure

CREATE PROCEDURE  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

Login Code

 private void button1_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text == "")
            {
                MessageBox.Show("Please provide User Name", "OOPPS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (txtPwd.Text == "")
            {
                MessageBox.Show("Please provide 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)
                    {
                        MainForm frm2 = new MainForm();
                        frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
                        frm2.Show();
                        this.Hide();
                    }

                    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();
                }  

             
            }
        }

Connection String

  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["nameofconnectionstring"].ConnectionString);


App.Config



 
 
 
          providerName="System.Data.SqlClient" />
   >
 



October 15, 2009

Login in Asp.net using Three Tier Architecture

This article explains how to implement a login form using ASP.NET C# in a three-tier architecture.

In the previous article I have already explained how to create a new website in ASP.NET C#. You can check this Link.

Lets continue with the same example.

Basically 3-Tier architecture contains 3 layers
1.    Application Layer or Presentation Layer 
2.    Business Access Layer(BAL) or Business Logic Layer(BLL) 
3.    Data Access Layer(DAL)

October 9, 2009

Close Parent Form from Child Form

How to Close Parent Form from Child Form in Windows Forms

C#
private void btnOpenForm_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
frm2.Show();
this.Hide();
}

private void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
}

October 6, 2009

Creating a new Asp.Net Website

Your First Website

This article explains you the step by step procedure to how to get start to create a asp.net website in Visual Studio.


The Steps are :

1. File Menu -> New -> Web Site
 
The New Web Site dialog box will open

Select ASP.NET Empty Web Site

Give some name to the Website and Click on the OK Button.





September 28, 2009

Welcome to my blog



:D

Hello friends,

My name is Sudhir, and this is the first time I am writing a blog. Truly saying this is the first ever post I am writing. I know you are having certain questions about this blog, you are excited to know about the person behind this blog and stuffs going to be there in this blog.

Well frankly speaking these type of questions actually arises in my mind also, when  I am going through a new blog or an article. Nothing wrong about it.

So, first let me introduce myself.

Name :Sudhir Singh
(Yeah, You know it I know)
DOB : 6 October
Hobbies : Drawing, Writing
Interest : , Playing Cricket and Computer Games
Education :  Pursuing BCA
(Future :  Will be doing MCA if got the chance to do so).

You know what, a million of thoughts are roaming around in once mind, Which he himself is not aware of, until and unless he starts writing, this is the reason why I thought of writing this blog. Although it took a lot for me to write this first post itself.  The reason behind this was that, I myself was not sure, what to write ?

Now coming to the second question. I can just say that it is going to be something about technical stuffs. As I said I am doing BCA, what I feel is that some of my classmates are struggling in completing some programming stuffs, I though of writing this blog because my past experiences in coding and the computer courses I attended makes me feel like I can shot out their problems. 

So that's all about what I tried to do. Further you can read my posts and if you feel any difficulty you can mail me at jonydexter@gmail.com.

Finally guys it's Sudhir saying good bye for today.