0
        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));
                conRowCount.Open();                
                SqlDataReader dtrRowCount = cmdRowCount.ExecuteReader();
                if (dtrRowCount.Read())
                    ++RowCount;
                cmdRowCount.Parameters.Remove("@current");
                dtrRowCount.Close();
                conRowCount.Close();
            }           
            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.

2
Contributors
5
Replies
14
Views
4 Years
Discussion Span
Last Post by gahhon
0

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"));
0

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

0

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.

0

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. :(

0

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.