1

I am currently trying to populate a listview with some data I have pulled from my database table; but not sure where to start; I have tried the following:

lstData.DataSource = conn;
lstData.DataBind();

But that causes an error:

"Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource. MVC"

Am I using the correct query strings in order to populate my listview?

Thanks,

Callum

C# Code:

string ssConnectionString = "Server connection";
SqlConnection conn = new SqlConnection(ssConnectionString);
conn.Open();

SqlCommand command = conn.CreateCommand();
command.CommandText = "SELECT Category FROM [dbo].[Category] WHERE CategoryID = '16'";            

command.ExecuteNonQuery();          

string com = command.ExecuteScalar().ToString();
lblSQL.Text = com;
conn.Close();
3
  • 1
    Assigning an SqlConnection to .DataSource property is not logical. And your ExecuteNonQuery is meaningless. It doesn't do anything at all. What are you try to do exactly? And use using statement to dispose your database connections Commented Jun 28, 2014 at 9:58
  • I am trying to get the information I pull from the SqlConnectionString into a listview via different controls. Commented Jun 28, 2014 at 10:02
  • 1
    lstData.DataSource = conn; is wrong lstData.DataSource=aDataTable Commented Jun 28, 2014 at 10:35

1 Answer 1

1

Using your code as a base to start from you may want to try the following: I assume your connection in "Server connection" is a place holder for a real connection string and you know what should go there.

string ssConnectionString = "Server connection";
SqlConnection conn = new SqlConnection(ssConnectionString);
conn.Open();
SqlCommand command = conn.CreateCommand();
command.CommandText = "SELECT Category FROM [dbo].[Category] WHERE CategoryID = '16'";     
SqlDataAdapter da = new SqlDataAdapter(command); 
DataTable dataTable;
da.Fill(dataTable);   
lstData.DataSource = dataTable;
lstData.DataBind();
conn.Close();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.