Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,

in my form application i have used sql database compact 3.5 with visual studio 2008.. at the inserting data in the table by query, it generates error at opening of the connection and also prompts that the datasource is not found..

what should i do?

Code :

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        static string ConStr = @"Data Source=C:\Users\Rohit\Desktop\WindowsFormsApplication1\MyDatabase#1.sdf";
        OdbcConnection con = new OdbcConnection(ConStr);

        public Form1()
        {
            InitializeComponent();
        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            string InsStr = "insert into student values ('"+textBox1.Text+"','"+textBox2.Text+"')";
            OdbcDataAdapter da = new OdbcDataAdapter(InsStr, con);
            DataTable dt = new DataTable("student");
            da.Fill(dt);
            MessageBox.Show("Record inserted succesfully...");
        }
    }
}
Posted
Updated 6-Aug-12 21:07pm
v3
Comments
Mehdi Gholam 7-Aug-12 2:48am    
Show your code.

For SQLCE you need to use the SqlCeXXX classes:
C#
SqlCeConnection con = new SqlCeConnection(ConStr);

private void addBtn_Click(object sender, EventArgs e) 
{ 
   string InsStr = "insert into student values '"+textBox1.Text+"','"+textBox2.Text+"')"; 
   SqlCeDataAdapter da = new SqlCeDataAdapter (InsStr, con); 
   DataTable dt = new DataTable("student"); 
   da.Fill(dt); 
   MessageBox.Show("Record inserted succesfully..."); 
}

Also you must Open() the connection before using it.

http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection%28v=vs.80%29.aspx[^]
 
Share this answer
 
Don't use an ODBC connection, use an SqlCeConnection instead:
C#
using (SqlCeConnection con = new SqlCeConnection(ConnectionString))
    {
    con.Open();
    using (SqlCeCommand cmd = new SqlCeCommand("SELECT iD, userName FROM MyTable", con))
        {
        SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
        da.Fill(dt);
        }
    }
You may need to download it first (http://www.microsoft.com/en-us/download/details.aspx?id=5783[^]) and add a reference to "System.Data.SqlCE.dll"
 
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