954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Storing Number in an integer from sql Database

i am using database with table RESUME and column PageIndex in it which type is number in database but when i want to store this PageIndex value to an integer i get exception error

**Specified cast is not valid.**

here is the code

string sql;
    string conString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\\Deliverable4.accdb";
    protected OleDbConnection rMSConnection;
    protected OleDbDataAdapter rMSDataAdapter;
    protected DataSet dataSet;
    protected DataTable dataTable;
    protected DataRow dataRow;


on Button Click

sql = "select PageIndex from RESUME";
    rMSConnection = new OleDbConnection(conString);
    rMSDataAdapter = new OleDbDataAdapter(sql, rMSConnection);
    dataSet = new DataSet("pInDex");
    rMSDataAdapter.Fill(dataSet, "RESUME");

    dataTable = dataSet.Tables["RESUME"];

    

int pIndex = (int)dataTable.Rows[0][0];

    rMSConnection.Close();
    
    if (pIndex == 0)
    {            
        Response.Redirect("default.aspx");
    }
    else if (pIndex == 1)
    {            
        Response.Redirect("default-2.aspx");
    }
    else if (pIndex == 2)
    {          
            Response.Redirect("default-3.aspx");
        }
    }


i am getting error in this line

int pIndex = (int)dataTable.Rows[0][0];
ar31an
Newbie Poster
14 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

try

int pIndex = int.Parse(dataTable.Rows[0][0].ToString());
finito
Nearly a Posting Virtuoso
1,321 posts since May 2010
Reputation Points: 60
Solved Threads: 135
 

now i get this exception

Input string was not in a correct format.

i need to store int not string can u please change the code for integer

ar31an
Newbie Poster
14 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

thanks finito it works now thanks very much

ar31an
Newbie Poster
14 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

nps your welcome please mark as solved.

finito
Nearly a Posting Virtuoso
1,321 posts since May 2010
Reputation Points: 60
Solved Threads: 135
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You