Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having two rows n the resultset after i am deleting one row using the ResultSet.deleteRow() method and moving to the previous row i am getting an error message "Invalid cursor state" pls help

Java
public void sales_reduce()
{
    boolean notify = true;
    try
        {
        int qty = 0,billed_qty = 0;
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:fraiserDB");
        Statement s = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        ResultSet rs;
        for(int i=0; i<50; i++)
        {
        rs = s.executeQuery("select billed_qty from sales_order,Grand_total_client where batch_no = '"+t.getValueAt(i, 1).toString()+"' and Grand_total_client.invoice_no = sales_order.invoice_no  and client_id = " + id);
        rs.afterLast(); //Every time move after the last position of resultset
        rs.previous(); // moves to the last data in the resultset
        qty = Integer.parseInt(t.getValueAt(i,5).toString());
        billed_qty = rs.getInt(1);
        while(qty != 0)
        {
        if(qty < billed_qty)
        {
            rs.updateInt("billed_qty", billed_qty-qty);
            rs.updateRow();
            JOptionPane.showMessageDialog(null, "Database updated","Database updated",JOptionPane.INFORMATION_MESSAGE);
            break;
        }
        else // if qty is greater than it will zero the current stock and
        {
            qty = qty - billed_qty;
            //rs.updateInt("billed_qty", 0);
            rs.deleteRow();
            rs.previous();
            billed_qty = rs.getInt(1); // Error coming in this row
        }
        }
        rs.close();
        }
        }
        catch(NullPointerException e) {}
        catch(ClassNotFoundException e)
        {
            JOptionPane.showMessageDialog(null, "Class Not Found","SQL Exception",JOptionPane.ERROR_MESSAGE);
        }
        catch(SQLException e)
        {
            JOptionPane.showMessageDialog(null, e.getErrorCode() +" "+ e.getMessage(),"SQL Exception",JOptionPane.ERROR_MESSAGE);
        }
    if(!notify)
        JOptionPane.showMessageDialog(null, "Sales order removed","Database Updated",JOptionPane.INFORMATION_MESSAGE);
}
Posted
Updated 8-Feb-12 23:01pm
v3
Comments
TorstenH. 9-Feb-12 5:26am    
are you messing around with the cursor? comment out all your custom cursor commands. See if it happens again.
Wasim007 13-Feb-12 1:49am    
yes i was pointing to the row in the database that was already deleted. i got the solution thank you very much bro.
TorstenH. 13-Feb-12 9:47am    
yo bro!

[OP Solved]

Was pointing to the cursor of the deleted record.
 
Share this answer
 
Hi
Add check whether previous record is available or not. it might be possible you are deleting a first record at that time previous is not available so you have to aa one extra check.
VB
rs.previous()


and also check whether RS is in open state or not. if it is in close state then also it throws error.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900