Sir,
I am having some problem regarding database in SQL Server.
My project is desktop application with login method….
My program is written in C# executes normally as I want with no errors in my queries…
But data is not stored back to database which the program inserts or updates… My database is created by visual studio 2010 as a separate mdf file in data folder…
So kindly please help me in this. Here is a sample code :
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection cn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database_File.mdf;Integrated Security=True;User Instance=True"))
{
using (SqlCommand cmd = new SqlCommand("select * from table_users where user_name='" + txtBoxUserName.Text + "' and user_password='" + txtBoxUserPass.Text + "' ",cn))
{
cn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr["user_name"].ToString() == txtBoxUserName.Text && dr["user_password"].ToString() == txtBoxUserPass.Text)
{
...
...
try
{
using (SqlCommand cmd1 = new SqlCommand("insert into table_user(id,user_name,user_status) values(" + key + "," + active_user_name + "',"+ user_status + ")",cn))
{
cmd1.ExecuteNonQuery();
Thread form_name = new Thread(new ThreadStart(thread_Proc_form_name));
form_name.Start();
}
}
catch { throw;}
}
}
}
}
}
catch { throw;}
}
The program runs as i want but when i look into the database table no changes are saved. For eg : when I login then no information of login is shown in the database on next run..
Same is the case for register… every time i run the app I have to register again and again. I don’t know where is the error. Please help me sir where i am going wrong….
Thanks & Regards
Saurabh Mahajan
This snippet suggests to me that you’ve got a
Database_File.mdfin your solution in Visual Studio:Each time you build your project,
Database_File.mdfwill be copied to the output directory, overwriting whatever file was there previously.This behaviour is by design.
Note that user instances are deprecated, you should consider attaching your
Database_File.mdfto your SQL Express instance for the duration of development or using something like SQL Server LocalDB.