none
C# How can i differentiate datatable per users RRS feed

  • Question

  • 0
    <button aria-label="down vote" aria-pressed="false" class="js-vote-down-btn grid--cell s-btn s-btn__unset c-pointer" data-selected-classes="fc-theme-primary" style="margin:2px;box-sizing:inherit;font:inherit;padding:0px;border-width:initial;border-style:none;border-color:initial;border-radius:3px;background-image:none;background-background-size:initial;background-repeat:initial;background-attachment:initial;background-origin:initial;background-clip:initial;outline:none;box-shadow:none;" title="This question does not show any research effort; it is unclear or not useful"><svg aria-hidden="true" class="svg-icon m0 iconArrowDownLg" height="36" viewBox="0 0 36 36" width="36"><path d="M2 10h32L18 26 2 10z"></path></svg></button><button aria-label="favorite" aria-pressed="false" class="js-favorite-btn s-btn s-btn__unset c-pointer py8 js-gps-track" data-gps-track="post.click({ item: 1, priv: -1, post_type: 1 })" data-selected-classes="fc-yellow-600" style="margin:0px;box-sizing:inherit;font:inherit;padding:0px;border-width:initial;border-style:none;border-color:initial;border-radius:3px;background-image:none;background-background-size:initial;background-repeat:initial;background-attachment:initial;background-origin:initial;background-clip:initial;outline:none;box-shadow:none;" title="Click to mark as favorite question (click again to undo)"><svg aria-hidden="true" class="svg-icon iconStar" height="18" viewBox="0 0 18 18" width="18"><path d="M9 12.65l-5.29 3.63 1.82-6.15L.44 6.22l6.42-.17L9 0l2.14 6.05 6.42.17-5.1 3.9 1.83 6.16L9 12.65z"></path></svg>
    </button>

    Hi i am making a grading system using visual studio 2010 c# using local database and i am just going to ask on how can i do a log in form for every users that shows a data in a combo box or data grid view that is assign to them during run time. Where other users can't see all the data in database, they can only see the data that is assign to them during run time. i tried making it by assigning a user id per subject but it didn't work hope you can help me thank you :)

    if(scmd.ExecuteScalar().ToString()=="1")
    {
    pictureBox1.Image = new Bitmap(@"C:\Users\Mic 18\Documents\Visual Studio 2015\Projects\mylogin\granted.png");
    MessageBox.Show("YOU ARE GRANTED WITH ACCESS");
    }
    else
    {
    pictureBox1.Image = new Bitmap(@"C:\Users\Mic 18\Documents\Visual Studio 2015\Projects\mylogin\denied.jpg");
    MessageBox.Show("YOU ARE NOT GRANTED WITH ACCESS");
    lbl_Msg.Text = ("You Have Only " + Convert.ToString(attempt) + " Attempt Left To Try");
    --attempt;
    txt_UserName.Clear();
    txt_PWD.Clear();
    }

    I am trying using this code but it only do a user level

    • Moved by CoolDadTx Friday, November 15, 2019 2:47 PM Winforms related
    Friday, November 15, 2019 5:33 AM

All replies

  • Hi ToastMalone,

    Thank you for posting here.

    Based on your description, you seems to want to show different data when different user logs in.

    You can add a column in the data table to record the user id which this row of data belongs.

    Here is the code.

            private void Button1_Click(object sender, EventArgs e)
            {
                string connString = @"connectionstring ";
                using (SqlConnection con = new SqlConnection(connString))
                {
    
                    con.Open();
                    string sql = $"select content from dataTable where userid = (select id from userTable where username = '{textBox1.Text}')";
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
                    DataSet set = new DataSet();
                    dataAdapter.Fill(set);
                    DataTable dataTable = set.Tables[0];
                    dataGridView1.DataSource = dataTable;
    
                }
            }


    UserTable:

    DataTable:

    Result:


    If you want to create a data table for each user, you can execute the following SQL statement when the user registers.

    string sql1 = $@"CREATE TABLE {userName}_data (
                                        column1 datatype,
                                        column2 datatype,
                                        column3 datatype,
                                       ....
                                    );";

    Hope this could be helpful.

    Best Regards,

    Timon


    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.




    Friday, November 15, 2019 9:50 AM
  • Hello,

    • The following is for SQL-Server.
    • It's not a full solution, it's a really good starter
    • You will need to invest time into getting it all together

    If you can't handle the above do not continue.

    The following link at first may appear a) only for Entity Framework b) hard to understand c) incomplete for a total solution. But if you take time to study it all is there for doing a student database which work with stored procedures.

    Each student is in a person table where you can query for grades against their primary key e.g.

    DECLARE @StudentIdentifier AS INT = 7
    SELECT SG.EnrollmentID ,
           SG.Grade ,
           SG.CourseID ,
           SG.StudentID ,
           C.Title
    FROM   dbo.StudentGrade AS SG
           INNER JOIN dbo.Course AS C ON SG.CourseID = C.CourseID
    WHERE  ( SG.StudentID = @StudentIdentifier );

    In C# code create a connection and command object and a parameter for the command to inject the value for the student id rather than @StudentIdentifier (it's wise to first create the query in the database to ensure it works), use ExecuteReader to get the data into a DataTable. Even better, the sample database has a Stored Procedure for this so simply call the stored procedure with the student identifier.

    Here is a raw query for example.

    Here is what is included in the database.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, November 15, 2019 11:22 AM
    Moderator
  • Thank you :)your answer really answer my question but i am just a bit confused in your data table because you have 2 id's and by the way can you do it in a combo box with a log in? i mean if you log in a user it shows different data in combo box thank you again :)
    Friday, November 15, 2019 1:01 PM
  • Thanks it helps a lot :)
    Friday, November 15, 2019 1:03 PM
  • and by the way i am using a local database in c# does that query applicable in local db of visual studio? thanks for the help appreciate it a lot :) 
    Friday, November 15, 2019 1:04 PM
  • Hi ToastMalone,

    Thanks for your feedback.

    The first id is the id of the row of data, and the userid is the id of the owner of the row of data, which corresponds to the id in the UserTable.

    And for your last question, We are using a local database.

    Hope this could be helpful.

    Best Regards,

    Timon


    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.

    Tuesday, November 19, 2019 8:42 AM