When the database field is empty, there are many ways in the B / S structure, and the DOTNET era is in the DOTNET era, and it is different from the original inherent. It is simply to emphasize the self-processing of the server. Step step by step, you will start to say from everyone's most familiar thing.
First use a simple ASP Access message as an example. In the database, there is a corresponding response for each message, and as long as the message will be tagged with a data type unriddonicatus field, replying to marked with "1", otherwise "0" ( Only a key part of the code is given below, that is, the value of the MessageStatus field is true or fake):
<%
IF Messagers ("Messagestatus") = false kil
%>
................ (omitted HTML code here)
<%
Else
Set replyrs = server.createObject ("adoDb.recordset")
ReplySQL = "SELECT * from reply where messageid =" & message ("messageid")
Replyrs.open replysql, conn
%>
................ (omitted HTML code here)
<%
Replyrs.close
SET replyrs = Nothing
END IF
%>
In ASP.NET, since the program code has been fully able to separate the HTML code, add the server-side controls provided, so that we must find a way to ASP.NET. Let's go directly into our topic, and discuss how ASP.NET is better.
The three methods known below are listed below. The following is also an example.
The first--
It can be solved this way, write a function in the CS page, used to determine if a post has a reply, if there is a reply, return the reply content, no, return "no reply". Then bind this method to a DataGrid or DataList, or a Label control in the Reapter space ItemTemplate.
The following is an example for displaying a picture. First, use the isimageavailable () method to determine if this picture is present, returning is a Boolean value to control whether the IMG control and the Label control are displayed. If there is a picture, the IMG control is displayed, Otherwise the Label control is displayed.
Visible = '<% # isimageavailable (dataBinder.eval (Container.DataItem, "Lastname"). TSTRING ())%>'
SRC = '<% # "Images //" DataBinder.eval (Container.DataItem, "LastName") ".bmp"%>' />
itemtemplate>
asp: templateColumn>
// isiMageavailable () function
Private Bool IsImageavailable (String Strlastname)
{
String strimagefile = "images //" strlastname ".bmp";
Return file.exists (server.mappath (strimagefile);
}
This is probably like this. When there is no picture, use the Label control to display "No Picture Available".
Second kind -
The message board data shows that it is easy to use the DataGrid control. When using the DataGrid control display data, if the field value is null, it will automatically default empty. So we can save the data as a DataSet, then bind to the DataGrid, use the for loop to determine if a field is empty, as follows:
For (Int i, i { If DS.TABLE [0] .row [i] ["field name] = NULL { DS.TABLE [0] .Row [i] ["field name"] = "No reply"; } } Third type - Use only SQL statements directly. Let's first take a look at the SQL statement: Select Guestname, Guest, PostTentAme, PostTime, Postcontent, PostTime, Postip, Case When (ReplyContent is Null) The 'unregistered' else replycontent end as reply from guestbook order by ID DESC There is a case when () the ... Else ... End As (), here, look at its specific usage. Case Calculate the list of conditions and return one of multiple possible results expressions. Case has two formats: 1. Simple Case function compares an expression with a simple expression to determine the result. 2, the CASE search function calculates a set of Boolean expressions to determine the result. Both formats support optional ELSE parameters. grammar Simple case function: Case INPUT_EXPRESSION WHEN WHEN_EXPRESSION THEN RESULT_EXPIPRESSION [... N] [ELSE ELSE_RESULT_EXPRESSION END CASE search function: Case when boolean_expression dam_expression [... n] [else else_result_expression end parameter INPUT_EXPRESSION It is the expression calculated when using simple case format. Input_expression is any effective Microsoft® SQL ServerTM expression. Whenew_expression Simple expressions compared when INPUT_EXPRESSION is used when using simple case format. When_Expression is an arbitrary effective SQL Server expression. Input_expression and the data type of each when_expression must be the same, or hidden conversion. n The placeholder indicates that multiple WHEN. Then Result_expression When INPUT_EXPIRESSION = when_expression is True, or the expression returned when the Boolean_Expression is taken by TRUE. Result Expression is an arbitrary effective SQL Server expression. Else Else_Result_Expression When the comparison calculation is not returned, the expression is not returned. If this parameter is omitted and the comparison calculation is not true, CASE will return a NULL value. Else_Result_Expression is an arbitrary effective SQL Server expression. ELSE_RESULT_EXPRESSION and all Result_Expression data types must be the same, or must be implicit conversions. When Boolean_Expression The Boolean expression calculated when using the CASE search format. Boolean_expression is an arbitrary effective Boolean expression. Type Returns the highest priority type type from Result_expressions and the optional else_Result_Expression. So, summarize the three methods mentioned above: 1. The first method requires additional writing a process. Added workload; 2, using Dataset in the DOTNET, it is not a problem for friends who can be skilled, but this is a bit difficult for beginners. And there is no consideration from the angle of reducing the code; 3, fully utilize the SQL statement, complete the processing of the empty field to the server, do not need to write any code. In fact, whether it is a B / S structure procedure or a C / S structure, it is inseparable from the database association, then a good SQL statement will bring great convenience. The arrival of the DOTNET era will further speed up our integrity three-layer architecture, and directly utilize the SQL statement directly in the program described above will be irregular, and the future procedures will be divided into data layers, intermediate layers, application layers Then, the development of the data layer is actually directly, or indirectly associated with the SQL statement, so it will be necessary to fully explore the existing SQL statements.