Click here to Skip to main content
15,914,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to modify my code to display inserted data in data gridview...
My code is
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace employee
{
    public partial class details : Form
    {
        public details()
        {
            InitializeComponent();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string empid = textBox1.Text;
            string name = textBox2.Text;
            string designation = textBox3.Text;
            string salary = textBox6.Text;
            string value = "";
            bool isChecked = radioButton1.Checked;
            if (isChecked)
                value = radioButton1.Text;
            else
                value = radioButton2.Text;
            string address = textBox4.Text;
            string pincode = textBox5.Text;
            string prifix = textBox8.Text;
            string phone = textBox7.Text;
            string email = textBox9.Text;
            if (empid == "" || name == "" || designation == "" || salary == "" || address == "" || pincode == "" || prifix == "" || phone == "" || email == "")
            {
                MessageBox.Show("Please ensure all fields are entered", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                System.Data.SqlClient.SqlConnection sqlcommand1 = new System.Data.SqlClient.SqlConnection("Persist Security Info=True;User ID=*******;Password=******;Initial Catalog=ubsemp;Server=****");

                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "IF NOT EXISTS(SELECT * FROM UEMPLOYEE WHERE EMPLOYEE_ID=@EMPLOYEE_ID) INSERT INTO UEMPLOYEE(EMPLOYEE_ID,NAME,DESIGNATTION,GENDER,ADDRESS,PINCODE,SALARY,PHPREFIX,PHONE,EMAIL) VALUES(@EMPLOYEE_ID,@NAME,@DESIGNATTION,@GENDER,@ADDRESS,@PINCODE,@SALARY,@PHPREFIX,@PHONE,@EMAIL)";
                cmd.Parameters.AddWithValue("@EMPLOYEE_ID", empid);
                cmd.Parameters.AddWithValue("@NAME", name);
                cmd.Parameters.AddWithValue("@DESIGNATTION", designation);
                cmd.Parameters.AddWithValue("@SALARY", salary);
                cmd.Parameters.AddWithValue("GENDER", value);
                cmd.Parameters.AddWithValue("@ADDRESS", address);
                cmd.Parameters.AddWithValue("PINCODE", pincode);
                cmd.Parameters.AddWithValue("PHPREFIX", prifix);
                cmd.Parameters.AddWithValue("PHONE", phone);
                cmd.Parameters.AddWithValue("@EMAIL", email);
                cmd.Connection = sqlcommand1;
                sqlcommand1.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("successfully inserted");
            }
        }
    }
}
Posted
Updated 18-Jun-14 23:00pm
v2
Comments
Thanks7872 19-Jun-14 5:01am    
Then after inserting data into database,call function that will fetch data from db into datatable and bind GridView with that data. I don't see any code where you are binding GridView.

create method like below and call it after you insert complete
C#
private void Loademployees()
{
    try
    {
        using(SqlConnection connection = new SqlConnection(connetionString))
        {
            connection.Open();
            SqlDataAdapter adapter = new SqlDataAdapter("select * from UEMPLOYEE", connection);
            adapter.Fill(ds);
            connection.Close();
            dataGridView1.DataSource = ds.Tables[0];
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show (ex.ToString());
    }
    
}
 
Share this answer
 
Hello,
After successful insertion , you have to fetch data from database . and then show it to girdview .
Try this way.
SqlConnection con=new SqlConnection (connectionstring);
con.Open();
SqlDataAdapter  da=new SqlDataAdapter("select * from UEMPLOYEE",con);
DataSet ds=new DataSet();
da.Fill(ds);
con.Close();
datagridview.DataSource=ds.Table[0]; //now set gridview DataSource 


thanks
 
Share this answer
 
v2
Comments
chaitanya556 19-Jun-14 5:22am    
what is Table[0] here?
Animesh Datta 19-Jun-14 9:47am    
here Tables[0] means , the first table of the dataset .

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