locked
how is convert to Entity FrameWork RRS feed

  • Question

  •  Please help convert this code to the entity framework.

    help me

    class AccessLevel
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DBLogin;Integrated Security=True");
            public static string UserName = string.Empty;
            public static string FirstName = string.Empty;

            public static bool frmMain_ChangeBackColor = false;
            public static bool frmMain_AddUser = false;
            public static bool frmMain_Calculation = false;

            public void SetAccess(string userName)
            {
                SqlCommand sqlcmdSet = new SqlCommand();
                sqlcmdSet.CommandText = "select * from tblAccesslevel where User_Name=@User_Name";
                sqlcmdSet.Parameters.Add("@User_Name", SqlDbType.NVarChar).Value = userName;
                sqlcmdSet.Connection = con;
                SqlDataAdapter data = new SqlDataAdapter(sqlcmdSet);
                DataTable dt = new DataTable();
                data.Fill(dt );

                frmMain_AddUser =(bool ) dt.Rows[0][1];
                frmMain_Calculation = (bool)dt.Rows[0][3];
                frmMain_ChangeBackColor = (bool)dt.Rows[0][2];
            }

    • Edited by vahid2231 Thursday, July 26, 2018 3:25 PM
    Thursday, July 26, 2018 2:39 PM

Answers

  •   

    You can do something like this...

    The search function helps you get the required data from the database by passing in a parament(firstname). Now based on the data returned, you use it to fill your datagridview and if the data doesnt match any record, a message is shown back to the user.

    private void btn_Click(object sender, EventArgs e) { string search = txtSearch.Text.Trim(); List<user> user= Search(search); dataGridView1.DataSource = (user.Count == 0 || search == string.Empty) ?

    MessageBox.Show("User Not Found!") : dataGridView1.DataSource = user.ToList(); } private List<user> Search(string firstname) { using (DbModel db = new DbModel()) { return db.users.Where(u => u.firstname == firstname).ToList(); } }



    Hope this helps..

    Kind Regards,

    GNG

          
    Thursday, July 26, 2018 3:57 PM
  • Hi vahid2231,

    Here is about Entity framework, you can take a look:

    https://www.c-sharpcorner.com/article/entity-framework-introduction-using-c-sharp-part-one/

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by vahid2231 Friday, July 27, 2018 2:33 PM
    Friday, July 27, 2018 1:51 AM

All replies

  • Hi,

    Please, can you elaborate more on your question?. I do not seem to understand your challenge.

    Kind Regards,

    GNG

    Thursday, July 26, 2018 2:50 PM
  • Please help convert this code to the entity framework. help me
    • Edited by vahid2231 Thursday, July 26, 2018 3:26 PM
    Thursday, July 26, 2018 3:25 PM
  • Are you trying to implement a search function(by username) in WinForms using EntityFramework?

    Kind Regards,

    GNG

    Thursday, July 26, 2018 3:32 PM
  • yes

    and permision  user for login 

    Thursday, July 26, 2018 3:42 PM
  •   

    You can do something like this...

    The search function helps you get the required data from the database by passing in a parament(firstname). Now based on the data returned, you use it to fill your datagridview and if the data doesnt match any record, a message is shown back to the user.

    private void btn_Click(object sender, EventArgs e) { string search = txtSearch.Text.Trim(); List<user> user= Search(search); dataGridView1.DataSource = (user.Count == 0 || search == string.Empty) ?

    MessageBox.Show("User Not Found!") : dataGridView1.DataSource = user.ToList(); } private List<user> Search(string firstname) { using (DbModel db = new DbModel()) { return db.users.Where(u => u.firstname == firstname).ToList(); } }



    Hope this helps..

    Kind Regards,

    GNG

          
    Thursday, July 26, 2018 3:57 PM
  • Thankful
    Thursday, July 26, 2018 5:09 PM
  • Please, remember to mark this as an answer if this solved your challenge. So that others could learn from it.

    Appreciate.

    Kind Regards,

    GNG

    Thursday, July 26, 2018 6:49 PM
  • Hi vahid2231,

    Here is about Entity framework, you can take a look:

    https://www.c-sharpcorner.com/article/entity-framework-introduction-using-c-sharp-part-one/

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by vahid2231 Friday, July 27, 2018 2:33 PM
    Friday, July 27, 2018 1:51 AM