Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            SqlCeConnection con = new SqlCeConnection("Data Source="
                + System.IO.Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "MyDB.sdf"));
            SqlCeDataAdapter sda = new SqlCeDataAdapter();
            SqlCeCommand cmd = con.CreateCommand();
            cmd.CommandText = "select * from nolon";
            sda.SelectCommand = cmd;
            SqlCeDataAdapter dad=new SqlCeDataAdapter(cmd);
            DataSet ds = new DataSet();
            dad.Fill(ds);

            if (ds.Tables[0].Rows.Count > 0)
            {
                
               
                dataGridView1.DataSource = ds;
            }
        }

datagridview did not show any data or even column name
Posted
Updated 6-Feb-15 2:32am
v2

Things to check:

- the copy of the database in your bin\debug folder definitely has some data in table nolon (or if you are running this as an Exe and not in Visual Studio then check the bin\release folder)

- the database does not require a user-id and password (if it does then include these in your connection string)

- put break point on if(ds.Tables[0].Rows.Count > 0) and see what the value of Rows.Count is.

- If it's > 0 then have a look at this MS forums post[^]

- If the program doesn't stop at the breakpoint then it's throwing an exception which you are not seeing - try wrapping the code in a try-catch block to capture and view the exception
 
Share this answer
 
Comments
Liju Sankar 6-Feb-15 9:27am    
+5, right to the point
Please try this connection string

SqlConnection conn = new SqlConnection
{
    ConnectionString = "Data Source=" +    System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\Database.sdf"
};


alternatively you are use Relative path as below

ConnectionString = "Data Source=|DataDirectory|\Database.sdf";


Modifying DataDirectory as executable's path:

string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
        string path = (System.IO.Path.GetDirectoryName(executable));
        AppDomain.CurrentDomain.SetData("DataDirectory", path);
 
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