Click here to Skip to main content
15,908,843 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
XML
App.config
<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="DBCS" connectionString="data source=.;Database=MnStores; Integrated Security = True"/>
  </connectionStrings>
</configuration>
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace MnStores
{
    public partial class Form1 : Form
    {
        public Form1()
        {   InitializeComponent();
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                string command = "select * from tbl_CategoryProducts";
                SqlCommand cmd = new SqlCommand(command, con);
                cmd.Connection = con;
                con.Open();
                dataGridView1.DataSource = cmd.ExecuteReader();
                con.Close();
            }
        }
Posted
Updated 18-Mar-15 17:58pm
v4
Comments
Thomas Daniels 18-Mar-15 14:38pm    
What error do you get?
Dave Kreskowiak 18-Mar-15 14:42pm    
Really!?!?!?! Think about this for a second. Put yourself in someones shoes. We can't see your screen, your hard drive, what you expect this code to do, ..., NOTHING. We're sitting here completely blind, reliant entirely on you to tell us about the problem and you haven't told us ANYTHING at all about it.

Now, how are we supposed to know what's wrong and what to look for?
[no name] 18-Mar-15 15:07pm    
He can't be held liable, see here :) http://www.skidmore.edu/~pdwyer/e/eoc/help_vampire.htm
Dave Kreskowiak 18-Mar-15 15:13pm    
Oh yes he can be held liable! >(

Participating in a forum environment carries the prerequisite that you can communicate with people and coherently convey a concept.
[no name] 18-Mar-15 15:13pm    
Did you read it? ;)

1 solution

Try Below
C#
DataTable _dt = new DataTable();
using (SqlConnection sqlcon = new SqlConnection(ConStr))
{
     string sqlstr = "select * from tbl_CategoryProducts";
     using (SqlDataAdapter adap = new SqlDataAdapter(sqlstr, sqlcon))
     {
          adap.Fill(_dt);
          cmbProvider.DataSource = _dt;
     }
}

AVK
 
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