Results 1 to 7 of 7

Thread: Fill a Listview with the SQLdatareader

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2002
    Location
    Helsingborg,Sweden
    Posts
    35

    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

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    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

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2002
    Location
    Helsingborg,Sweden
    Posts
    35
    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.

  4. #4
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    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

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2002
    Location
    Helsingborg,Sweden
    Posts
    35
    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

  6. #6
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    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); 
    }

  7. #7
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472
    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
  •  



Click Here to Expand Forum to Full Width