|
-
Sep 12th, 2004, 09:51 AM
#1
Thread Starter
Member
Fill a Listview with the SQLdatareader
Hi!
I need help with a listview..
My Question is how do i fill a listview with the SQLDataReader
I have gor so far as this
Code:
string ConnectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DVDFilmer;Data Source=TYRONNE;";
string SQL ="Select * from Film";
SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand(SQL,con);
con.Open();
SqlDataReader rst;
rst = cmd.ExecuteReader();
while (rst.Read())
{
ListViewItem item1 = new ListViewItem("Ett",0);
item1.SubItems.Add("2");
item1.SubItems.Add("3");
}
con.Close();
So how do i add the Values from the reader into the listview.
regards!
Tyson
-
Sep 12th, 2004, 10:02 AM
#2
<?="Moderator"?>
try
Code:
while (rst.Read())
{
ListViewItem item1 = new ListViewItem("Ett",0);
item1.SubItems.Add(rst.GetString(1)); // where 1 is the column number
//or
item1.SubItems.Add( (String)rst["ColumnName"] );
}
That is what i used with MySQLDriverCS.
Hope it Helps
-
Sep 12th, 2004, 01:10 PM
#3
Thread Starter
Member
ok.. thx!
but i got an error now
I am using on of youre example, but i got an error i the first row
Code:
{
ListViewItem item1 = new ListViewItem(rst.GetString(0),0);
}
the message is "Specified cast is not vaild".
I am using an bigint datatype in that Colum in my DB
Whats wrong?
/tyson
Last edited by tyson; Sep 12th, 2004 at 01:15 PM.
-
Sep 13th, 2004, 05:06 AM
#4
<?="Moderator"?>
Ok Here are the methods of the data reader.
Method Returns
----------------------------------------------------------
GetBoolean - A Boolean Values
GetByte - A Byte
GetBytes - An Array of Bytes
GetChar - A Character Values
GetChars - An Array of Characters
GetDataTypeName - The Name Of The Source's Data Type
GetDateTime - A DateTime object
GetDecimal - A Decimal Value
GetDouble - A Double Value
GetFieldType - A Type Value
GetFloat - A Float Type
GetGuild - A Guild Object
GetInt16 - A Short Value
GetInt32 - An Integer Values(int)
Get64 - A Long Value
GetName - The Column's Name
GetOrdinal - The Ordinal, When Passes The Column Title
GetSchemaTable - A DataTable Object
GetString - A String Value
GetTimeSpan - A TimeSpan Object
GetValue - The Value
GetValues - Gets All The Attribute Values In The Current Row
Just pic the method for your datatype. Hope it help
-
Sep 13th, 2004, 06:45 AM
#5
Thread Starter
Member
ok!
Just for trying i used this code
Code:
private void List_Films()
{
ListView listView1 = new ListView();
string ConnectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DVDFilmer;Data Source=TYRONNE;";
string SQL ="Select * from Filmer";
SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand(SQL,con);
con.Open();
SqlDataReader rst;
rst = cmd.ExecuteReader();
while (rst.Read())
{
ListViewItem item1 = new ListViewItem(rst[0].ToString(),0);
item1.SubItems.Add(rst.GetString(1));
item1.SubItems.Add(rst.GetString(2));
item1.SubItems.Add(rst.GetString(4));
}
con.Close();
}
And when i run the app the listvew is empty
The view is set to detail. i have configure the columns
Whats wrong?
Tyson
-
Sep 13th, 2004, 11:13 AM
#6
<?="Moderator"?>
You need to let the listview to add the items to the list, try this
Code:
while (rst.Read())
{
ListViewItem item1 = new ListViewItem(rst[0].ToString(),0);
item1.SubItems.Add(rst.GetString(1));
item1.SubItems.Add(rst.GetString(2));
item1.SubItems.Add(rst.GetString(4));
listView1.Items.Add(item1);
}
-
Sep 21st, 2004, 03:37 AM
#7
Hyperactive Member
And when i run the app the listvew is empty
The view is set to detail. i have configure the columns
Whats wrong?
hi there...
does the columns equivalent to the subitems to add? because what happens their if you set the listview to detail and no columns, the listview will be empty, be sure to set how many columns.
or try this:
hope it helps...
Code:
private void button1_Click(object sender, System.EventArgs e)
{
SqlConnection cn=new SqlConnection("user id=sa;password=password;initial catalog=northwind");
String cmdText = "select * from Orders";
SqlCommand cm=new SqlCommand(cmdText,cn);
cn.Open();
SqlDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
ListViewItem li = listView1.Items.Add(dr.GetValue(0).ToString());
li.SubItems.Add(dr.GetValue(1).ToString());
li.SubItems.Add(dr.GetValue(2).ToString());
////add here on how many subitems items to add
}
dr.Close();
cn.Close();
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|