Answered data is not inserted into database

  • 19 iulie 2012 15:47
     
      Are cod

    Hello,

    I am using linq to sql with ado.net entity framework in winforms application.

    I created a database. 

    Added ado.net entity framework.

    on button click I am saving data

    private void button1_Click(object sender, EventArgs e)       
    {           
    try           
    {               
    using (Database1Entities myEntities = new Database1Entities())               
    {                   
    demouser myUser = new demouser();                   
    myUser.Name = textBox1.Text;                   
    myUser.NickName = textBox2.Text;                   
    myEntities.AddTodemousers(myUser);                   
    myEntities.SaveChanges();                   
    MessageBox.Show("Data saved");               
    }           
    }           

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

    In the above code I am writing code to save data. But when I see table data in server explorer then I don;t see any data.

    However on other button which retrieves data is working fine. (If I am entering data manually in table and click another button then it is working)

    private void button2_Click(object sender, EventArgs e)
            {
                try
                {
                    using (Database1Entities myEntities = new Database1Entities())
                    {
                        var allUsers = from user in myEntities.demousers
                                       select user;
                        dataGridView1.DataSource = allUsers;
                        //dataGridView1.;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
    
            }

    Please tell me what is the issue? I've debugged the application but nothing wrong found.

Toate mesajele

  • 20 iulie 2012 05:13
    Moderator
     
     Răspuns

    Hi sophia_asp,

    I assume that the database is included in the project. If so, then you should check the database file in the output folder. For more details, see http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/ad837d14-31ea-445f-b830-1a8682b69e68.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

  • 20 iulie 2012 16:32
     
     

    Hi Bob,

    yes that link points out the problem I am facing (I am using ado.net entity framework.)
    I have taken step as mentioned there but it is giving error now.

    Can you please tell me any other solution?

    Many thanks

  • 23 iulie 2012 07:41
    Moderator
     
     

    Hi sophia_asp,

    What's the error message?

    If the database have been included in the project, you should set the build action to content, Copy to Output Directory to copy always. When you submit any changes to the database, the changes will applied in the database located in the output folder( bin\debug or bin\ release folder by default). Have you check the database in the output folder?

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

  • 25 iulie 2012 10:57
     
     

    Hi bob,

    I want that data get inserted into database which I have created not in the output folder. How it can be done?

    Many thanks

  • 26 iulie 2012 03:11
    Moderator
     
     

    I want that data get inserted into database which I have created not in the output folder. How it can be done?

    You need to change the AttachDbFilename to the path of the database file. The path could be the full path of the database file or the relative path of the database file, such as ..\..\[database file].

    However, I doesn't recommend this. By default, the database file included in the project will be added into the project folder. After you build and distributed to users, the application may no longer work since it can't find the database file.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us