Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Select Distinct certain fields using SQLDataReader

Avatar of ttphil
ttphil asked on
Microsoft SQL Server.NET Programming
11 Comments1 Solution1925 ViewsLast Modified:
Hi,

I am trying to figure out how to pull in data from an SQL DB without duplicate names.
The fields im using are fname, sname, company and notes.
I use SELECT DISTINCT to  pull in data to a datagrid where sname is in a textbox. I only want it to return 1 instance of each fname, sname and company and to return only the most recent notes instance. There can be many rows with the same fname, sname and company, but at the moment, its including the notes too so I get more than one result.

How might i go about doing this?

Hope this is clear, ive posted my code below.


string sSelectSQL = "SELECT DISTINCT fname, sname, company, notes FROM Main WHERE sname like '" + snametxt.Text + " %' ORDER BY fname ASC";
                    SqlCommand oCommand = new SqlCommand(sSelectSQL, oConnection);
                    oConnection.Open();
                    oReader = oCommand.ExecuteReader();
 
                    
                    DataSet ds = new DataSet();
                    DataTable dt = new DataTable("Table1");
 
                    ds.Tables.Add(dt);
                    ds.Load(oReader, LoadOption.PreserveChanges, ds.Tables[0]);
                    datagridview1.DataSource = ds.Tables[0];
 
                    oReader.Close();
                    oConnection.Close();
ASKER CERTIFIED SOLUTION
Avatar of reb73
reb73Flag of Ireland image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answers