views:

323

answers:

3

Hello everyone i am new in wpf. so i have got problems with it. if you help me, i will be so pleased. thanks everyone in advance.

My problem is, can not insert into name inside database in wpf. how can i fix it? my codes as follows;

private void button1_Click(object sender, RoutedEventArgs e)
{       
    try
    {
     string SqlString = "Insert Into UserInformation(name) Values (?)";
     using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Cell.mdb;Persist Security Info=True"))
     {
      using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
      {
       cmd.CommandType = CommandType.Text;
       cmd.Parameters.AddWithValue("name", textBox1.Text);
       conn.Open();
       cmd.ExecuteNonQuery();

      } 
     }
    }
    catch (Exception ex)
    {  }
}
A: 

Try to use cmd.Parameters.AddWithValue("@name", textBox1.Text);

CSharpAtl
According to this : http://www.microsoft.com/poland/developer/net/programowanie/dostep_do_baz.mspxParameter name should stay without @. (I know it's Polish, but the code doesn't change)
kubal5003
I dont know who down voted me...but here is a link to an MSDN article:http://msdn.microsoft.com/en-us/library/dw70f090.aspx
CSharpAtl
@CSharpAtl good suggestion +1
Sander Rijken
A: 

The problem here seams to be the parameter. In the command text you don't specify its name, but when you add it, it has a name. Change command text to :

Insert Into UserInformation(name) Values (@name)

In line: cmd.Parameters.AddWithValue("name", textBox1.Text);

the parameter name should stay without @ .

kubal5003
You down voted me....and you are wrong.....here is an MSDB article.....http://msdn.microsoft.com/en-us/library/dw70f090.aspx
CSharpAtl
that is an MSDN article
CSharpAtl
@name is SQL Server syntax, '?' should be used with OLE DB, that's described in the MSDN page you linked to
Sander Rijken
With OleDB, '?' should be used in the CommandText, and @paramname in the .AddWithValue
Sander Rijken
A: 

Is it opening the right database file? As people have suggested in the comments, set Visual Studio to break on first-chance exceptions, or remove the exception handling. The database file needs to exist, and you need the appropriate JET drivers.

I've tried your code and it works without any problems here (in a WPF application or otherwise). Using named parameters instead of a question mark was a good suggestion, but it doesn't appear to be the problem. (I have Office 2007 and .NET 3.5 SP1 installed, but I doubt that matters).

Are you using a WPF browser application (cbap)? Because you won't be able to access the local file system (and thus the database) if you are. WPF browser applications run with isolated permissions, much like a Silverlight browser application.

Thorarin
hi thoarin, i have office 2007 and net 3.5 sp1. i removed try cacth blocks. but it still doesnt work. i dont know why it behaves like this. and i have another window, i can make select query work in same application.
neki
@neki: without exception handling, you should be able to see an error message if something is going wrong, unless the code isn't executed at all. Try verifying execution by setting a breakpoint on the `string SqlString` line and running in debug mode. Step through execution with F10 after the debugger breaks. If it doesn't break, your `Click` handler isn't attached to the button.
Thorarin