Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
please help me with the following code
VB
Dim conn As SqlCeConnection = Nothing

     
      Try
          conn = New SqlCeConnection("Data Source = MyDb.sdf; Password =''")
          conn.Open()



          Dim cmd As SqlCeCommand = conn.CreateCommand()
          
          ''cmd.CommandText = "INSERT INTO Customer_Details ([Cust_Order], [Customer_Name]) Values('1', '" + txtCustomer.Text + "')"
          cmd.CommandText = "INSERT INTO Customer_Details (Cust_Order, Customer_Name) Values(1, '" + txtCustomer.Text + "')"
          cmd.ExecuteNonQuery()
         
          cmd.CommandText = "SELECT * FROM Customer_Details"
         
      Catch
          MessageBox.Show("Database not connected")
      End Try



I'm able to connect with database but not able to populate table with values.. Let me know where I am going wrong.Thank you in advance
Posted
Comments
Sergey Vaselenko 8-Jan-14 6:22am    
You have no the execute command for the SELECT statement like cmd.ExecuteReader in the code sample.
Do you have it in your code?
shwetap07 8-Jan-14 6:56am    
Yup.. I Tried.. but there is no effect... :(

1 solution

Exactly why that doesn't work is going to be down to you, for the most part.
The important thing is not to use anonymous catches - you are throwing away information you need in order to solve this.
Instead of writing:
VB
Catch
    MessageBox.Show("Database not connected")
End Try
Try this:
VB
Catch ex As Exception
    MessageBox.Show(String.Format("Unable to insert values:\n{0}", ex.ToString()))
End Try
Which will at least give you an overview of what is going wrong.

My guess? Either your table doesn't have those columns, or it has a value of 1, or it's an Identity field.
 
Share this answer
 
Comments
shwetap07 8-Jan-14 6:51am    
@OriginalGriff I tried as you said but catch block is not executing.
Try
conn = New SqlCeConnection("Data Source = MyDb.sdf; Password =''")
conn.Open()



Dim cmd As SqlCeCommand = conn.CreateCommand()
MessageBox.Show("Command Created")
''cmd.CommandText = "INSERT INTO Customer_Details ([Cust_Order], [Customer_Name]) Values('1', '" + txtCustomer.Text + "')"
cmd.CommandText = "INSERT INTO Customer_Details (Cust_Order, Customer_Name) Values(1, '" + txtCustomer.Text + "')"
cmd.ExecuteNonQuery()
MessageBox.Show("Commmand Executed SuccessFully")
cmd.CommandText = "SELECT * FROM Customer_Details"
cmd.ExecuteReader()
'MessageBox.Show("Table Refreshed")
Catch ex As Exception
MessageBox.Show(String.Format("Unable to insert values:\n{0}", ex.ToString()))
End Try
OriginalGriff 8-Jan-14 7:08am    
If the catch block isn't executing, then the problem is quite likely twofold: and you extra note kinda confirms that.
No catch means no error - the operation has succeeded - so the lack of updates has two reasons.
The first is that executing cmd.ExecuteReader doesn't do anything useful on it's own - it returns a SqlCeReader which can be used to look at the rows returned, but it doesn't change any data showing on your form. (You would need to use the reader it returned to do that).
The other is that it is quite likely that because you have added the file as an item, you are overwriting it into your debug folder every time. Try something for me: create a new folder of your root called "TestDB" and manually copy your database into that. Then change your connection string to explicitly refer to the newly copied file:
conn = New SqlCeConnection("Data Source = C:\MyDb.sdf; Password =''")
Now when you run your app, it will always be using the existing copy and updates your application makes should be preserved from run to run.
shwetap07 8-Jan-14 6:58am    
Note: I have created database programmatically.... i.e via "Add New Items" in project.Is that a correct way ??
shwetap07 8-Jan-14 9:27am    
@OriginalGriff thank you very much :) with your help was able to do.
OriginalGriff 8-Jan-14 9:33am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900