Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Store procdure

if(@Activity = 'All')
select top 1 FullName,Email,Pasword,imgurl registration order by id desc
End

in UserDal

public List<user>All(ActivityMode a)
{
SqlConnection con = ConnectionDAL.SQL();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = &amp;quot;Sp_User&amp;quot;;
cmd.Parameters.AddWithValue(&amp;quot;@activity&amp;quot;, a.ToString());
List&amp;lt;User&amp;gt; lst = null;
con.Open();
using (con)
{
SqlDataReader DR = cmd.ExecuteReader();
if (DR.HasRows)
{
lst = new List&amp;lt;User&amp;gt;();
while (DR.Read())
{
User obj = new User();
obj.Id = Convert.ToInt32(DR[&amp;quot;ID&amp;quot;]);
obj.FullName = DR[&amp;quot;FullName&amp;quot;].ToString();
obj.Email = DR[&amp;quot;Email&amp;quot;].ToString();
obj.Password = DR[&amp;quot;Password&amp;quot;].ToString();
obj.Address = DR[&amp;quot;Address&amp;quot;].ToString();
obj.Imgurl = DR[&amp;quot;Imgurl&amp;quot;].ToString();
lst.Add(obj);
}
}
}
return lst;
}&lt;/pre&gt;</pre>
,

What I have tried:

i`m running the project the show the error..

how to resolve..
i am only disply last record..plz help me..


Server Error in '/' Application.

ID

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: ID

Source Error:


Line 90: {
Line 91: User obj = new User();
Line 92: obj.Id = Convert.ToInt32(DR["ID"]);
Line 93: obj.FullName = DR["FullName"].ToString();
Line 94: obj.Email = DR["Email"].ToString();
Posted
Updated 9-Mar-16 17:04pm
Comments
[no name] 11-Mar-16 2:35am    
Try changing your sp. as instructed by "AnvilRanger".

1 solution

Look at your stored procedure. You are not selecting the ID column.

Edit
That is not how it works around here. Code Project members will not do your work for you. We take our personal time to help people on this forum, and we are not paid for anything. We do this because we love development and what to help others with their development issues. I know nothing about your system, what you are trying to accomplish, expect for wanting the last record. You are best person to complete your task. If I were to do you work for you, what would you learn. Nothing. Even asking me to write a complete solution is very rude.

The solution to your problem is very simple. All you need to do is look at the error message and your code. You error message clearly tells you that you are trying to reference a item that does not exists, in this case ID. And you can see from your code that you are trying to set obj.Id = Convert.ToInt(DR["ID"]). That is where your error is coming from.

Now look at your stored procedure. In you select list,FullName,Email,Pasword,imgurl, you are selecting 4 items, but one of those is not ID.
 
Share this answer
 
v2
Comments
Mehak Naaz 10-Mar-16 1:15am    
plaese create full project using ado net ..display the last record
AnvilRanger 10-Mar-16 7:59am    
Please see the updated solution.

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