Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why the data is not fetching by this program.I want to fetch only four columns data which points is greatest among all points.

ERROR- Index Out Of Range was unhandled by user

Error Line is - TextBox4.Text = dRow.ItemArray.GetValue(4).ToString();

My CS COADING IS GIVEN BELOW

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;


public partial class Retrive_Data_From_Database_and_display_in_TextBox : System.Web.UI.Page
{
    OleDbConnection con;
    OleDbCommand com;
    OleDbDataAdapter da;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new System.Data.OleDb.OleDbConnection();
        ds = new DataSet();
        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/kamnagroup.mdb") + "";
        string sql = "select aname,add1,photo,point from joining WHERE point=(SELECT MAX(point) FROM joining)";
        da = new System.Data.OleDb.OleDbDataAdapter(sql,con);
        con.Open();
        da.Fill(ds,"joining");
        navigatedata();
        con.Close();
        con.Dispose();
    }
    private void navigatedata()
    {
        DataRow dRow=ds.Tables["joining"].Rows[0];
        
        TextBox1.Text = dRow.ItemArray.GetValue(1).ToString();
        TextBox2.Text = dRow.ItemArray.GetValue(2).ToString();
        TextBox3.Text = dRow.ItemArray.GetValue(3).ToString();
        TextBox4.Text = dRow.ItemArray.GetValue(4).ToString();
        Image1.ImageUrl = TextBox3.Text;
        
    }
}
Posted
Updated 24-Jan-12 22:11pm
v3

//Correct code is.........
C#
private void navigatedata()
      {
            DataRow dRow=ds.Tables["joining"].Rows[0];

            TextBox1.Text = dRow.ItemArray.GetValue(0).ToString();
            TextBox2.Text = dRow.ItemArray.GetValue(1).ToString();
            TextBox3.Text = dRow.ItemArray.GetValue(2).ToString();
            TextBox4.Text = dRow.ItemArray.GetValue(3).ToString();
            Image1.ImageUrl = TextBox3.Text;

      }
 
Share this answer
 
Comments
Janardan Pandey 25-Jan-12 4:43am    
But the image is not displaying in Image1 field.Please help me to rectify.
<pre lang="c#">

private void navigatedata()
{
DataRow dRow=ds.Tables["joining"].Rows[0];

TextBox1.Text = dRow[0].ToString();
TextBox2.Text = dRow[1].ToString();
TextBox3.Text = dRow[2].ToString();
TextBox4.Text = dRow[3].ToString();
Image1.ImageUrl = TextBox3.Text;

}
</pre>
Try this I am sure this will work.. :)
 
Share this answer
 
Comments
Janardan Pandey 25-Jan-12 4:58am    
not working
What is the value returned by each call to GetValue(x), is it valid or not? Also what is the value stored in Image1.ImageUrl? Try using your debugger to step through the code and check each value as you go to see exactly why it is failing. Just saying "not working" is not a very useful comment.
 
Share this answer
 
Comments
Janardan Pandey 25-Jan-12 5:25am    
I checked the value is comming correct.The Image1.ImageUrl is also taking it's path but image is not comming only a icon visibled indicating a image there.
Richard MacCutchan 25-Jan-12 5:29am    
but image is not comming
Coming where? Please think about what should be happening and explain with the code that is in your program. Your original problem was concerned with an object reference being incorrect, but now you are talking about something not being displayed.
Janardan Pandey 25-Jan-12 6:02am    
Ok i am not about to explain about this so ggive a small look on my HTML and CS coading given below.


HTML PART

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Image ID="Image1" runat="server" ImageUrl="~/blank.jpg" />
<br />
<br />
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server">
<br />
<br />
<br />
<br />
<asp:TextBox ID="TextBox2" runat="server">
<br />
<br />
<br />
<asp:TextBox ID="TextBox3" runat="server">
<br />
<br />
<asp:TextBox ID="TextBox4" runat="server">
</div>
</form>
</body>
</html>


CS PART


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;


public partial class Retrive_Data_From_Database_and_display_in_TextBox : System.Web.UI.Page
{
OleDbConnection con;
OleDbCommand com;
OleDbDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
con = new System.Data.OleDb.OleDbConnection();
ds = new DataSet();
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/kamnagroup.mdb") + "";
string sql = "select aname,add1,photo,point from joining WHERE point=(SELECT MAX(point) FROM joining)";
da = new System.Data.OleDb.OleDbDataAdapter(sql,con);
con.Open();
da.Fill(ds,"joining");
navigatedata();
con.Close();
con.Dispose();
}
private void navigatedata()
{
DataRow dRow=ds.Tables["joining"].Rows[0];

TextBox1.Text = dRow.ItemArray.GetValue(0).ToString();
TextBox2.Text = dRow.ItemArray.GetValue(1).ToString();
TextBox3.Text = dRow.ItemArray.GetValue(2).ToString();
TextBox4.Text = dRow.ItemArray.GetValue(3).ToString();
//Image1.ImageUrl = TextBox3.Text;
Image1.ImageUrl = dRow.ItemArray.GetValue(2).ToString();

}
}
Richard MacCutchan 25-Jan-12 6:15am    
Sorry, but I'm not an ASP.NET expert so most of the above does not mean anything to me.
Janardan Pandey 25-Jan-12 6:22am    
ok, But nice to help me.

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