Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.Configuration;
using System.Data.SqlClient;

namespace login
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection con = new SqlConnection("data source=E:\\Jegan Works\\c sharp\\login\\login\\login.sdf;initial catalog=login;Integrated security=true");
        SqlCommand cmd;

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            cmd=new SqlCommand();
            cmd.Connection=con;
            cmd.CommandType=CommandType.Text ;
            cmd.CommandText="insert into login(username,password) values(@username,@password)";
            cmd.Parameters.AddWithValue("@username", username.Text);
            cmd.Parameters.AddWithValue("@password", password.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("record inserted successfully");

        }
    }
}
Posted
Updated 13-Dec-13 1:02am
v2
Comments
joginder-banger 13-Dec-13 9:04am    
what type of error getting???

Hello ,

You are using SqlServer Compact Edition (.sdf) ,so how could you expect to open the connection using SqlConnection .


to connection with Compact Edition Service you must add
using System.Data.SqlServerCe

this namespace .and use
SqlCeConnection ,SqlCeDataAdapter
instead of
SqlConnection,SqlCommand,and SqlDataReader

last but not the least in the webconfig file add this providername
providerName="Microsoft.SqlServerCe.Client.3.5"
if you are using SqlserverCe 3.5


SqlCeConnection con = new SqlCeConnection("data source=E:\\Jegan Works\\c sharp\\login\\login\\login.sdf;initial catalog=login;");
con.Open();
//other codes...
con.Close()


thanks
 
Share this answer
 
You don't open an SDF file with an SqlConnection: you need an SqlCeConnection instead.
Similarly, you will need SqlCeCommand, SqlCeReader and so forth as well.
 
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