Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No 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 grid
{
    public partial class Form1 : Form
    {
        public DataTable dtRaw = new DataTable();
        public Form1()
        {
            InitializeComponent();
            //Fill.Data();
        }



        private void Form1_Load(object sender, EventArgs e)
        {


            SqlConnection conn = new SqlConnection("Server=KK-PC;Database=SampleDB;User id=sa;password=sa2008;");
            string sql = "SELECT *FROM Emp_Details";

            
            SqlCommand command = new SqlCommand(sql,conn);
            command.Connection = conn;
           
            DataTable data = new DataTable();
            SqlDataAdapter Adapter = new SqlDataAdapter(command);
                 Adapter.Fill(data);
               dataGridview.DataSource = data;
            
        }

    }

}




When i am trying to execute this code an "Error 1 The name 'dataGridview' does not exist in the current context C:\Users\KK\Documents\Visual Studio 2010\Projects\grid\grid\Form1.cs 37 16 grid" is occured.


Can any one help me to solve this issue.
Posted
Updated 11-Feb-14 20:23pm
v2
Comments
[no name] 12-Feb-14 0:26am    
I think you have removed the grid from form ..check it
Karthik Achari 12-Feb-14 0:48am    
I am new to C# can you suggest me necessary changes.
Thanks
midnight_ 12-Feb-14 1:34am    
Just to be mentioned:

SqlCommand command = new SqlCommand(sql,conn);
command.Connection = conn;

second line is not necessary - bcause you already attached the sql connection in the line above
santhu888 12-Feb-14 2:26am    
I think You didn't bind the gridview?? you should write "dataGridview.DataBind();" after "dataGridview.DataSource = data;" this sentense
Karthik Achari 12-Feb-14 2:33am    
After writing this line dataGridview.DataBind();


it shows error The name 'dataGridview' does not exist in the current context.

 
Share this answer
 
v2
1. You may have removed the grid or it wasn't there.
2. What about conn.open()? you should open the sql connection before fetching records;
 
Share this answer
 
Comments
Karthik Achari 12-Feb-14 2:05am    
grid is there but it shows error 'dataGridview' does not exist in the current context
Tesfamichael G. 12-Feb-14 6:11am    
Are you the name of the gridview is dataGridview
Karthik Achari 12-Feb-14 2:08am    
I am using .NET framework 2.0 & WindowsFormsApplication.

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