Click here to Skip to main content
15,922,015 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnsubmit_Click(object sender, EventArgs e)
        {

            String connectionString = "....;user id=sa;password=...;database=student";
            SqlConnection thisConnection = new SqlConnection(connectionString);
            SqlCommand sqlCommand = new SqlCommand();


            thisConnection.Open();

            String thisQuery = "INSERT INTO detail (ROLL_NO, S_NAME, AGE) VALUES ('" + txtroll.Text + "', '" + txtname.Text + "', '" + txtage.Text + "')";
            SqlCommand thisCommand = new SqlCommand(thisQuery, thisConnection);

            thisCommand.ExecuteNonQuery();
            thisConnection.Close();

            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                sqlCommand = sqlConnection.CreateCommand();
                SqlDataAdapter sda = new SqlDataAdapter(sqlCommand.CommandText, connectionString);
                SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                DataTable detail = new DataTable();
                sda.Fill(detail);
                BindingSource bSource = new BindingSource();
                bSource.DataSource = detail;
                dataGridView1.DataSource = bSource;
            }
        }
    }
}
Posted
Updated 30-Jan-12 1:00am
v2
Comments
OriginalGriff 30-Jan-12 6:56am    
What error? All you give is a code dump.
Use the "Improve question" widget to edit your question and provide better information.
Rajesh Anuhya 30-Jan-12 7:00am    
Where is the error??
--RA
priyanka999 30-Jan-12 7:03am    
values are storing in db but not in data gridview

You want to fetch data from database but did not define any commands.

After the below line
C#
sqlCommand = sqlConnection.CreateCommand();

Add another line like
C#
sqlCommand.CommandText = "Select ......";

(Write Select Query to fetch the data for gridview...)

Then modify the line
C#
SqlDataAdapter sda = new SqlDataAdapter(sqlCommand.CommandText, connectionString);

to
C#
SqlDataAdapter sda = new SqlDataAdapter(sqlCommand);


Add the line to bind the datatable to gridviw like

C#
dataGridView1.DataBind();
 
Share this answer
 
Comments
priyanka999 30-Jan-12 7:26am    
thank u very much dear
My pleasure, anytime.......
please also write down what error u getting exactly when u run this code

u forget the bind the grid i belevie

dataGridView1.DataBind();
 
Share this answer
 
v2
Comments
priyanka999 30-Jan-12 7:04am    
values are storing in db but not in data gridview
After Insert/Edit/delete operation re-bind your grid.
 
Share this answer
 
Comments
priyanka999 30-Jan-12 7:28am    
thank u sir
SqlDataAdapter sda = new SqlDataAdapter(sqlCommand.CommandText, thisConnection );

u hav to give the object of sqlconnection!!!
 
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