1
votes

I'm creating an application where simply i'm storing data in DB and getting data from DB. I'm Using SQLite for database.

MainWindows.xaml.cs File

using System.Data.SQLite;

namespace SQLiteSample
 {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
     public partial class MainWindow : Window
     {
         private SQLiteCommand sql_cmd;
        private DataSet DS = new DataSet();
        private DataTable DT = new DataTable();
        private SQLiteDataAdapter DB;
        string myConnectionString = "Data Source=C:\\Documents and Settings\\20024509\\My     Documents\\SQLite\\Enum.db";
        SQLiteConnection myConnection = new SQLiteConnection();

        public MainWindow()
        {
             InitializeComponent();

          }

        private void LoadData()
        {
            try
            {
                myConnection.ConnectionString = myConnectionString;
                myConnection.Open();
                sql_cmd = myConnection.CreateCommand();
                string CommandText = "select EmpID,EmpName from Employee_Info";
                DB = new SQLiteDataAdapter(CommandText, myConnection);
                DS.Reset();
                 DB.Fill(DS);
                DT = DS.Tables[0];
                // grid1.DataSource = DT;
                 myConnection.Close();
             }
             catch (Exception e)
             {
                 myConnection.Close();
             }
           }


         private void SaveData()
         {
             SQLiteConnection myConn = new SQLiteConnection(myConnectionString);
             string myInsertQuery = "INSERT INTO Employee_Info(EmpID,EmpName,EmpAge,EmpSalary) Values(503453535, 'DEVELOPMENT',32987654)";
             SQLiteCommand sqCommand = new SQLiteCommand(myInsertQuery);
              sqCommand.Connection = myConn;
              myConn.Open();
             try
             {
                 sqCommand.ExecuteNonQuery();
              }
              finally
             {
                  myConn.Close();
              }

             }

          private void  FromDB_Click(object sender, RoutedEventArgs e)
           {
             LoadData();
           }

           private void ToDB_Click(object sender, RoutedEventArgs e)
          {
              SaveData();
          }
          }
  }

I downloaded Binary for .net from "http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki"(Setups for 32-bit Windows (.NET Framework 4.0) ) and installed it.

Then added reference in my wpf application. But still it's giving Exception

"{"'The invocation of the constructor on type 'SQLiteSample.MainWindow' 
that matches the specified binding constraints threw an exception.' 
Line number '3' and line position '9'."}"
2
working on visual studio 2010. - Swati
Can somebody give me exact steps in which i can develop simple wpf app using SQLite? - Swati
Post the real exception message (when debugging with VS) - Fabske

2 Answers

5
votes

Make sure you compile your project as x86 instead of AnyCPU. The bitness issue for SQLite is so famous, that I often recommend beginners to use C#-sqlite (which works fine in all modes as it is written in pure C#),

http://code.google.com/p/csharp-sqlite/

2
votes

look at below link.

http://web.archive.org/web/20100208133236/http://www.mikeduncan.com/sqlite-on-dotnet-in-3-mins/

to bind data with wpf data grid convert datatable to dataview by using

dt.DefaultView;