protected int getRowCount()
            int RowCount = 0;
            SqlConnection conRowCount = new SqlConnection(ConfigurationManager.ConnectionStrings["connMSJ"].ConnectionString);
            SqlCommand cmdRowCount = new SqlCommand("SELECT Category FROM MonthlyBudget WHERE (Username=@username AND CurrentMonth=@current) GROUP BY Category", conRowCount);
            cmdRowCount.Parameters.AddWithValue("@username", Master.getUsername);            
            for (int i = 0; i < intMonth.Length; i++)
                cmdRowCount.Parameters.AddWithValue("@current", Convert.ToDateTime("1/" + intMonth[i] + "/" + ddlYear.SelectedItem.Text));
                SqlDataReader dtrRowCount = cmdRowCount.ExecuteReader();
                if (dtrRowCount.Read())
            return RowCount;

i want to only compare the years since i am going to produce a yearly report.
this code is can't work due to some error of the parameters values passing.

3 Years
Discussion Span
Last Post by gahhon

What is the actual error? Also, you are sure that an acceptable value is stored in ddlYear.SelectedItem.Text?

You should test your SQL query without the use of variables and textbox.Text first to make sure your SQL is good.

For example.. what happens if line 9 is like this...

cmdRowCount.Parameters.AddWithValue("@current", Convert.ToDateTime("1/1/2013"));

Ya it is able to execute, but i just want comparing the year with the end-users selected year on dropdownlist, but i have no idea to do this other than i provided in previous reply.

The actual error is "The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects." at line 14

if i excluded the remove("@current") the error displayed "@current" is already assigned a value.

Edited by gahhon


I generally use SelectedValue, not SelectedItem for the dropdown selection. Try ddlYear.SelectedValue instead.

I'm not understanding the purpose of your loop and what intMonth[i] is referring to.


Due to i am not assigning the value to each of the item in dropdownlist, thus i using ddlYear.SelectedItem.Text instead of SelectedValue.
For the intMonth[i] is because of our sql data is store Date datatype which result in 1/1/2013 and this is used to be produce yearly report, but i have to ignoring the month but i have no other idea to doing this. :(


Problem solved by adding a new column which storing years. Thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.