Click here to Skip to main content
15,947,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi guys,i Added a "service-based database" into my project with this name : "database2.mdf" and connected to this DB from Server Explorer and i made a Table with this name:"UserTbl" and this table has 2x column with this names : "Id,Name".
Next step i added a Ado.net EF6 and i selected "database2.mdf" as my ado.net connection.
now i have a model,into my UserTbl.cs in my model i write this method to add a string into my Database,that's my code:

public partial class UserTbl
    {
        Database2Entities1 db = new Database2Entities1();
        public int Id { get; set; }
        public string Name { get; set; }


        public bool Add(UserTbl entity, bool autosave = true)
        {
            db.UserTbls.Add(entity);
            if (autosave)
            {
                return Convert.ToBoolean(db.SaveChanges());

            }
            else
                return false;
        }
    }


in my windows form i just have a textbox and a button and that's my code behind of my windows form:
private void btn_Click(object sender, RoutedEventArgs e)
        {

            var bluserInfo = new UserTbl();
            var DataUser = new UserTbl()
            {
                Id = 10,
                Name = txtbox.Text

            };
            bluserInfo.Add(DataUser);
        }


i made a BreakPoint and check my codes,everything gone right,but my database2.mdf did't Receive my data, i don't know what's my problem...

TY for your attention and your time <3

What I have tried:

i made a BreakPoint and check my codes,everything gone right,but i don't know what's my problem...
Posted
Updated 19-Apr-23 5:32am
Comments
Richard MacCutchan 19-Apr-23 11:18am    
Make sure that you are examining the correct mdf file. EF (and other systems) sometimes copy the source file from one directory to another, so you may be looking at the source of the copy.
Max Speed 2022 19-Apr-23 16:38pm    
thank you ,that's right <3

1 solution

Sooo many problems in such a small amount of code. Where do I start? Well, why would you want to create two instances of the UserTbl class when you need to add just a single user?

First, your UserTbl class needs an overhaul. You're thinking in terms of tables when you should be thinking in terms of a single user. The class should describe a User record:
C#
public partial class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

You'll have to update your Database2Entities1 class to change UserTbls to Users and use the User type instead of what you have now.

Next, you can have a class that manages the users, something like this:
C#
public static class UserManager
{
    public static void AddUser(User user)       // The "autosave" crap isn't needed
    {
        using Database2Entities1 db = new();

        db.Users.Add(user);
        db.SaveChanges();
    }
}

Now, when you want to add a user, you just do this:
C#
private void btn_Click(object sender, RoutedEventArgs e)
{
    User newUser = new()
    {
        // DO NOT SET THE ID VALUE YOURSELF! This should be managed by the database!
        Name = txtbox.Text
    };
    
    UserManager.Add(newUser);
}

Note: There are still plenty of problems with this code! First instance, you can add the same name to the Users table without any error at all! You're also not validating the content of the textbox before you use it.
 
Share this answer
 
Comments
Max Speed 2022 19-Apr-23 14:42pm    
@dave kreskowiak
Thank you a lot for your helping and your timing :).
your method working like mine but when i see my table in my database,there is nothing... :(
My Id Column is Identity increment +1,i used same method in my another program and that's working with SQL database,but now i wanna use a local database and i don't know what's wrong...
anyway thank you a lot <3
Dave Kreskowiak 19-Apr-23 14:47pm    
As Richard already said in his comment, you're looking in the wrong copy of the database. The one you see in Visual Studio is the original design copy. When you build and run the project, the database file is copied to the bin\Debug or bin\Release folder. THAT is the copy your code is using and writing to, NOT the one you see in Visual Studio.
Max Speed 2022 19-Apr-23 16:38pm    
yeah that was wrong path :(, thank you
idiot visual studio AI :D

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