8 Replies - 1602 Views - Last Post: 31 May 2010 - 11:07 AM Rate Topic: -----

#1 JamieY   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 06-May 10

help with combo box

Posted 30 May 2010 - 08:42 PM

I have my database set up. I have a combo box on form two that I want to populate with the values of firstName and lastName from the fields in my database. I have the combo box populating with firstName right now. How can I get both names in there? I tried using the query builder to pull from both, but I get an error every time that says

The schema returned by the new query differs from the base query
Is This A Good Question/Topic? 0
  • +

Replies To: help with combo box

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: help with combo box

Posted 30 May 2010 - 09:03 PM

SELECT firstName + ' ' + lastName FROM sometable WHERE somecondition ORDER BY somefield
Was This Post Helpful? 1
  • +
  • -

#3 JamieY   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 06-May 10

Re: help with combo box

Posted 30 May 2010 - 09:11 PM

View PostMomerath, on 30 May 2010 - 08:03 PM, said:

SELECT firstName + ' ' + lastName FROM sometable WHERE somecondition ORDER BY somefield



I still get the same error. I clicked on Add Query and typed this in

SELECT firstName + ' ' + lastName FROM dbo.users

still get the same error

I found another site that posted a reply 3 years ago and he said his solution was to

I had to change the properties in ‘edit dataset with designer’ to your scheme and it worked.

I think the scheme is adding the + ' ' + but I can't find where to do that
Was This Post Helpful? 0
  • +
  • -

#4 Searock   User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 259
  • Joined: 15-September 09

Re: help with combo box

Posted 30 May 2010 - 11:49 PM

Can you post your code so that we can help you to fix your code?
Was This Post Helpful? 0
  • +
  • -

#5 FlashM   User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: help with combo box

Posted 31 May 2010 - 12:04 AM

This things are easier to do without wizards and dataset designers... Code your SQL statements in your code. I find this procedure easier and cleaner... Dataset designers might sometimes be confusing...
Was This Post Helpful? 0
  • +
  • -

#6 JamieY   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 06-May 10

Re: help with combo box

Posted 31 May 2010 - 10:08 AM

I wasn't using code. I was using the dataset stuff. Trying to use the wizards to make it work. I think it might be better to use code for the query. I will try that.
Was This Post Helpful? 0
  • +
  • -

#7 JamieY   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 06-May 10

Re: help with combo box

Posted 31 May 2010 - 10:43 AM

View PostJamieY, on 31 May 2010 - 09:08 AM, said:

I wasn't using code. I was using the dataset stuff. Trying to use the wizards to make it work. I think it might be better to use code for the query. I will try that.



Okay, I tried code but it isn't working. the box is empty. Of course I also do not know what I am doing.

[code]


private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string connectionString = (@"Data Source=.\SQLEXPRESS;AttachDbFilename='C:\My Documents\database.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlConnection connection = new SqlConnection(connectionString);

string sqlQuery = "SELECT firstName, lastName FROM dbo.users;";

SqlCommand command = new SqlCommand(sqlQuery, connection);
connection.Open();

SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
comboBox1.Items.Add(sqlQuery.ToString());
}

// Call Close when done reading.
reader.Close();
}
[code]

I got part of this code from another website. I am new to C# and have never used it to do this kind of thing before. So if you asnwer, please make your response as simple as possible. I tried doing it this way because I know the connection works. I don't know any other way to connect to the database. The combo box is in form2. On form one, I have the connectionString and it has a form that allows me to enter first and last name into the database.
Was This Post Helpful? 0
  • +
  • -

#8 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: help with combo box

Posted 31 May 2010 - 10:59 AM

In this line:
comboBox1.Items.Add(sqlQuery.ToString());



You are only adding the actual query string (meaning: "select blah blah blah").

What you are trying to do is read information from the returned SQL command.
Was This Post Helpful? 0
  • +
  • -

#9 JamieY   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 06-May 10

Re: help with combo box

Posted 31 May 2010 - 11:07 AM

okay. then what should i put there?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1