ODD One out Quiz

Wednesday, 7 May 2014

Login Database Connectivity

Login Database Connectivity
1)      Add three labels and add two textboxes in windows form
2)      Name the first label as “Enter username” and second label as “Enter password”
3)      Add two buttons. First button named as “Add record ” and second button named as “Connect Database”.



4)      Now double click on connect database button and it will go to source code file.
5)      Now then add two namespaces on the top of code.
using System.Data.Sql;
using System.Data.SqlClient;

6)  Then write the code in Connect database button click

7)  try
8)              {
9)   
10)                 conn.ConnectionString = "Data Source=(localdb)\\Projects;Initial Catalog=Login;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False";
11)                 conn.Open();
12)  
13)                 if (conn.State == ConnectionState.Open)
14)                 {
15)  
16)                     label3.Text = label3.Text + "\r\n" + "SQL Server Connection Successful.";
17)                     
18)                 }
19)                 else
20)                 {
21)                     label3.Text = label3.Text + "\r\n" + "Error in SQL Connection!";
22)  
23)                 }
24)             }
25)             catch (Exception te)
26)             {
27)                 label3.Text = label3.Text + "\r\n" + te.ToString();
28)  
29)  
30)             }



After this you can run the program and check whether sql server connection is successful created or not.
Then click connect database button.
See the picture

To add or Insert a record in database. Double click on add record button and write the code

try
            {

                string sSQL = "INSERT INTO record (username,password)VALUES (@user,@pass)";
                SqlCommand objCmd = new SqlCommand(sSQL, conn);


              
                objCmd.Parameters.Add("@user", SqlDbType.VarChar, 50);
                objCmd.Parameters.Add("@pass", SqlDbType.VarChar, 50);

               
                objCmd.Parameters["@user"].Value = textBox1.Text;
                objCmd.Parameters["@pass"].Value = textBox2.Text;
               
                objCmd.ExecuteNonQuery();

                              MessageBox.Show("Record Added");

               
            }

            catch (Exception ex)
            {
                label3.Text = label3.Text + "\r\n" + ex.ToString();
            }

Then add datagridview control in windows form...



Add one button named as “show record” and then double click on it and write the following code.

try
            {

                string sSQL = "select username,password from record";

                SqlCommand objCmd = new SqlCommand(sSQL, conn);

                SqlDataAdapter dadapt = new SqlDataAdapter(objCmd);

                DataSet daset = new DataSet();

                dadapt.Fill(daset, "record");
                dataGridView1.DataSource = daset.Tables["record"];
                dataGridView1.Refresh();
               
            }

            catch (Exception ex)
            {
                label3.Text = label3.Text + "\r\n" + ex.ToString();
            }

Now run the program..
Step1..Click on connect database button
Step2: then click on show record button..

Outputwindow:






Now add one button named as search record and double click on it and write the following code:

try
            {

                string sSQL = "select * from record where username= @user";


                SqlCommand objCmd = new SqlCommand(sSQL, conn);

                objCmd.Parameters.Add("@user", SqlDbType.VarChar,50);
                objCmd.Parameters["@user"].Value = textBox1.Text;



                SqlDataAdapter dadapt = new SqlDataAdapter(objCmd);
                DataSet daset = new DataSet();
                dadapt.Fill(daset, "record");

                if (daset.Tables[0].Rows.Count > 0)
                {
                    dataGridView1.DataSource = daset.Tables["record"];
                    dataGridView1.Refresh();
                    MessageBox.Show("Record Found");
               

                }
                else
                {
                    MessageBox.Show("Record Not Found");
                }


            }

            catch (Exception ex)
            {
                label3.Text = label3.Text + "\r\n" + ex.ToString();
            }

 And then add one button named as update record and double click on it and write the following code:

try
            {

                string sSQL = "update record set password=@pass where username=@user";

                SqlCommand objCmd = new SqlCommand(sSQL, conn);
               
                objCmd.Parameters.Add("@user", SqlDbType.VarChar, 50);
               
                objCmd.Parameters.Add("@pass", SqlDbType.VarChar, 50);
               

                objCmd.Parameters["@user"].Value = textBox1.Text;
                objCmd.Parameters["@pass"].Value = textBox2.Text;
              

                objCmd.ExecuteNonQuery();

                MessageBox.Show("Record Updated");
               
            }
            catch (Exception ex)
            {
                label3.Text = label3.Text + "\r\n" + ex.ToString();
            }

And then add one button named as delete record and and double click on it and write the following code:

try
            {

                string sSQL = "delete from record where username=@user";

                SqlCommand objCmd = new SqlCommand(sSQL, conn);
                objCmd.Parameters.Add("@user", SqlDbType.VarChar, 50);

                objCmd.Parameters["@user"].Value = textBox1.Text;


                objCmd.ExecuteNonQuery();

                MessageBox.Show("Record Deleted");


            }
            catch (Exception ex)
            {
                label3.Text = label3.Text + "\r\n" + ex.ToString();
            }

Final Output:-




No comments:

Post a Comment