none
Updating .mdf table using Visual studio but when I quit its gone

    Question

  • Hello 

    I am using 

      private void BtnUpdate_Click(object sender, EventArgs e)
            {
    
                try
                {
                    SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True;");
                    con.Open();
    
    
                    SqlCommand cmd1 = new SqlCommand("UPDATE Login SET Password='" + TxtNewPassword.Text + "'", con);
                    cmd1.ExecuteNonQuery();
    
                }
                catch
    
                { }
           

    There is only one row in database 

    CREATE TABLE [dbo].[Login] (
        [Id]        INT          IDENTITY (1, 1) NOT NULL,
        [UserName]  VARCHAR (50) NULL,
        [Password]  VARCHAR (50) NULL,
           PRIMARY KEY CLUSTERED ([Id] ASC)
    );
    

    Before and After changing value i.e password I am able to login but after closing the windows form 
    I check the database its not update and also cant login with the changed value 
    so what should i do 
    need help with that, I know I am doing a small mistake but dont know where need help 


    Tahir Ayoub

    Sunday, June 23, 2013 10:53 PM

Answers

All replies

  • I suspect the problem is your connection string, and by not putting anything in the catch, you have no idea what error you might be getting. Take a look at this link for proper connection strings: http://www.connectionstrings.com/

    EDIT: You should also go ahead and catch that exception an display it:

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com


    Monday, June 24, 2013 12:30 AM
  • Hi Tahir;

    When working with a local database file in your project you need to configure it so that each time you restart the program you do not overwrite the file you just modified it in the last run. Please see this article on hoe to configure for the behavior you want.

    Debugging with local database file.

       


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, June 24, 2013 12:36 AM
  • catch(Exception ex)

                {
                    MessageBox.Show(ex.Message);
                }

    I have used this too But there is no error Its not giving me any exception

    and secondly I read data using the same connection string: if there is an error there how can i read the data from the same table 

    Its Updating the value but not in Table after i close the app 

    It should update at the same time when I edit it


    Tahir Ayoub

    Monday, June 24, 2013 12:37 AM
  • Hello

    I got it that two copies of database are there one for temporary changes and one is the database1.mdf in my case

    but how to commit changes in database1.mdf table when i exit the app 

    My connection string is 

     <connectionStrings>
            <add name="WindowsFormsApplication1.Properties.Settings.Database1ConnectionString"
                connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"
                providerName="System.Data.SqlClient" />
        </connectionStrings>


    Tahir Ayoub

    Monday, June 24, 2013 12:53 AM
  • What is the setting of the "Copy To Output Directory" property, that was mentioned in the link that Fernando provided?

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Monday, June 24, 2013 1:06 AM
  • Hi Tahir;

    To your statement, "but how to commit changes in database1.mdf table when i exit the app ",  you don't it always uses the database in the bin directory in the project.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, June 24, 2013 1:11 AM
  • This is the overview of my project and this is the first project I am making 
    and How to see the 
    "Copy To Output Directory" property

    and there is no BIN directory in my project is it good or bad?


    Tahir Ayoub


    • Edited by Tahir Ayoub Monday, June 24, 2013 1:16 AM forget something
    Monday, June 24, 2013 1:15 AM

  • Tahir Ayoub

    Monday, June 24, 2013 1:17 AM
  • In the Solution Explorer window right click on the Database1.mdf and select Properties. In the properties window what is the property "Copy To Output Directory" set too?

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, June 24, 2013 1:20 AM
  • "Its Copy Always" 

    I changed the abc password to 2233


    Tahir Ayoub

    Monday, June 24, 2013 1:28 AM
  • this 2233 password is working before i close the app and login after closing it its not working


    Tahir Ayoub


    • Edited by Tahir Ayoub Monday, June 24, 2013 1:33 AM forget something
    Monday, June 24, 2013 1:29 AM
  • This option "Its Copy Always" will ALWAYS copy Database1.mdf to the bin directory, which is the database which the program is writing to and therefore overwriting any updates from the previous execution of your program. Change it to Do not copy and the database in the bin directory will NOT be overwritten. 

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, June 24, 2013 1:38 AM
  • Now its giving 
    An attempt to attach an auto-named database for file C:\Users\Tahir\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

    this error is now coming in exception when i change it to DO NOT COPY


    Tahir Ayoub

    Monday, June 24, 2013 1:42 AM
  • Is this the right way to close and app form by clicking the X sign on the right upper corner ; does this have to do any thing with the updating query 

    Tahir Ayoub

    Monday, June 24, 2013 1:45 AM
  • You were right (Bonnie Berent DeWitt ) there was problem with connection string 
    Now I use the below code got from internet 
    http://www.youtube.com/watch?v=yh0A9rGXp1M
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.Sql;
    using System.Data.SqlClient;
    
    namespace Insert_Delete_Update
    {
        public partial class Form1 : Form
        {
    
            SqlConnection cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\tahir\documents\visual studio 2012\Projects\Insert Delete Update\Insert Delete Update\Database1.mdf;Integrated Security=True");
            SqlCommand cmd = new SqlCommand();
            SqlDataReader dr;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    cn.Open();
                    cmd.CommandText = "INSERT INTO Login (id,name) values ('" + textBox1.Text + "','" + textBox2.Text + "' )";
                    cmd.ExecuteNonQuery();
                    cmd.Clone();
                    MessageBox.Show("success");
                }
                catch { }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                cmd.Connection = cn;
            }
        }
    }
    


    Tahir Ayoub

    Monday, June 24, 2013 4:01 AM