Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,
i am new in windows mobile application using C#.
in my application i can read data from database SQLce.
but can't insert........
my code are bellow:-
C#
try
   {
    using (SqlCeConnection con = new SqlCeConnection(DBConn.connString))
     {
      con.Open();
      string SQL = "INSERT INTO USERINFO(UserID,Password,Name,Cell,Email,Status) VALUES ('"+txtUserID.Text+"','"+txtPassword.Text+"','"+txtName.Text+"','"+txtCellNo.Text+"','"+txtEmail.Text+"',0)";
     SqlCeCommand com = new SqlCeCommand(SQL, con);
     com.ExecuteNonQuery();
     MessageBox.Show("User Registration Successfull!");
     }
    }
   catch {
     MessageBox.Show("Registration Failed!");
   }

the code is execute.....
but data not inserted...
Details:----------
VS 2008
3.5 .NET framework
SQL CE
Windows XP SP2
Windows Mobile Application 5.0 Pocket PC

please help me.....
Posted
Comments
dmageiras 19-Oct-10 2:42am    
What is the exception message?
Syed Imran hossen 20-Oct-10 0:21am    
message show :- User Registration Successfull!
but data not show in the table....
OriginalGriff 20-Oct-10 3:29am    
"cant find data into table...."
How are you checking the table for data? What is your connection string?
Syed Imran hossen 20-Oct-10 4:39am    
Connection String are:---
public static string connString = "Data Source =" +(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\ASoft.sdf;");
Actually i want to know at the time of inserting data....its insert into My project database or Mobile emulator database........
if Mobile emulator database...then how can i find this...
OriginalGriff 20-Oct-10 5:46am    
"if Mobile emulator database...then how can i find this..."
Which DB did you think the Mobile Emulator was going to update? The one it knows about, or the one that could be on the moon for all it knows? It's a Pocket PC! It doesn't expect to be net connected all the time...

1 solution

Couple of things:
How do you know it did not work? Do you get the "Registration Failed!" message box? Or does it not appear in your database?

Several changes are needed here:
1) Don't do it this way. You are wide open to an SQL Injection attack, even by mistake. Instead, use SqlCeCommand.AddWithValue:
C#
using (SqlCeConnection con = new SqlCeConnection(DBConn.connString))
  {
  con.Open();
  string SQL = "INSERT INTO USERINFO (UserID,Password,Name,Cell,Email,Status)" +
               " VALUES (@ID, @PW, @NM, @CN, @EM, 0)";
  SqlCeCommand com = new SqlCeCommand(SQL, con);
  com.AddWithValue("@ID", txtUserID.Text);
  com.AddWithValue("@PW", txtPassword.Text);
  com.AddWithValue("@NM", txtName.Text);
  com.AddWithValue("@CN", txtCellNo.Text);
  com.AddWithValue("@EM", txtEmail.Text);
  com.ExecuteNonQuery();
  MessageBox.Show("User Registration Successfull!");
  }

2) Change your exception so that it catches and reports the problem:
C#
catch (Exception ex)
   {
   MessageBox.Show("Registration Failed!\n" + ex.ToString());
   }
That way at least it tells you you have a problem and give you information on what the problem is! (It is considered bad practice to use empty catch blocks)

The second change may give you enough info to solve the problem: It may be that the UserID already exists, or a similar problem.
 
Share this answer
 
Comments
Syed Imran hossen 19-Oct-10 23:51pm    
hi, i used your code.......
but same result...
mesg show "User Registration Successfull!"
but cant find data into table....
BR
Imran
rabmat 2-Feb-11 6:54am    
I am having the same problem.
there are no exceptions but the data is not insertes into table
please help.
Member 7906164 12-May-11 15:10pm    
i'm having the same problem...there are no exceptions but the data is not insertes into table ... i think is from connection.close() or how the application ends...please help

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