Add data into database using IF Statement - asp-classic

I would like to add data into database using if statement which based on certain condition..but my codes below doesn't work..need your advise.
<% If (rs_view.Fields.Item("CGPAOverall").Value>="2.00") Then %>
rs_view("Status")="Proceed"
<% Else %>
rs_view("Status")="Stop"
<% End If %>
I would like to save the result direct into database. How can I do that? Can't get the right codes for this. Hope you can help. Thanks.

I think you need to remove all of your "<%" and "%>" except for the first and last one.
<% If (rs_view.Fields.Item("CGPAOverall").Value>="2.00") Then
rs_view("Status")="Proceed"
Else
rs_view("Status")="Stop"
End If %>

If you are adding a new record, add a statement before you try to set the value of your fields
rs_view.AddNew()
rs_view("Status")="Stop"
rs_view.Update()
If you are editing an existing record, you need to call rs_view.Update() after you have set the values for your fields, you wish to change the value of.
You need a call to rs_view.Update(), to push the changes to the DB.
<% If (rs_view.Fields.Item("CGPAOverall").Value>="2.00") Then
rs_view("Status")="Proceed"
Else
rs_view("Status")="Stop"
End If
rs_view.Update() %>
Statements enclosed between <% %> will be executed on the server (IIS). I suggest, you read something on difference between server side code execution & the need/usage of <% %> and when to use it/not to use it.

Without a given error message it is hard to know where the problem is. Try this:
I assume your CGPAOverall column is stored in a numeric column. For this purpose it is safe to convert to double and compare against a number instead of a string.
<%
If cdbl(rs_view.Fields.Item("CGPAOverall").Value) >= 2 Then
rs_view("Status")="Proceed"
Else
rs_view("Status")="Stop"
End If
%>

It's impossible to solve your problem without telling us the error message.
One possibility: I see your recordset is called "rs_view." Not sure if "rs_view" is coming from a view, table, or stored procedure... but if it's coming from a view, not all views are updateable.
(Views are essentially canned select statements created from one or more tables and can contain various calculated columns. You can't save changes to those calculated columns back to the database because they're not actually columns in any table)

Related

Handling Chinese in ASP classic

I write the following piece of codes :
rst.Open(strSQL & Request.QueryString("C-PLACE")), conn, 0, 1
But got the following error. However, if the querystring is in English or just number, no error will pop out. Any guru can help please ?
Microsoft OLE DB Provider for ODBC Drivers error '80040e10'
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.
/deliverable/GetMemberTest.asp, line 19
It's going to either be passing an encoding variable to the server, or in the case of your error, its saying "too few parameters". In this case, the parameter is "C-PLACE" and its suppose to be passed to your asp script from the previous page's link, something like:
/deliverable/GetMemberTest.asp?C-PLACE=THECPLACE
https://www.w3schools.com/asp/coll_querystring.asp
(citation about query strings)
or something like that .. obviously its not actually "THECPLACE", but just saying a QueryString("VARIABLENAME") looks to the URL of the previous page to pass the parameter to the script, so that error message should of done something to add a ? mark = C-PLACE= to something, and we aren't seeing that. So something on the previous page that was suppose to add this when they click a submit button didn't do it's job, or the script is just getting run on its own without the proper previous page's work being done to prepare it to execute properly on the following page.
It will also be of note that these types of things are easily hacked through sql script injection, so if you aren't validating your url first, someone could use some code to escape out of your sql and add their own code, such as one to drop your tables ..., so make sure you validate the variable FIRST instead of dumping it straight into your code. I can give some guidance into that later, but first lets figure out your problem.
(side note - can i request strSQL from you? Can you put this line in before that line:
<%
response.write("strSQL is " & StrSQL & "<BR>")
%>
All this code does is display what is stored in the StrSQL variable, see if we can figure out what is going on here. Also take note that your error message indicated that it expected 2 parameters, so we are missing 2 for this script to run properly.
EDIT - try this encoding:
<%
Response.CodePage=65001
Response.Charset="UTF-8"
Response.ContentType = "text/html"
%>
Try this strSQL, you didn't need the Response.Write and on C-PLACE you want to use '' instead of "" because the "" will exit you out of the SQL statement. Try this, and let me know how it works, but I still think we are going to need another parameter supplied to it, unless its getting one from the string and then it isn't actually counting the one supplied from the url perhaps.
<%
strSQL="SELECT * FROM DetailMemberInfo
WHERE C-PLACE=" & strSQL & Request.QueryString('C-PLACE'))"
%>

ASP.NET code control is confusing

I found this on a project I'm working on:
<% If Session("VALUE1") <> "" Then %>
document.forms[0].action= "<%=Session("VALUE1")%>";
<% Else %>
document.forms[0].action="NewPage.aspx"
<% End If %>
When I single step through this starting at the top line, the code skips over the If Session("VALUE1") but also skips over the Else as well. How is this possible?
Inside both the If and Else blocks, there's no actual server code, only markup (which happens to be javascript). Since there's nothing to execute, your debugger doesn't have anything to stop at. So it's not actually skipping both of them.
If you look at the rendered output, one of the two will end up on the page.
The code isn't skipped, it's just that you don't see the actual code that is executed.
The code that is generated for that markup when the page is compiled looks something like this:
If Session("VALUE1") <> "" Then
Response.Write(" document.forms[0].action= """)
Response.Write(Session("VALUE1"))
Response.Write(""";")
Else
Response.Write(" document.forms[0].action=""NewPage.aspx""")
End If
As the Response.Write statements are generated and has no corresponding statements in the source code, it will look like they are skipped when you single step through the code.

How can i get the values of parameters from query string in aspx file?

Previously i passed the parameters to my page in a query string. Now, how am i supposed to get them and use them in my page ?
I tried this code and it doesn't work :
<asp:Label runat="server" ID="ShowParameter"><%# Request.QueryString["IDProduct"] %></asp:Label>
You need to use <%= Request.QueryString["IDProduct"] %>. # is used for databinding.
It also goes without saying that you should also check to make sure the query string is not null and that it actually exists. It is also generally not a good idea to directly output a query string like this for a number of reasons.

Does many include's (in "case") in ASP classic hurt the performance of the server?

I want to build this page with ASP classic:
<%
Dim depart
depart = 1556
Select Case depart
Case 1
%>
<!--#include virtual="/check/noam/newDesign/test1.asp"-->
<%
Case 2
%>
<!--#include virtual="/check/noam/newDesign/test2.asp"-->
<%
Case 3
%>
<!--#include virtual="/check/noam/newDesign/test3.asp"-->
<%
Case 4
%>
<!--#include virtual="/check/noam/newDesign/test4.asp"-->
<%
Case 5
%>
<!--#include virtual="/check/noam/newDesign/test5.asp"-->
<%
Case 6
%>
<!--#include virtual="/check/noam/newDesign/test6.asp"-->
<%
Case 7
%>
<!--#include virtual="/check/noam/newDesign/test7.asp"-->
<%
Case 8
%>
<!--#include virtual="/check/noam/newDesign/test8.asp"-->
<%
End If
%>
And I like to know if the server in the background need to enter to every include or he will enter only to the include in the right case?
I need to know that because I like to know if the server will get bad performance with this one.
It really depends on what is inside the includes but I wouldn't expect such a structure to have any observable impact on performance unless you either have 100s of case statements or have 100s of requests per second for the page.
It is true that all the includes will be composed into the script first before code is executed however it should also be remember that the composed and parsed "p-code" version of the final script is cached by ASP.
If the includes are primarly just static HTML content then this approach is actually quite efficient. On the other hand the more inline global identifiers (identifiers not enclosed in Sub, Function or Class) the more time needed to register these identifiers in the Script context (however there would still need to a lot of them to make a noticable difference).
A potential alternative is to use Server.Execute instead of an include. In this case the ASP executed has its own independant script context so it can't share the callers functions and variables (this may or may not be a good thing.) Also its quite possible that Server.Execute will actually turn out slower.
Includes are loaded before they're executed in classic ASP, so you're basically loading all of these includes, even if they're not executed.
You can use a different pattern: Execute Global, which is the equivalent of using eval() in VB. You read in the file as a string, then execute it. This gets around a cluster of includes but is rather ugly in itself.
You can use server.execute
server.execute("/check/noam/newDesign/test"&depart&".asp")
OR
page="/check/noam/newDesign/test"&depart&".asp"
server.execute(page)

Using ASP <% .... %> Tags

I have been searching for this for quite a while and couldn't find a solution...
I have my aspx file and in it a asp:SqlDataSource, where I want to get values which are equal to the Request.QueryString["key"]. I have defined a parameter for it but I can't find the right syntax to set the value.
Currently it is looking like this:
<SelectParameters>
<asp:Parameter Name="courseID" DefaultValue="<%= Request.QueryString["course_name"]
</SelectParameters>
where I always get the error, it is not well formed. What is the correct syntax, and is there an article how you use this <%.. %> commands?
There's an MSDN page that goes over what each tag is and what it does. Probably using <%...%> is not correct, as that's just a code tag. You want <%=...%> or <%:...%> which actually write values to the page.
But! Actually, if I'm reading what your problem is correctly, you want neither of those. For a SqlDataSource to pull in a query string value, you want to add a <SelectParameters> tag to the datasource, then add a <QueryStringParameter> to that.
Edit:
Yep, looking at the edit you just made, you definitely want a QueryStringParameter.
You can't use <%= within high-level asp controls. That construct writes directly to the output buffer, whereas the controls need to be processed first. In other words, rather than processing your <%= before expanding the control, it must first expand the control before it can process your <%=. They are at different levels of abstraction.
To do what you want to accomplish, rather than a plain <asp:Parameter> use an <asp:QueryStringParameter>. This will allow you to set the key you want to use from the query string.

Resources