Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I'm trying to fetch intime value from dataset and i got it in string format as like '01/01/1900 10:00:00 AM'
txtintime.Text = (ds.Tables[0].Rows[0][5].ToString().Substring(11, 11));

Output: txtintime.Text = 10:00:00 AM;

It works fine but when i run it on IIS it shows error of index out of range.
So how can i handle this exception.
Plz reply,

Thank you
Posted
Comments
sudipta biswas 24-Jun-13 3:43am    
Maybe data problem.You can check the length of the before doing substring.
yogeshkansete 24-Jun-13 3:46am    
Is there any solution without using substring

Syntax for substring:
C#
string string.substring(int startIndex,int length)

Try with:
C#
Substring(12, 11)
 
Share this answer
 
v2
Or, if you know that the string will always be like

"date time AM"

the better way would be to use split

C#
string the_string = "01/01/1900 10:00:00 AM";
string[] tmp_split = the_string.Split(' ');

MessageBox.Show(tmp_split[1]); 

//the output
//tmp_split[0] = 01/01/1900
//tmp_split[1] = 10:00:00
//tmp_split[2] = AM


And you dont have to count any length. Just get it from the array :)
 
Share this answer
 
Comments
yogeshkansete 24-Jun-13 4:12am    
Thank you so much sir, it works well on IIS too.
I use split function.
Thanks once again
Please use format datetime only and get your expected result:

C#
DateTime abc = DateTime.Now;
Label3.Text = abc.ToString("MM/DD/YYYY hh:mm:ss tt").Substring(11,11);


This code will help you are alway get the your expected result

C#
txtintime.Text = (ds.Tables[0].Rows[0][5].ToString("MM/DD/YYYY hh:mm:ss tt").Substring(11, 11));
 
Share this answer
 
v2
Solution 4

First follow this website and you can do by self..

All about substrings in c#

[^]

thanks
Prince
 
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