question

BenTam-3243 avatar image
0 Votes"
BenTam-3243 asked BenTam-3243 commented

Populating objectListView's cells with SQL Server data

Dear All,

How can I populate objectListView's cells with SQL Server data?

TIA

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered BenTam-3243 commented

You can use a DataListView, inherited from ObjectListView

I have no SQL Server to test, but I did a test with an Access database
You can adapt it by changing the connexion string

 // using System.Data.OleDb;
 string sFile = "E:\\Employees.accdb";
 string sSql = "SELECT FirstName, LastName, Location FROM employees";
 //string sSql = "SELECT * FROM employees";
 OleDbCommand myCommand = new OleDbCommand();
 OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + sFile + ";Persist Security Info=False;Jet OLEDB:Database Password='toto';");
 OleDbDataAdapter dataadapter = new OleDbDataAdapter(sSql, myConnection);
 DataSet ds = new DataSet();
 myConnection.Open();
 dataadapter.Fill(ds, "employees_table");
 myConnection.Close();
 dataListView1.DataSource = ds;
 dataListView1.DataMember = "employees_table";
 dataListView1.AutoResizeColumns();
 dataListView1.ShowGroups = false;

130324-datalistview.jpg



datalistview.jpg (40.4 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Castorix31,

Thanks for your reply. I'll try it.

0 Votes 0 ·
BenTam-3243 avatar image
0 Votes"
BenTam-3243 answered Castorix31 commented

Hi @Castorix31

The connection string for Access is
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + sFile + ";Persist Security Info=False;Jet OLEDB:Database Password='toto';"

Could you tell me the connection string for SQL Server 2016?

Thanks!

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

On the site I posted there are some connexion strings for different SQL Server versions : Microsoft OLE DB Driver for SQL Server connection strings


0 Votes 0 ·

Hi @Castoric31,

I've followed your code and modified the connection string to make it connect to SQL Server. However the Student_dataListViews were red lined. The only difference was the connection string. May I know how to fix it?

132296-sqlconnect.gif


0 Votes 0 ·
sqlconnect.gif (33.1 KiB)
Castorix31 avatar image Castorix31 BenTam-3243 ·

Don't prefix it with the Form class name

0 Votes 0 ·
BenTam-3243 avatar image
0 Votes"
BenTam-3243 answered BenTam-3243 commented

Hi @Castorix31,

Thanks for your reply.

I have tried it. However the red lines moves to Student_dataListView. I wonder if this is not the true dataListView.
At a time, when I draw a dataListView, it appears grey in the middle. There are 2 extra links when I click at the top-right triangle. I redraw many times but it can't be reproduced.

132453-sqlconn.gif
132434-student-form.gif



sqlconn.gif (31.2 KiB)
student-form.gif (33.2 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If it is created with the Designer (this.Student_dataListView = new BrightIdeasSoftware.DataListView(); , etc...)
remove the static keyword in your function

0 Votes 0 ·

Hi @Castorix31

The red lines are removed. Thank you very much!

0 Votes 0 ·