Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
Hi,
1- when I delete the if()-else() command, label display id field value. But when I put the if()-else() command, label does not display anything. all check column have value 1.
2- Conditional orders rapidly lowers.How do I optimize the below instructions
C#
i = 0;
while (i < ds.Tables[0].Rows.Count)
{
	if (ds.Tables[0].Rows[i]["check"] == "1")
	{
		int digit = ds.Tables[0].Rows[i]["digit"];
		try
		{
			int div= digit/i;
			Label1.Text += ds.Tables[0].Rows[i]["id"].ToString() + ", ";
			i++;
		}

		catch ()
		{
			string id = ds.Tables[0].Rows[i]["id"].ToString();
			Label1.Text += ds.Tables[0].Rows[i]["id"].ToString() + ", ";
			cmd.CommandText = "update mytb set check=0 where id=" + id + "";
			con.Open();
			cmd.ExecuteNonQuery();
			con.Close();
			i++;
		}
	}

	else
	{
		i++;
	}
}
Posted
Updated 29-Aug-12 12:03pm
v5
Comments
[no name] 29-Aug-12 16:24pm    
Okay so does "ds.Tables[0].Rows[i]["check"]" equal the string "1"?
me64 29-Aug-12 16:28pm    
all check columns have value 1.
The I++ is for go to the next record. for display id field value in label
problem is not in equal the string "1"
R. Giskard Reventlov 29-Aug-12 16:57pm    
Have you set a breakpoint and stepped through the code? Where did it fail? Does it fall straight through to the catch? Why?
me64 29-Aug-12 17:06pm    
when there is a problem in divide, go to catch block

1) In quick watch verify whether column "check" has a value "1" in any of the rows,
2) You have to Change your code as suggested by Sourav Sarkar to
if (ds.Tables[0].Rows[i]["check"].ToString().Trim() == "1")
 
Share this answer
 
Comments
me64 29-Aug-12 17:24pm    
thank you
what answer for The second part of my question ? (Conditional orders rapidly lowers.How do I optimize the below instructions)
Its because your check column contain 0 or something else not 1.
that's why its excuting i++ command.
or
Use Trim() function
if (ds.Tables[0].Rows[i]["check"].ToString().Trim() == "1") 

or
if (Convert.ToInt32(ds.Tables[0].Rows[i]["check"]))== 1)
 
Share this answer
 
Comments
me64 29-Aug-12 18:04pm    
what answer for The second part of my question ? (Conditional orders rapidly lowers.How do I optimize the below instructions)
[no name] 29-Aug-12 18:29pm    
your second part of the question is not clear.

explain what is there in digit column and what you do with that div variable?
why that division line.
Explain Elaborately in your question.
me64 29-Aug-12 18:41pm    
I got the answer to the first question.
part second: You can changed commands above to better performance?
[no name] 29-Aug-12 19:02pm    
explain my questions first.
Hi there, I've restructured your code as much as possible even though the logic you've used, does not make ANY sense :S Anyways, you ought learn so many basic level coding 'stuff' like condition checking, try catch blocks, managing db connections, commenting, variable naming conventions, string manipulation, etc.

Take it slow and one by one; but learn them all.

I hope the restructuring helps the performance. If you explain what you really want to do, it would make it a lot easier to help you out.

Hope this helps, Regards
C#
int i = 0;

// This condition prevents the logic even starting if there are no valid data
if ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0))
{
	for (int counter = 0; counter < ds.Tables[0].Rows.Count; counter++)
	{
		DataRow currentRow = ds.Tables[0].Rows[counter];

		try
		{
			// I don't know why you've declared these 
			// 'div' & 'digit' variables and logic
			// if you don't use it :S
			int digit = (int) currentRow["digit"];
			int div = digit/i;
		} 
		catch
		{
			// I've not caught the exception to a variable, because you don't use it

			SqlConnection con = new SqlConnection();
			SqlCommand cmd = con.CreateCommand();
			cmd.CommandText = "UPDATE [mytb] SET [check]=0 WHERE [id] = @id";
			cmd.CommandType = CommandType.Text;
			cmd.Parameters.AddWithValue("@id", currentRow["digit"]);

			try
			{
				con.Open();
				cmd.ExecuteNonQuery();
			} 
			catch
			{
				if (con.State != ConnectionState.Closed)
					con.Close();
			}
		}

		Label1.Text += string.Format("{0}, ", currentRow["id"].ToString());
	}
}
 
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