Click here to Skip to main content
15,908,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, i am binding in gridview from database.

here i need to use attributes and arraylist. to get like dictionary.

i will give my coding with explain:
C#
SqlCommand cmd = new SqlCommand("select * from mylist where lsname is not null", con);

SqlDataAdapter adap = new SqlDataAdapter(cmd);
DataTable dtmylist = new DataTable();

Note : //the result of select query : 2 rows :

lsname qty
arun 5
bala 6

SqlDataReader dr = cmd.ExecuteReader();
string lsname = string.Empty; int qty = 0;
while(dr.Read())

{

lsname = dr["db_lname"].ToString();
qty = dr["db_qty"].ToString();
arr.at_lname = lsname; // can you see i used attributes to get two(lsname,qty) record in one attributes : 
arr.at_qty = qty; 
order.al.Add(arr); //so now order is class name and al is object of arraylist. so, now order.al[position is 0 and count is 1) is having arr's(attributes values)

}
C1GridView1.DataSource = order.al;
C1GridViewlist.DataBind();<pre>
but the output in gridview is showing
<pre lang="text">lsname qty
bala 6

so, how to show all rows which comes from database in gridview.
Note: i want to use attributes and arraylist as i used above to get 2 records(lsname,qty) in one.
i mean : a[0]={lsname(arun),qty(5)}
and a[1]={lsname{bala),qty(6)}.

help needed.
Posted
Updated 14-Dec-13 4:01am
v2

Well, look at what you are doing, and it's pretty obvious if you stop and think about it: how many different objects are you adding to order.al?

Surprisingly, it's not two. It's only one, just you try and add it with different values each time.
Think of it this way: if you look at your wallet, it's the same wallet even if you take some money out or put some more in - the value of the wallet changes, but the physical wallet doesn't.

The same thing happens here: you change the values in the "wallet":
C#
arr.at_lname = lsname;
arr.at_qty = qty;
But you always add the same "wallet" to the list:
C#
order.al.Add(arr);

What you need to do is create a new "wallet" each time round the loop. I can't tell you how exactly, because I don't have the part of your code where you create this one, but it's probably along the lines of
C#
arr = new MyClassForArr();
Use your class name, and add that to the top of your while loop.
 
Share this answer
 
First of all doing all this is unnecessary. As you have used SqlDataAdapter, so you can directly Fill the DataTable and assign that to DataSource like below.
C#
adap.Fill(dtmylist);
C1GridView1.DataSource = dtmylist;


The code you have used is updating the same object, so always last row will be added to object.
Refer OriginalGriff's answer for more information.
 
Share this answer
 
v2
hi thanks a lot for all. i fixed the above issue.

i made mistake that i declared attributes globally (outside of while loop). i declared attributes locally inside of while loop. now it works correctly.
 
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