-1

Im trying to fill listView1 with the first table of my database. But it is not working, can someone help me?

The function LoadList is not recognizing the DataTable DT

Here is the code I am using

namespace Project.Data_Layer
{
    class Leerling
    {
        public static DataTable ExecSelect(string Leerling)
        {
            DataSet DS = new DataSet();
            SqlDataAdapter DA;
            DA = new SqlDataAdapter(Leerling, SQLString.Conn);
            DS.Clear();
            DA.Fill(DS);
            // 1e tabel uit database
            DataTable DT = DS.Tables[0];
            return DT;
        }

        public static bool ExecOverig(string Leerling)
        {
            bool gelukt = true;
            SqlConnection Connect = new SqlConnection(SQLString.Conn);
            SqlCommand Cmd = new SqlCommand(Leerling, Connect);
            try
            {
                Cmd.Connection.Open();
                Cmd.ExecuteNonQuery();
                Connect.Close();
            }
            catch
            {
                gelukt = false;
            }
            return gelukt;
        }

        public void LoadList()
        {
            if(DT.tables.count > 0)
            {
                listView1.DataSource = DT;
                listView1.DataBind();
            }
            else
            {
                //
            }
        }
    }
}
Jari PSV
  • 11
  • 2
  • 2
    Describe *not working*? – Liam Nov 15 '17 at 11:34
  • Also see [What is the C# Using block and why should I use it?](https://stackoverflow.com/questions/212198/what-is-the-c-sharp-using-block-and-why-should-i-use-it) – Liam Nov 15 '17 at 11:35
  • 3
    Well, you really need to read some basic tutorial on C#, in particular you should read about [variable scope](https://msdn.microsoft.com/en-us/library/ms973875.aspx) – Steve Nov 15 '17 at 11:36
  • How would that LoadList know about a DT? Where did you define it, where did you fill it? (you fill "a" DT variable in ExecSelect but that is local to that method, and you never call that method anyway) – Hans Kesting Nov 15 '17 at 11:37
  • Do not mix Dutch with English in your code Jari, -only- use English unless no other way possible. Trust me, it will prevent unnecessary complications. – MwBakker Nov 15 '17 at 11:58

2 Answers2

1
[enter image description here][1]
string sel = "select * from EmployeeMaster";
        DataTable dt = new DataTable();
        dt = con.filldata(sel);
        foreach(DataRow r in dt.Rows)
        {
            listView1.Items.Add(r["E_Name"].ToString());
            listView1.Items.Add(r["Salary"].ToString());

        }

  [1]: https://i.stack.imgur.com/72fmI.png
0

Declare the DataTable variable DT outside of method 'ExecSelect'

DataTable DT = new DataTable();    
public static DataTable ExecSelect(string Leerling)
    {
        DataSet DS = new DataSet();
        SqlDataAdapter DA;
        DA = new SqlDataAdapter(Leerling, SQLString.Conn);
        DS.Clear();
        DA.Fill(DS);
        // 1e tabel uit database
        DT = DS.Tables[0];
        return DT;
    }
Moorthi Daniel
  • 157
  • 2
  • 5