backiyalakshmi K

backiyalakshmi K

  • NA
  • 6
  • 11.6k

I created form Used Following code but when am running the program am getting only a empty...whats the wrong with can anyone find ? do me a hel yaar....

Jul 6 2012 3:09 AM
I created form Used Following code but when am running the program am getting only a empty...whats the wrong with can anyone find ? do me a hel yaar....
 

my form:
                                         
my code:


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 datagrid
{
    public partial class Form1 : Form
    {
        String conn="Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Backiya Lakshmi\\Documents\\Visual Studio 2008\\listbox\\datagrid\\datagrid\\Database1.mdf;Integrated Security=True;User Instance=True";


        private void Form1_Load(object sender, System.EventArgs e)
        {
         BindGrid();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(conn);
            con.Open();
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
               String str ="insert into student(sid,sname,addr,age) VALUES ('" + dataGridView1.Rows[i].Cells["sid"].Value + "', '" + dataGridView1.Rows[i].Cells["sname"].Value + "'," + dataGridView1.Rows[i].Cells["addr"].Value + ",'" + dataGridView1.Rows[i].Cells["age"].Value + "')";
               SqlCommand com = new SqlCommand(str, con);
               com.ExecuteNonQuery();
               con.Close();
               MessageBox.Show("Values Saved");

            }
        }
        private void BindGrid()
        {
           SqlConnection con = new SqlConnection(conn);
           con.Open();
           String str = "select * from student";
           SqlCommand com = new SqlCommand(str, con);
           SqlDataAdapter Sqldataada = new SqlDataAdapter(com);
           DataSet ds = new DataSet();
           Sqldataada.Fill(ds, "student");
           dataGridView1.DataMember = "student";
           dataGridView1.DataSource = ds;
           con.Close();
        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

    }
}




Answers (7)