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

0 comments:

Post a Comment