Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
2.59/5 (3 votes)
See more:
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 EnableorDisableApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            //FillData();
        }
 
 
        SqlConnection con = new SqlConnection("Server=KK-PC;Database=SampleDB;User id=sa;password=sa2008;");
 
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                con.Open();
            }
            catch (SystemException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
 
        {
 
            SqlConnection con = new SqlConnection("Server=KK-PC;Initial Catalog=SampleDB;User id=sa;password=sa2008;");
            using (SqlCommand cmd = new SqlCommand("Insert into Emp_Details (Emp_ID,Name,Education) values (" + textBox2.Text + ",'" + textBox3.Text + "','" + textBox1.Text + "')", con))
            {
                cmd.ExecuteNonQuery();
            }
 
 
        }
 
 
      void FillData()
        {
            // 1
            // Open connection
            using (SqlConnection c = new SqlConnection("Server=KK-PC;Initial Catalog = SampleDB;User id=sa;Password=sa2008;"))
            {
                c.Open();
 
                // Create new DataAdapter
                using (SqlDataAdapter a = new SqlDataAdapter(
                    "SELECT * FROM Emp_Details", c))
                {
                    // 3
                    // Use DataAdapter to fill DataTable
                    DataTable t = new DataTable();
                    a.Fill(t);
                    // 4
                    // Render data onto the screen
                    dataGridView1.DataSource = t;
                }
            }
        }
 
        private void label2_Click(object sender, EventArgs e)
        {
 
        }
 
        private void label1_Click(object sender, EventArgs e)
        {
 
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
 
        }
    }
}



Data is displaying in grid view but when i enter the particulars it's not inserting in to DB & grid view.

Can any one help me tosolve this problem.
Posted
Updated 11-Feb-14 23:29pm
v2

There is a lot wrong with the code I'm afraid.

1. Get the SQL stuff out of the GUI code. Read this article[^] eg.
2) You're code is vulnerable for SQL injection. See here[^]
3) If you want to update your grid, you need to call FillData again after an insert. The grid's DataSource object will not update itself.
4) Remove the Form1, Button1, etc... instances and replace them with some useful names. (also change the corresponding event handler names.)

hope this helps.
 
Share this answer
 
try this


C#
private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("User ID=sa;password=pass@word1;Initial Catalog=Test;Data Source=.;");
            con.Open();
            using (SqlCommand cmd = new SqlCommand("Insert into Emp_Details (Emp_ID,Name,Education) values (" + textBox1.Text + ",'" + textBox2.Text + "','" + textBox3.Text + "')", con))
            {
                cmd.ExecuteNonQuery();
            }
            con.Close();

            FillData();
        }

        void FillData()
        {
            using (SqlConnection c = new SqlConnection("User ID=sa;password=pass@word1;Initial Catalog=Test;Data Source=.;"))
            {
                c.Open();
                using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM Emp_Details", c))
                {
                    DataTable t = new DataTable();
                    a.Fill(t);
                    dataGridView1.DataSource = t;
                }
            }
        }
 
Share this answer
 
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 MyDBApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }
        SqlConnection con = new SqlConnection("Server=127.0.0.1;Database=SampleDB;User id=sa;password=sa2008;");

        
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                FillData();
            }
            catch (SystemException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            using (SqlCommand cmd = new SqlCommand("Insert into Emp_Details (Emp_ID,Name,Education) values (" + textBox2.Text + ",'" + textBox3.Text + "','" + textBox1.Text + "')", con))
            {
                cmd.ExecuteNonQuery();
            }
            FillData();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    
    
     void FillData()
        {
            
                using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM Emp_Details", con))
                {
                    DataTable t = new DataTable();
                    a.Fill(t);
                    dataGridView1.DataSource = t;
                }
            
        }
    }
}
 
Share this answer
 

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