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.

Recommended Answers

All 5 Replies

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.