Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to get the UserID from the form and cross reference this from my sql table to edit the specific row. I am currently getting the below error message:

The parameterized query '(@UserID nvarchar(4000))SELECT * FROM [JB_DB].[dbo].[Tble_SD_Use' expects the parameter '@UserID', which was not supplied.

Below is the c# code:
C#
public void OnGet()
{

    String ID = Request.Query["UserID"];

    try
    {

        String connectionString = "Data Source=SQLTableSource";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {

            connection.Open();

            String sql = "SELECT * FROM [JB_DB].[dbo].[Tble_SD_Users] WHERE UserID = @UserID;";

            using (SqlCommand commmand = new SqlCommand(sql, connection))
            {

                commmand.Parameters.AddWithValue("@UserID", ID);
                //commmand.Parameters.Add("@ID", System.Data.SqlDbType.Int).Value = Convert.ToInt32(id);
                //commmand.Parameters.AddWithValue("UserID", userInfo.UserID ?? (object)DBNull.Value);

                //commmand.Parameters.AddWithValue("@UserID", System.Data.SqlDbType.NVarChar).Value = userInfo.UserID;

                using (SqlDataReader reader = commmand.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        userInfo.ID = "" + reader.GetInt32(0);
                        userInfo.UserID = reader.GetString(1);
                        userInfo.firstName = reader.GetString(2);
                        userInfo.lastName = reader.GetString(3);
                        userInfo.userDepartment = reader.GetString(4);
                    }
                }

            }

        }

    }


What I have tried:

I have another edit page in my asp.net project and it looks at another table but, the below code works:

C#
public void OnGet()
{

    String ID = Request.Query["tStatus"];

    try
    {
        String connectionString = "Data Source=SQLTableSource";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            //String sql = "SELECT TOP (1000) ISNULL([tNumber], ' ') [tNumber], ISNULL([tStatus], ' ') [tStatus], ISNULL([tApplication], ' ') [tApplication], ISNULL([tType], ' ') [tType], ISNULL([tDateLogged], ' ') [tDateLogged], ISNULL ([tDateUpdated], ' ') [tDateUpdated], ISNULL ([tDateClosed],' ') [tDateClosed], ISNULL([tDateLastChecked], ' ') [tDateLastChecked], ISNULL([tUserLogged], ' ') [tUserLogged], ISNULL([tCurrentlyWith], ' ') [tCurrentlyWith], ISNULL([tLoggedOrAdHocTTL], ' ') [tLoggedOrAdHocTTL] FROM [JB_DB].[dbo].[Tble_COF_IT_UK_Tickets];";
            String sql = "SELECT * FROM [JB_DB].[dbo].[Tble_COF_IT_UK_Tickets] WHERE tStatus = @tStatus;";
            using (SqlCommand command = new SqlCommand(sql, connection))
            {
                command.Parameters.AddWithValue("@tStatus", ID);
                //command.Parameters.Add("@ID", System.Data.SqlDbType.Int).Value = Convert.ToInt32(ID);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        ticketInfo.ID = "" + reader.GetInt32(0);
                        ticketInfo.tNumber = reader.GetString(1);
                        ticketInfo.tStatus = reader.GetString(2);
                        ticketInfo.tApplication = reader.GetString(3);
                        ticketInfo.tType = reader.GetString(4);
                        ticketInfo.tDateLogged = reader.GetString(5);
                        ticketInfo.tDateUpdated = reader.GetString(6);
                        ticketInfo.tDateClosed = reader.GetString(7);
                        ticketInfo.tDateLastChecked = reader.GetString(8);
                        ticketInfo.tUserLogged = reader.GetString(9);
                        ticketInfo.tCurrentlyWith = reader.GetString(10);
                        ticketInfo.tLoggedOrAdHocTTL = reader.GetString(11);
                    }
                }
            }
        }
    }
Posted
Comments
Richard Deeming 23-Nov-23 7:09am    
If you debug your code, does Request.Query["ID"] actually return a non-empty value?
Jordan Blair 23-Nov-23 7:25am    
The Request.Query["UserID"] looks at the html form to see the UserID in question
Richard Deeming 23-Nov-23 7:29am    
That's not an answer to my question:

When you debug your code, does the Request.Query["ID"] return a non-empty value?

Nobody else can debug your code for you.
Jordan Blair 23-Nov-23 7:49am    
When I run the debug, nothing appears. However, when I hover over it in the code, I see the below:
Gets the query value collection parsed from Request.QueryString.

Returns:
The query value collection parsed from Request.QueryString.

'Query'is not null here.

CS8600: Converting null literal or possible null value to non-nullable type
Richard Deeming 23-Nov-23 8:40am    
And what does the ID variable contain after that line executes?

1 solution

Quote:
ID didn't appear to have a value.

There you go! That's why you're getting the error. Now all you have to do is figure out why your page isn't sending the "tStatus" value in the query string to your controller code.
 
Share this answer
 
Comments
Jordan Blair 24-Nov-23 7:29am    
The issue isn't with tStatus. That one is working, the issue is with the first section of the code.
Dave Kreskowiak 24-Nov-23 11:06am    
Now you're just being ridiculous. YOU YOURSELF said ID doesn't have a value after this line:
string id="Request.Query["tStatus"]

That can ONLY mean you don't have a value coming in from the URL query string. The rest of the code does not matter if you do not have a value to start with.

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