Showing posts with label status. Show all posts
Showing posts with label status. Show all posts

Wednesday, March 28, 2012

substitute data from database into web form

Hello.

Newbie here, I have a field in a database called status with the values "I" or "A". I need to display "Inactive" or "Active" in a gridview or formview web page. How do I substitute these values.

Thanks in advance.

Is this data being displayed in a datalist / datagrid? You can either create a temp table in your sql or modify your code. If you post your code I can show you how, otherwise you can do something like this in your sql:

create table #temp(
activeInactiveField1 char(5)
, activeInactiveField2 char(10)
-- , your other fields here
)

Insert into #Temp(
-- all fields from your existing sql
from all your existing tables

update #temp
set activeInactiveField2 = 'Active'
where activeInactiveField1 = 'A'

update #temp
setActiveInactiveField2 = 'Inactive'
where activeInactiveField1 = 'I'

select * from #Temp
drop table #Temp

Then you can databind the field in your webform to activeInactiveField2


Hello, Thank you for the quick response. Below is my code. As stated I am a newbie and the filed I am working with is status. Thanks a lot.

<%@.PageLanguage="VB"AutoEventWireup="false"CodeFile="Default2.aspx.vb"Inherits="Default2" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataSourceID="SqlDataSource1"

Style="z-index: 100; left: 0px; position: absolute; top: 0px">

<Columns>

<asp:BoundFieldDataField="Citation"HeaderText="Citation"SortExpression="Citation"/>

<asp:BoundFieldDataField="Type"HeaderText="Type"SortExpression="Type"/>

<asp:BoundFieldDataField="Status"HeaderText="Status"SortExpression="Status"/>

<asp:BoundFieldDataField="Last Name"HeaderText="Last Name"SortExpression="Last Name"/>

<asp:BoundFieldDataField="First Name"HeaderText="First Name"SortExpression="First Name"/>

<asp:BoundFieldDataField="Middle"HeaderText="Middle"SortExpression="Middle"/>

<asp:BoundFieldDataField="DOB"HeaderText="DOB"SortExpression="DOB"/>

<asp:BoundFieldDataField="Violation Date"HeaderText="Violation Date"SortExpression="Violation Date"/>

<asp:BoundFieldDataField="Charge"HeaderText="Charge"SortExpression="Charge"/>

<asp:BoundFieldDataField="Description"HeaderText="Description"SortExpression="Description"/>

<asp:BoundFieldDataField="Disposition"HeaderText="Disposition"SortExpression="Disposition"/>

<asp:BoundFieldDataField="Cost"HeaderText="Cost"SortExpression="Cost"/>

<asp:BoundFieldDataField="Paid"HeaderText="Paid"SortExpression="Paid"/>

<asp:BoundFieldDataField="Balance"HeaderText="Balance"SortExpression="Balance"/>

</Columns>

</asp:GridView>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:MUNI_COURTConnectionString4 %>"

ProviderName="<%$ ConnectionStrings:MUNI_COURTConnectionString4.ProviderName %>"

SelectCommand="SELECT * FROM [MUNI_COURT_REC]"></asp:SqlDataSource>

</div>

</form>

</body>

</html>


You can handle this in RowDataBound event of the gridview. To add this event to your gridview, when in design view of Visual Studio, click on gridview once to get the properties window. From that, go to the events window (identified by a small "lightning" icon) and look for RowDataBound event. Double click on this event and an event hadler will created in your code file. Just copy and paste this code there.

protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e) {if (e.Row.RowType == DataControlRowType.DataRow) {if(e.Row.Cells[7].Text =="A") { e.Row.Cells[7].Text ="Active"; }else { e.Row.Cells[7].Text ="Inactive"; } } }

Or you can change your select statement - something like select case status when 'i' then 'inactive' else 'active' end as status from blah blah blah...

By the way, it's never good practice to do a select *.


Hi, try this inline IIF statement to evaulate the status field and display conditional text. You will have to first convert your GridView to a template so you have control over the HTML controls.

<asp:LabelID="LabelStatus"runat="server"Text='<%# IIF(CONVERT.ToString(Eval("Status"))="A","Active", "Inactive") %>'></asp:Label>


Thanks I got that suggestion to work in a gridview and formview. Big Smile

What if I had another field called type that I had to change

"H" to "Harbor"

"C" to "Civil"

"T" to "Traffic"

"L" to "Local"


You can use the same solutions that are marked as answer to extend the functionality. Like in mine, you can access the columns by their index (0 based). So whatever the index of your "Type" column, access it in RowDataBound event, check the text of the cell, and change it accordigly.


Sorry bullpit,

when I run your suggestion I get the following error message

Compiler Error Message:BC30205: End of statement expected.

I believe your suggestion would work best.

I forgot to uncheck the answer message button


Paste your code here. Please use the InsertCode option in the text editor.


Partial Class Default2 Inherits System.Web.UI.Page Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBoundprotected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e) {if (e.Row.RowType == DataControlRowType.DataRow) {if(e.Row.Cells[7].Text =="A") { e.Row.Cells[7].Text ="Active"; } Else { e.Row.Cells[7].Text ="Inactive"; } } } End SubEnd Class

Thanks


learn_asp_fast:

Partial Class Default2 Inherits System.Web.UI.Page Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBoundprotected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e) {if (e.Row.RowType == DataControlRowType.DataRow) {if(e.Row.Cells[7].Text =="A") { e.Row.Cells[7].Text ="Active"; } Else { e.Row.Cells[7].Text ="Inactive"; } } } End SubEnd Class

The piece of code I posted was an event handler for RowDataBound event. An event handler is a method (function) by itself. What you did was that you attached an event handler the way I mentioned (by double clicking the RowDataBound event in designer) but also copied and pasted the event handler that I posted into the event handler that the designer generated. So now you have a function defined in another function, which is not right. Morover, you are using VB and the code I pasted was C#. Change the above code to this:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBoundIf e.Row.RowType = DataControlRowType.DataRowThen If e.Row.Cells(7).Text ="A"Then e.Row.Cells(7).Text ="Active"Else e.Row.Cells(7).Text ="Inactive"End If End If End Sub

Bullpit,

Thank you very much for your time. I did realize I pasted the code into another function, but neith er way worked for me. I should be able to get this sample to work in other ways for me.

This a very helpful board.


Also, change the column index (7 in the code I posted) to the column index where you want to change the text. That might be the problem. For example, in your gridview, the "STATUS" colum index maybe 4 instead of 7.


Bullpit,

If you do not mind hopw do I get that to work in a Formview?

Thanks in advance

Monday, March 26, 2012

Suggestion for App

I am trying to do a web page with a network status on it for several apps. I have tried it one way and posted it here but no replies. Any suggestions on how to have it ping a server with many different servers with online if it gets a reply or offline if it gets the time out or cant ping. Any helpe would be appreciated.

Thanks

To simply ping a server it is as simple asMy.Computer.Network.Ping("hostnameOrIp")

It will return true or false.


Cool. What I am trying to do is take that information and put in a page that says Online or Offline. But I have several Servers to do this. Any suggestions? Thanks for replying by the way.

Well, do one ping per server :P The way ping works is that it tries to send small packets to the destinated IP, if the data gets through we have PING! (funny word btw ;-). Things will get a bit different if you want to check if the server answers on specific ports. But just to do ping on several servers is piece of cake:

If My.Computer.Network.Ping("server1") = true
response.write("Online")
else
response.write("Offline")
end if

If My.Computer.Network.Ping("server2") = true
response.write("Online")
else
response.write("Offline")
end if


I would setup an ArrayList to hold all the servers, then loop through them, ping, and print their status:

Dim arrServersAs New ArrayList()Dim intCountAs IntegerDim strMessageAs StringarrServers.Add("192.168.1.100")arrServers.Add("192.168.1.101")arrServers.Add("192.168.1.102")For intCount = 0To arrServers.Count - 1 strMessage ="Server " &CStr(arrServers(intCount))If My.Computer.Network.Ping(arrServers(intCount))Then strMessage +=" is online"Else strMessage +=" is offline"End If Response.Write(strMessage)Next
 
Hope this helps! Please do not forget to mark the helpful post(s) as Answer so future readers know what solved your problem.

MY is requiring declaration and where is it to be declared?

IfMy.Computer.Network.Ping(arrServers(intCount))Then


Are you using ASP 1.x or 2.0?
2.0

Well that is really strange, becuase My works just fine and I have tested it.

It should be an automatically inherited namespace. I guess the only thing I can suggest you to try it to put

Imports My

at the top of the code behind.

Also, are you using C#?


Heck no. I can't stand anything to do with C. LOL I am using VB.

It works to an extent. I have about 14 servers I do this and I want to be able to do:

XXX Online or XXX Offline

YYY Online or YY Offline

Right now it is accounting all of the being online becasue it is going to the first one.


Thanks,


I got it to work. I had to alter some of the code. I got rid of the arrays and the loop. I did use the rest of your coding. I appreciate your help.
Glad you got it working! Please mark the most helpful post(s) asAnswer for the sake of future readers. Thank you.