views:

100

answers:

4

Hi there,

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.

A: 

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.

shahkalpesh
+2  A: 

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 %>
mga911
A: 

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
%>
Michal
A: 

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)

John Booty