Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using InStr Function 1

Status
Not open for further replies.

arpan

Programmer
Oct 16, 2002
336
IN
I am retrieving the Casual Leaves (CL) & Medical Leaves (ML) from a SQL Server DB table & displaying them in an HTML table using the Repeater control. It is possible that some of the records are not whole numbers; they can be something like 15.5 or 28.5 i.e. the records will either be a whole number or a fraction ending with .5 only (no other decimals allowed). I want to display the records like 30.0, 24.5, 29.0, 22.0 etc... i.e. whole numbers should be suffixed by .0 & fractional numbers should be displayed as
it is. This can be done using the InStr function to find out whether a decimal point is present in the record or not. This is what I have done:

<script language=&quot;VB&quot; runat=&quot;server&quot;>
Sub Page_Load(obj As Object,ea As EventArgs)
Dim objDS As DataSet
Dim objConn As SQLConnection
Dim objDapter As SQLDataAdapter
objConn=New SQLConnection(&quot;Server=(local);Database=DBName;UID=sa;PWD=&quot;)
objDapter=New SQLDataAdapter(&quot;SELECT EName,CL,ML FROM.......&quot;,objConn)

objDS=new DataSet()
objDapter.Fill(objDS,&quot;LeaveDetails&quot;)

myRepeater.DataSource=objDS.Tables(&quot;LeaveDetails&quot;).DefaultView
myRepeater.DataBind()
End Sub
</script>
<form runat=&quot;server&quot;>
<asp:Repeater id=&quot;myRepeater&quot; runat=&quot;server&quot;>
<HeaderTemplate>
<table border=3>
<tr>
<th>EMPLOYEE NAME</th>
<th>CASUAL LEAVES</th>
<th>MEDICAL LEAVES</th>
</tr>
</asp:HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Container.DataItem(&quot;EName&quot;) %></td>
<td><%# Container.DataItem(&quot;CL&quot;) %></td>
<td><%# Container.DataItem(&quot;ML&quot;) %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>

Now my question is where do I accomodate the InStr function in the above ASPX code to find out whether a number is a whole number or not - if it is a whole number, then suffix it with .0 else display the record as it is?

Thanks,

Arpan
 
Arpan: Good question. I was looking at the same problem a few days ago. If you could abandon the ItemTemplate approach and bind to a datagrid the following takes care of the problem:

<HTML>
...
<asp:DataGrid id=&quot;dgGrid&quot; AutoGenerateColumns=&quot;false&quot; runat=&quot;server&quot;>
<Columns>
<asp:BoundColumn
DataField=&quot;EName&quot;
HeaderText=&quot;EName&quot;/>
<asp:BoundColumn
DataField=&quot;CL&quot;
HeaderText=&quot;CL&quot;
DataFormatString=&quot;{0:c1}&quot;/>
<asp:BoundColumn
DataField=&quot;ML&quot;
HeaderText=&quot;ML&quot;
DataFormatString=&quot;{0:c1}&quot;/>
</Columns>
</asp:DataGrid>
...
</HTML>

If you want to use the &quot;InStr&quot; function, or a formatting statement in the code behind to save your ItemTemplate structure, I would suspect that you'll want to loop through the table as you load it it (using a &quot;While myReader.Read()...Loop structure) or other arrangement where you can pass the field elements through a formatting statement or InStr function during the loading process.
 
Arpan: Used the &quot;currency&quot; format {0:c1} should have been {0:f1} for doubles.
 
Hi,

Thanks for your response. Actually there is no need to abandon ItemTemplate as you have suggested. The formatting can still be done like this using ItemTemplate:

<asp:Repeater id=&quot;myRepeater&quot; runat=&quot;server&quot;>
<ItemTemplate>
<td>
<% #DataBinder.Eval(Container.DataItem,&quot;CL&quot;,&quot;{0:n1}&quot;) %>
</td>
.............
</ItemTemplate>

In fact, another easier approach to this would be using the FormatNumber function like this:

<td><%# FormatNumber(Container.DataItem(&quot;CL&quot;),1) %></td>

Regards,

Arpan
 
Arpan: Good work. I tried putting the formatting string in the template but left off the &quot;..&quot; quotes so it didn't work. Thanks for the insight. Good Luck.
 
It's my pleasure!!!!!!

Regards,

Arpan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top