Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My objective is once I click a button, the value associated with that value will be shown. In my design, there are 8 buttons and 8 value associated with each button are stored in sql procedure. However, once I click any buttons, the value is shown 8 times. How could I solve this issue?

What I have tried:

C#
foreach (Control ctrl in Controls)
            {
                if (ctrl is Button)
                {
                    ctrl.Click += new EventHandler(btn1_Click);
                    string strConn = string.Empty;
                    strConn = ConfigurationManager.AppSettings["DataDB"];

                    //SqlConnection _con = new SqlConnection(conString);
                    SqlConnection _con = new SqlConnection(strConn);
                    _con.Open();
                    //create command
                    SqlCommand cmd = _con.CreateCommand();
                    // cmd.Parameters(1);
                    //specify stored procedure to execute
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Each_Values";

                    DataTable rdr = new DataTable();
                    rdr.Load(cmd.ExecuteReader());
                    Console.WriteLine(rdr.Rows[0][0].ToString());
                }
            }
Posted
Updated 18-Sep-22 21:21pm
v2

1 solution

There are two things you need to do: firstly, look and see what your stored procedure Each_Values does. We can't do that, we have no access to your DB.

Secondly, look at see what your event handler method btn1_Click does. We can't do that either, we have no access to your code!

Then think about the symptoms of the problem: all you say is "the value is shown 8 times" but when is it shown? When you click the button? When the code you do show us is executed? We don't know - remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.

And use the debugger to look at your code while it is running - it lets you look at variables and inspect or change their content while your code executes.

If I had to guess, I'd say it's because the code you show reads the same data multiple times from the DB, but always shows the same row and column data on the console - but that's just a guess since we have no idea what your SP returns!
 
Share this answer
 
Comments
MK_VEEE 19-Sep-22 4:00am    
Hi, thanks for the suggestion.
My SP will return 3 rows for example,
EmployeeID GiftValue Acknowledge
101 12 1
102 11 0

And pls let me know how I can include picture here. Pls point me out if I missed out anything again.
OriginalGriff 19-Sep-22 4:39am    
And?
MK_VEEE 19-Sep-22 22:06pm    
Opps sorry. And is not included.
OriginalGriff 20-Sep-22 0:59am    
No, I mean: "And? What are you trying to get out of it? What have you tried? What happened when you did? Where are you stuck? What help do you need?"

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