Click here to Skip to main content
15,921,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created a function in that i want to increment count_mem but it fails to increment only 1st time increment

please check the code and tell me where i go wrong..


C#
private void testsidecount(string code)
   {

       con.Open();

       com = new SqlCommand("SELECT COUNT(*) FROM Registration_Master WHERE sponcorid='" + code + "'", con);
       int count = (int)com.ExecuteScalar();
       SqlDataAdapter da = new SqlDataAdapter();
       try
       {
           if (count == 2)
           {
               int presentCount = 0;
               presentCount++;

               SqlCommand comm = new SqlCommand();
               comm.Connection = con;
               comm.CommandText = "UPDATE Registration_Master SET count_mem = '" + presentCount + "' ,modifieddate='" + DateTime.Now + "' WHERE regCode = '" + code + "'";
               comm.CommandType = CommandType.Text;
               comm.ExecuteNonQuery();

               if ((sponcorL2.Text != "Blank") && (sponcorR3.Text != "Blank"))
               {
                   int temp = 0;
                   temp = presentCount + 1;

                   comm = new SqlCommand();
                   comm.Connection = con;
                   comm.CommandText = "UPDATE Registration_Master SET count_mem='" + temp + "' WHERE sponcorid='" + sponcor + "'";
                   comm.CommandType = CommandType.Text;
                   comm.ExecuteNonQuery();

                   if ((SponcorL4.Text != "Blank") && (sponcorR7.Text != "Blank"))
                   {
                       int temp1 = 3;

                       com.Connection = con;
                       com.CommandText = "UPDATE Registration_Master SET count_mem='" + temp1 + "' WHERE sponcorid='" + sponcor + "'";
                       com.CommandType = CommandType.Text;
                       com.ExecuteNonQuery();
                       if ((sponcorL8.Text != "Blank") && (sponcorR15.Text != "Blank"))
                       {
                           string count4 = "";
                           count4 = "4";
                           com.Connection = con;
                           com.CommandText = "UPDATE Registration_Master SET count_mem='" + count4 + "' WHERE sponcorid='" + sponcor + "'";
                           com.CommandType = CommandType.Text;
                           com.ExecuteNonQuery();
                           if ((sponcorL16.Text != "Blank") && (sponcorR31.Text != "Blank"))
                           {
                               string count5 = "";
                               count5 = "5";
                               com.Connection = con;
                               com.CommandText = "UPDATE Registration_Master SET count_mem='" + count5 + "' WHERE sponcorid='" + sponcor + "'";
                               com.CommandType = CommandType.Text;
                               com.ExecuteNonQuery();
                               if ((sponcorL32.Text != "Blank") && (sponcorR63.Text != "Blank"))
                               {
                                   string count6 = "";
                                   count6 = "6";
                                   com.Connection = con;
                                   com.CommandText = "UPDATE Registration_Master SET count_mem='" + count6 + "' WHERE sponcorid='" + sponcor + "'";
                                   com.CommandType = CommandType.Text;
                                   com.ExecuteNonQuery();
                                   if ((sponcorL64.Text != "Blank") && (sponcorR127.Text != "Blank"))
                                   {
                                       string count7 = "";
                                       count7 = "7";
                                       com.Connection = con;
                                       com.CommandText = "UPDATE Registration_Master SET count_mem='" + count7 + "' WHERE sponcorid='" + sponcor + "'";
                                       com.CommandType = CommandType.Text;
                                       com.ExecuteNonQuery();
                                       if ((sponcorL128.Text != "Blank") && (sponcorR255.Text != "Blank"))
                                       {
                                           string count8 = "";
                                           count8 = "8";
                                           com.Connection = con;
                                           com.CommandText = "UPDATE Registration_Master SET count_mem='" + count8 + "' WHERE sponcorid='" + sponcor + "'";
                                           com.CommandType = CommandType.Text;
                                           com.ExecuteNonQuery();
                                           if ((sponcorL256.Text != "Blank") && (sponcorR511.Text != "Blank"))
                                           {
                                               string count9 = "";
                                               count9 = "9";
                                               com.Connection = con;
                                               com.CommandText = "UPDATE Registration_Master SET count_mem='" + count9 + "' WHERE sponcorid='" + sponcor + "'";
                                               com.CommandType = CommandType.Text;
                                               com.ExecuteNonQuery();
                                               if ((sponcorL512.Text != "Blank") && (sponcorR1023.Text != "Blank"))
                                               {
                                                   string count10 = "";
                                                   count10 = "10";
                                                   com.Connection = con;
                                                   com.CommandText = "UPDATE Registration_Master SET count_mem='" + count10 + "' WHERE sponcorid='" + sponcor + "'";
                                                   com.CommandType = CommandType.Text;
                                                   com.ExecuteNonQuery();
                                               }
                                           }
                                       }
                                   }
                               }

                           }
                       }

                   }
               }
           }



       }
       catch (Exception ex)
       {
           throw ex;
       }
       finally
       {
           con.Close();
       }


   }


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 20-Nov-12 22:17pm
v2
Comments
[no name] 21-Nov-12 4:18am    
I think no problem in code. First you need to debug the code whether those loops are going execute at any point of time or not.

1 solution

That is probably the most cack-handed way of doing things I have seen in a very long time.

You are potentially issuing Ten UPDATE commands to the same column, of the same row, of the same table, of the same database, and only the final one that is executed is ever going to count - all the other values are discarded. In addition, you are updating it in the crudest way you can, by creating a string, adding it into another string, and sending it to SQL to parse and convert back into an integer before it can actually use it.

Did it really not occur to you to set an integer value at the start, update it in the if conditions, and only write it once to the database?

I don't know what you are trying to do with that code, but I strongly suggest you have another think about it, because I can't believe that what you have ended up with is what you planned!
 
Share this answer
 

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