Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using a C# Windows form application
My program store data in local database (.sdf). I can't get data from the database.
No errors but no retrieving data from database
this my code this is my first local database program
what is the wrong ?
Is there any other way to do this ?

table name = tbl_user
column name = u_name

C#
SqlCeConnection con = new SqlCeConnection("Data Source=H:\\application\\database.sdf");
            try
            {
                SqlCeCommand com = new SqlCeCommand("SELECT * FROM tbl_user WHERE u_id=@u_id", con);
                com.Parameters.AddWithValue("u_id", 1);
                SqlCeDataReader reader = com.ExecuteReader();
                string name = reader["u_name"].ToString();
                MessageBox.Show(name);
                con.Close();
            }
            catch (SqlCeException)
            {
                MessageBox.Show("Error");
               
            }



Thank in Advance!
Posted
Updated 22-Oct-13 21:17pm
v2
Comments
sp_suresh 23-Oct-13 2:32am    
what is .sdf?which type database?
Manoj Chamikara 23-Oct-13 2:35am    
@spshewale local database SQL server compact edition , file extension (.sdf)

You should start by replacing
C#
SqlCeConnection con = new SqlCeConnection("Data Source="H:\application\database.sdf"");

with
C#
SqlCeConnection con = new SqlCeConnection("Data Source=""H:\\application\\database.sdf""");

and see if it changes something.

Quotes in a string have to be doubled or escaped ; and it is the same with backslashes.

You can find the reference here:
String literals[^]

[edit]

You do not seem to ever open your connection ; that can be the problem, also.
A better way to handle that would certainly be:
C#
using (SqlCeConnection con = new SqlCeConnection("Data Source=""H:\\application\\database.sdf""")) {
   try {
      con.Open();
      // ... rest of the code ...
   }
   catch (SqlCeException e) {
      MessageBox.Show(e.Message); // Would be an improvement than just the word 'Error'
   }
}


[/edit]
 
Share this answer
 
v2
Comments
Manoj Chamikara 23-Oct-13 3:13am    
Thank you phil I change the code but still the same thing
No error
No data retrieving
And update the question
phil.o 23-Oct-13 3:22am    
Please see my updated answer ; you do not seem to ever open the connection.
Manoj Chamikara 23-Oct-13 3:36am    
Thank you very much phil
I put a message box to find where the error message box 1 to 5 popup but message box 6 not pop up


MessageBox.Show("1");
using (SqlCeConnection con = new SqlCeConnection("Data Source=""H:\\application\\database.sdf"""))
try
{
MessageBox.Show("2");
con.Open();
MessageBox.Show("3");
SqlCeCommand com = new SqlCeCommand("SELECT * FROM tbl_user WHERE u_id=@u_id", con);
MessageBox.Show("4");
com.Parameters.AddWithValue("u_id", 1);
SqlCeDataReader reader = com.ExecuteReader();
MessageBox.Show("5");
string name = reader["u_name"].ToString();
MessageBox.Show("6");
MessageBox.Show(name);
con.Close();
}
catch (SqlCeException e)
{
MessageBox.Show(e.Message)
}
phil.o 23-Oct-13 3:39am    
Please give up with these message boxes everywhere ; better put a breakpoint at the beginning of the method and hit F5 to enter debug mode ; the F11 to execute line by line, and check for your variables each time.
Manoj Chamikara 23-Oct-13 3:45am    
Thank you phil appreciate your help
it seeems that reader is not fetching data
you should do like
string apppath = Application.StartupPath.ToString() + "\\Northwind.sdf";
SqlConnection con = new SqlConnection("Data Source=KTPL11\\SQLEXP_KTPL11;Initial Catalog=Trial;Integrated Security=true");

try
{
con.Open();
SqlCommand com = new SqlCommand("SELECT * FROM tbl_user WHERE u_id=1", con);
com.CommandType = CommandType.Text;
com.Parameters.AddWithValue("u_id", 1);
using (SqlDataReader reader = com.ExecuteReader())
{
string name = "";
if (reader.Read())
{
name = reader["u_name"].ToString();
}
MessageBox.Show(name);
}
con.Close();
}
catch (SqlException ex)
{
MessageBox.Show("Error");

}
 
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