You must Sign In to post a response.
  • Category: ASP.NET

    Filling data in DataTable using OracleDataAdapter

    Hi Guys,

    I Am filling data in a datatable using OracleDataAdapter. I am assigning this datatable to gridview The code is below,

    OracleDataAdapter dAdapter = new OracleDataAdapter(cmd);
    DataTable dtReslt = new DataTable();
    dAdapter.Fill(dtReslt);

    The issue is , the first time when i click the particular even where above code fires, i get an issue "Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index" . But when i click again , the data loads correctly. Some times it does not work at second click but works at third click.

    I am getting the issue exactly in the code "dAdapter.Fill(dtReslt); "

    Help me to resolve.

    Regards,
    Rajabharathi R.
  • #761034
    Hi
    raja

    Your Answer have this statement .

    Must be non-negative and less than the size of the collection

    Your datatable did not comes the records where you get datatable you need use record count then you can overcome this issue use this code


    System.Data.DataTable dt = new System.Data.DataTable();

    if (dt.Rows.Count > 0)
    {
    //Code here
    }


    for in your issue i alter your code try and use them


    //Your Code

    OracleDataAdapter dAdapter = new OracleDataAdapter(cmd, "Mention your Connection");
    DataTable dtReslt = new DataTable();
    dAdapter.Fill(dtReslt);

    //I altered Code
    DataTable dtReslt = new DataTable();
    SqlConnection con = new SqlConnection(strCon);
    con.Open();
    SqlDataAdapter sqlcm = new SqlCommand("Select * from TestRecord", con);
    sqlcm.Fill(dtReslt);

    if (dtReslt.Rows.Count > 0)
    {
    //Code here
    }

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761038
    Hi Kumar,

    After filling the datatable , ie after the code,
    dAdapter.Fill(dtReslt);
    i use the condition if (dtReslt.Rows.Count > 0).
    But problem is i get the error in the code dAdapter.Fill(dtReslt);

    Regards,
    Rajabharathi R

  • #761040
    Hi
    Raj

    Can you share your issue in snapshots or share your code.

    Where did you download and install in oracle for dotnet?

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761049
    Hi Raj,

    i think check once your Query in Oracle whether it is working fine or not. If it is working fine than there might be issue are you using a stored procedure or inline query ?

    SRI RAMA PHANI BHUSHAN KAMBHAMPATI

  • #761051
    'Index was out of range' thrown by compiler when there is mismatch in mapping number of rows, check your sql query and see what it returns after fire a sql query
    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #761053
    Hello Rajabharathi,

    Refer the below code :

    try
    {
    DataTable dt = new DataTable();
    OracleConnection oraConn = new OracleConnection("Your connection string");
    oraConn.Open();
    OracleDataAdapter oraAdapt = new OracleDataAdapter("Your valid query", oraConn);
    oraAdapt.Fill(dt);
    if(dt.Rows.Count > 0)
    {
    GridView1.DataSource = dt;
    }
    else if (dt.Rows.Count <= 0)
    {
    GridView1.DataSource = null;
    }
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.StackTrace);
    }
    finally
    {
    oraConn.Close();
    }

    Hope this will work for you.
    Mark the answer if it helped you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #761058
    Hi BHUSHAN KAMBHAMPATI,

    I use inline query

    Regards,
    Rajabharathi R

  • #761070
    Hi bharati,

    Is it working in Oracle Editor. are you using Windows Application or Webbased Application, If Windows Probably your Predetermined Map Column Fields are not Matching it Once Check the Column Fields...
    for ex:- like the Below if you are using windows Application..
    objCol.HeaderText = "Charge Cust";//1
    objCol.MappingName = "ChargeCust";

    SRI RAMA PHANI BHUSHAN KAMBHAMPATI

  • #761149
    Hai Rajabharathi,
    It seems that the problem in the connection with the Oracle.
    You can write a condition, if the connection is null then it should first get the connection and then try. Because if the connection is not established with the correct database, it will not get anything.

    -- check for the connection and get the connection
    -- Create the DataAdapter object and pass the command with the connection
    -- Fill the dataset based on the adapter query

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments