Click here to Skip to main content
15,904,877 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
string rowValue = row["TrainNo"].ToString();


im storing the value of train no in rowvalue and want to divide the rowvalue into the loops to find exact no plz help asap

What I have tried:

string rowValue = row["TrainNo"].ToString();
Posted
Updated 11-Jun-18 21:16pm
Comments
[no name] 12-Jun-18 2:41am    
What is the format of TrainNo and what is the expected output? The code you posted isn't relevant to the question.
Shahbaz435 12-Jun-18 2:45am    
trainno datatype is varchar and i want the value coming in rowvalue should be divide in the loop
[no name] 12-Jun-18 2:48am    
I have no idea what you're trying to say, sorry.
Shahbaz435 12-Jun-18 2:49am    
kk
Shahbaz435 12-Jun-18 2:47am    
string rowValue = row["TrainNo"].ToString();

string[] splitString = rowValue.Split(',');
foreach (string str in splitString)
{
if (str == ("0"))
{
SoundPlayer sd = new SoundPlayer();
sd.SoundLocation = Server.MapPath("~/SoundWave/0.wav");
sd.PlaySync();
}


here is the code i want to divide the value in loop i have code but its oly supported to the comma i want to take the whole string and divide by 10000

If you want to turn it into a double and then divide it by 10000, you could do:

C#
if (!double.TryParse(rowValue, out double value)) {
  // Invalid format.
}
value /= 10000;
 
Share this answer
 
Comments
Shahbaz435 12-Jun-18 3:02am    
can u plz type in detail bcoz i'm not fimiliar with loops plz help
While the solution by Thaddeus Jones will work, do yourself a favour: Don;t store data in VARCHAR or NVARCHAR columns unless it is genuinely a string. Store data in appropriate datatypes: integers in INT columns, floating point numbers in FLOAT columns, date in DATE, DATETIME, or DATETIME2. It saves a lot of problems later and makes your code much, much, easier when you want to work with the data, because the column can't contain invalid data. If you have an integer TrainNumber column, then nothing can put "Jaipur" or "12K" in there, causing your code to fail because it isn't a number.
Then all you have to do is
int rowValue = (int) row["TrainNo"];
And off you go...
 
Share this answer
 
Comments
Shahbaz435 12-Jun-18 3:22am    
see my train no datatype is varchar bcoz i 'm storing the int and char in that column so thts y im getting toruble
OriginalGriff 12-Jun-18 3:37am    
Precisely. If you have a number, don't store it in a string based column: and definitely don't store it in a string based column with genuinely string based data!
Consider changing the column to two columns: a numeric part, stored probably in a INT and a string part in a NVARCHAR - you can add a Computed Column to return the combination if other code needs it, but it means you can work with the bit you want immediately (and the sort order works properly as well!)

While it's possible to split out the numeric and text part, it's a pain to do that whenever you need "just the number", especially if the data in the column isn't necessarily in a specific format (i.e. always four digits, then eight characters for example).

It may seem like more work, but it's a trivial change at eth beginning of the project that makies the rest of the project which works with the data considerably easier (and thus more reliable).
What does your TrainNo column data look like at the moment?
Based on your clarifications, your code has been modified. I hope this works for you.

C#
string rowValue = "12345,asdf,65431,10000";
           int result;

           string[] splitString = rowValue.Split(',');
           foreach (string str in splitString)
           {
               if(Int32.TryParse(str, out result))
               {

                   if (result % 10000 == 0)
                   {
                       Console.WriteLine(result +" : is needed");
                   }
                   else
                   {
                       Console.WriteLine(result +" : is not needed");
                   }

               }
               else
                   {
                       Console.WriteLine(str+" : is not needed");
                   }
           }
 
Share this answer
 
v2
Comments
Shahbaz435 12-Jun-18 3:23am    
no sir this is not solving my problem my problem is the loop checkinhg the whole string and divide the string and then playing the sound
Member 12270013 12-Jun-18 3:45am    
when you say 'Looping' you iterate through every single piece of string. i.e. every char in it & now you can divide that single digit by 10000. (but is that really what you need)
Now, other way around your question may be, "Condition checking on the whole string" if its a digit and then performing division on the whole string
then Do you mean to use
1> Mode(%) operation to divide the TrainNumber and get remainder?
This divides the string like (123456 % 10000) = 3456(result you get)
Now perform operations based on "3456"
OR
2> Divide(/) operation on TrainNumber and get Quotient?
This Divides string like (123456 / 10000) = 12.3456 (result you get)
Shahbaz435 12-Jun-18 4:08am    
yes like this only but how can i write the code for the loop plz help sir im new to looping
Member 12270013 12-Jun-18 9:00am    
I Have updated the solution.
please note, the value of rowValue is assumed to be a comma separated value. I hope it is the same way in your case too.

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