Click here to Skip to main content
16,019,435 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
protected void LinkButton1_Click(object sender, EventArgs e)
   {
       count = 0;

        string s = Session["r"].ToString();
           string s2 = Session["r2"].ToString();
           string s3 = Session["r3"].ToString();
           string s4 = Session["r4"].ToString();
       string t="Select * from Test_paper Where Paper_id='8'";
       DataTable dt3 = new DataTable();
       dt3 = c2.bindgrid(t);
       if (dt3.Rows[0][9].ToString() == s)
       {
           count = count + 1;
       }
       else
       {
           count = count + 0;
       }
      if (dt3.Rows[0][15].ToString() == s2)
       {
           count = count + 1;
       }
       else
       {
           count = count + 0;
       }


       if (dt3.Rows[0][20].ToString() == s3)
       {
           count = count + 1;
       }
       else
       {
           count = count + 0;
       }

      if (dt3.Rows[0][25].ToString() == s4)
       {
           count = count + 1;
       }
       else
       {
           count = count + 0;
       }


       Label2.Text =count.ToString();

   }
Posted
Comments
PIEBALDconsult 13-Jul-15 14:13pm    
My what interesting code you have; did you have a question? If so, please use Improve question to add it.
Member 10774811 13-Jul-15 14:17pm    
In this code it will match value of session which is from another page with value in table and if match then the count will increment by one
Wendelius 13-Jul-15 14:25pm    
It's still unclear. What is the specific question you have in mind?
PIEBALDconsult 13-Jul-15 14:28pm    
Add that to the question (Improve question); not as a comment.

1 solution

I am not sure whether my suggestions any way solves your problem or not but check if these helps. First of all, if you want a single counter to be incremented on any of the match then why not this-
C#
if ((dt3.Rows[0][9].ToString()) == s || (dt3.Rows[0][15].ToString() == s2) || (dt3.Rows[0][20].ToString() == s3) || (dt3.Rows[0][25].ToString() == s4))
{
    count ++; //equivalent to count=count+1;
}

This piece of code yields same result as that of your 4 sets of if..else statements.

Secondly, it is not a good practice to access value from data table columns by index. It would be more readable and less error-prone if you access them by their column names like
C#
//dt3.Rows[0][20].ToString()  --bad idea
dt3.Rows[0]["columnName"].ToString();


Also, you haven't checked if the data table contains any rows or not, which may lead to exception. Make sure that you have put check before all these operations.
C#
if(dt3.Rows.Count>0)
{
 // write rest of the logic here
}

These are just suggestion for building better code and may not solve your problem but can help you identify your problem easier than earlier.
 
Share this answer
 
v2

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