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" />
   >