Click here to Skip to main content
15,911,142 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
here is my code.please send me your solutions as soon as possible.This is my second time asking this problem.plz help me.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Ruhunu_spices_PVT_LTD
{
    public partial class emp_data : Form
    {
        SqlConnection conn1 = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\Sachintha\\Desktop\\Ruhunu spices(PVT)LTD\\Ruhunu spices(PVT)LTD\\Rspices.mdf;Integrated Security=True;User Instance=True");
        public emp_data()
        {
            InitializeComponent();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.Hide();

            employee_view f6 = new employee_view();
            f6.ShowDialog();

        }
        //Add btn code
        private void button2_Click(object sender, EventArgs e)
        {
            String g;
            String s;
            if (radioButton1.Checked)
            {
                g = "male";
            }
            else
            {
                g = "female";
            }
            if (radioButton3.Checked)
            {
                s = "married";
            }
            else
            {
                s = "single";
            }

            SqlCommand comm1 = new SqlCommand("INSERT INTO empdataT(empId,empName,Bday,gender,spouse,startdate,address,contactNo,Email)Values(@textBox1,@textBox2,@textBox3,'"+g+"','"+s+"','" + dateTimePicker1.Value.Date + "',@textBox4,@textBox5,@textBox6)", conn1);

            comm1.Parameters.Add("@textBox1", textBox1.Text);
          
            comm1.Parameters.Add("@textBox2", textBox2.Text);
            comm1.Parameters.Add("@textBox3", textBox3.Text);
            comm1.Parameters.Add("@textBox4", textBox4.Text);
            comm1.Parameters.Add("@textBox5", textBox5.Text);
            comm1.Parameters.Add("@textBox6", textBox6.Text);




            conn1.Open();
            comm1.ExecuteNonQuery();
            MessageBox.Show("one row inserted", "Insert Data", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);

            textBox1.Clear();
            textBox2.Clear();
            
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
            dateTimePicker1.Value = DateTime.Today;
           


            conn1.Close();


        }
        //update btn code
        private void button3_Click(object sender, EventArgs e)
        {
            String g;
            String s;
            if (radioButton1.Checked)
            {
                g = "male";
            }
            else
            {
                g = "female";
            }
            if (radioButton3.Checked)
            {
                s = "married";
            }
            else
            {
                s = "single";
            }
            SqlCommand comm1 = new SqlCommand("UPDATE empdataT SET empName=@textBox2,Bday=@textBox3 ,gender='"+g+"',spouse='"+s+"',startdate= '" + dateTimePicker1.Value.Date + "',address=@textBox4,contactNo=@textBox5,Email=@textBox6 WHERE empId=@textBox1", conn1);
            comm1.Parameters.Add("@textBox1", textBox1.Text);
           
            comm1.Parameters.Add("@textBox2", textBox2.Text);
            comm1.Parameters.Add("@textBox3", textBox3.Text);
            comm1.Parameters.Add("@textBox4", textBox4.Text);
            comm1.Parameters.Add("@textBox5", textBox5.Text);
            comm1.Parameters.Add("@textBox6", textBox6.Text);



            conn1.Open();
            comm1.ExecuteNonQuery();
            MessageBox.Show("one row updated");
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
            
            dateTimePicker1.Value = DateTime.Today;
            
            conn1.Close();


        }
        //delete btn code
        private void button4_Click(object sender, EventArgs e)
        {
            SqlCommand comm1 = new SqlCommand("DELETE FROM empdataT WHERE empId=@textBox1", conn1);
            comm1.Parameters.Add("@textBox1", textBox1.Text);
            
            comm1.Parameters.Add("@textBox2", textBox2.Text);
            comm1.Parameters.Add("@textBox3", textBox3.Text);
            comm1.Parameters.Add("@textBox4", textBox4.Text);
            comm1.Parameters.Add("@textBox5", textBox5.Text);
            comm1.Parameters.Add("@textBox6", textBox6.Text);
            conn1.Open();
            comm1.ExecuteNonQuery();
            MessageBox.Show("one row deleted");
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
            
            dateTimePicker1.Value = DateTime.Today;
           

            conn1.Close();

        }
        //search btn code
        private void button1_Click(object sender, EventArgs e)
        {
            String commandstring;
            commandstring = string.Format("SELECT * FROM empdataT  WHERE empId='{0}'", textBox1.Text.Trim());
            SqlDataReader dr1 = null;
            SqlCommand com2 = new SqlCommand(commandstring, conn1);
            try
            {
                conn1.Open();
            }
            catch
            {
                MessageBox.Show("error in seaching", "message", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            }
            dr1 = com2.ExecuteReader();


            if (dr1.Read())
            {
                textBox2.Text = dr1[1].ToString();

                dateTimePicker1.Value = (DateTime)dr1[3];
                textBox3.Text = dr1[2].ToString();
                textBox4.Text = dr1[4].ToString();
                textBox5.Text = dr1[5].ToString();
                textBox6.Text = dr1[6].ToString();
            }
            else
            {
                MessageBox.Show("Record can not found");
            }
            dr1.Close();
            conn1.Close();

        }
        //clear btn code
        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
           
            dateTimePicker1.Value = DateTime.Today;
           


        }
    }
}
Posted
Updated 5-Oct-13 5:33am
v3
Comments
Alexander Dymshyts 24-Sep-13 4:57am    
Did you try anything?
MCY 25-Sep-13 9:17am    
you can try ado.net http://msdn.microsoft.com/en-us/library/dw70f090.aspx#_SqlClient
Pradeep Shukla 5-Oct-13 11:34am    
You have not mentioned what is the problem you are facing in the CRUD process.. can you be specific...
Where is the problem?
sachinthasri 6-Oct-13 9:45am    
I don't know how to get(search)/delete the checked value of the radio button at the database.I found a way to add checked value of radio button to the database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900