2

I have a GetUser method to call a sql procedure and return a couple of fields. I need to hash the values but in testing it is working. Here is the method:

(edit: "The_User" is a public instance of my User class set outside of the method. This is just what I ended up with after screwing around trying to get the Person_ID to pass to my SaveRecord Method. I am open to doing this differently/better)

 public ActionResult GetUser(Models.User model)
    {
        User x = new User();
        x.User_Name = model.User_Name;
        x.Password = model.Password;
        using (var conn = new SqlConnection("connection stuff"))
        using (var com = new SqlCommand("GetUser", conn)
        { CommandType = CommandType.StoredProcedure })
        {
            com.Parameters.AddWithValue("@User_Name", x.User_Name);
            com.Parameters.AddWithValue("@Password", x.Password);
            conn.Open();
            using (var reader = com.ExecuteReader())
            {
                while (reader.Read())
                {
                    The_User.User_Name = reader["User_Name"].ToString();
                    The_User.Password =  reader["Password"].ToString();
                    The_User.Person_ID = reader["PersonID"].ToString();
                }
                if (x.User_Name == The_User.User_Name && x.Password == The_User.Password)
                {
                    User_Auth = 1;
                }
                else
                {
                    User_Auth = 0;
                }
            }
            conn.Close();
        }
    }

Later on down the workflow I want to get the Person_ID value from the GetUser method and pass it to another method - "Save Record". Here it is:

 public ActionResult SaveRecord(Models.ModelData model)
    {
        ModelData md = new ModelData();
        md.thing1 = model.thing1;
        md.thing2 = model.thing2;
        md.thing3 = model.thing3;

        if (md.thing1 is null) { md.thing1 = ""; };
        if (md.thing2 is null) { md.thing2 = ""; };
        if (md.thing3 is null) { md.thing3 = ""; };


        using (var conn = new SqlConnection("connection stuff"))
        using (var com = new SqlCommand("GetAppData1", conn)
        { CommandType = CommandType.StoredProcedure })
        {
            com.Parameters.AddWithValue("@thing1", md.thing1);
            com.Parameters.AddWithValue("@thing2", md.thing2);
            com.Parameters.AddWithValue("@thing3", md.thing3);
            com.Parameters.AddWithValue("@PID", I NEED PERSON ID RIGHT HERE!!!);
            conn.Open();
            com.ExecuteNonQuery();
            conn.Close();
        }
        return View();
    }

I have tried numerous solutions found on Stack overflow and keep running into my passed variable being null by the time I need it when the Save Record method is called. I appreciate any and all Help! :)

9
  • 1
    How are you currently getting the Person_Id from the get call and passing it to SaveRecord ? Commented Jan 10, 2020 at 18:16
  • 1
    What do you mean by down the workflow? How the two methods are chained together? I'd say you could use the Session container to store/retrieve data between requests in a safe way but I'm under impression you don't quite know what you are doing. Commented Jan 10, 2020 at 18:17
  • Clearly the Person Id is set on The_User but there's no indication in the code you've shown where The_User is defined or what its scope is. Commented Jan 10, 2020 at 18:22
  • Your while (reader.Read()) loop just overwrites The_User fields over and over, so your authorization check only reflects the status of the last record in the result set. Commented Jan 10, 2020 at 18:23
  • 1
    this code don't even compile. Your GetUser Method does not return a value. Commented Jan 10, 2020 at 18:49

1 Answer 1

1

There are multiple ways to do this.

First, try adding @Html.HiddenFor(m=> m.Person_ID") as part of the View.cshtml being returned by GetUser ActionResult. Assuming you are returning return View("Name Of View", The_User).

If you are using User.Identity you can extend it as described by the answer on this post

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.