9 Replies - 972 Views - Last Post: 20 October 2014 - 08:13 AM Rate Topic: -----

#1 ari03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 17-November 12

link combobox with sql column

Posted 19 October 2014 - 12:48 PM

hello everyone ,,
when i try to link combobox with sql column i get error !!
void comfunction() 
        {
            string constring = "Data Source=LC-VAIO\\SQLEXPRESS;Initial Catalog=sample1;Integrated Security=True";
            string query = "select * from tbltest";
            SqlConnection cn = new SqlConnection(constring);
            SqlCommand cmd = new SqlCommand(query,cn);
            SqlDataReader dreader;
            try
            {
                cn.Open();
                dreader= cmd.ExecuteReader();

                while (dreader.Read())

                {
                    string sname = dreader.GetString("name");
                    comboBox1.Items.Add(sname); 
                }
            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.Message);
            }
        }

i got error from "string sname = dreader.GetString("name"); "
and this how i try to link it so if you have any owther ways to do this it will be appreciated to let me know

Is This A Good Question/Topic? 0
  • +

Replies To: link combobox with sql column

#2 rusoaica   User is offline

  • They're watching you, Neo!
  • member icon

Reputation: 217
  • View blog
  • Posts: 689
  • Joined: 10-March 12

Re: link combobox with sql column

Posted 19 October 2014 - 12:50 PM

what is the error saying?
Was This Post Helpful? 0
  • +
  • -

#3 ari03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 17-November 12

Re: link combobox with sql column

Posted 19 October 2014 - 01:03 PM

thank you for you answers,, this is the error i got
[Error 1 The best overloaded method match for 'System.Data.Common.DbDataReader.GetString(int)' has some invalid ]
[Error 2 Argument 1: cannot convert from 'string' to 'int']
Was This Post Helpful? 0
  • +
  • -

#4 rusoaica   User is offline

  • They're watching you, Neo!
  • member icon

Reputation: 217
  • View blog
  • Posts: 689
  • Joined: 10-March 12

Re: link combobox with sql column

Posted 19 October 2014 - 01:06 PM

The error should be self explanatory: you are passing a string as an argument in a function that requires an integer. So, the function needs a number, not a string.

This post has been edited by rusoaica: 19 October 2014 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

#5 ari03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 17-November 12

Re: link combobox with sql column

Posted 19 October 2014 - 01:20 PM

so what should i change ?
Was This Post Helpful? 0
  • +
  • -

#6 rusoaica   User is offline

  • They're watching you, Neo!
  • member icon

Reputation: 217
  • View blog
  • Posts: 689
  • Joined: 10-March 12

Re: link combobox with sql column

Posted 19 October 2014 - 01:22 PM

first, you should show me how the function GetString looks like, so i can tell what you need.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is online

  • Entwickler
  • member icon

Reputation: 6602
  • View blog
  • Posts: 26,900
  • Joined: 12-December 12

Re: link combobox with sql column

Posted 19 October 2014 - 01:26 PM

    string sname = dreader.GetString("name");

The docs confirm that the argument to GetString is an integer, the ordinal number of the column.

If you want to use the column-name then you can use SqlDataReader.Item.

Item returns an Object so you probably need to convert it to a string. Perhaps
string sname = (string)(dreader.Item("name")); // or
string sname = string.Parse(dreader.Item("name"));

This post has been edited by andrewsw: 19 October 2014 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#8 ari03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 17-November 12

Re: link combobox with sql column

Posted 19 October 2014 - 01:36 PM

i have solved my problem by changing
 string sname = dreader.GetString("name");
comboBox1.Items.Add(sname);


to
 string sname = dreader.GetString(1);
comboBox1.Items.Add(sname);


and thank you for quick answers

This post has been edited by tlhIn`toq: 19 October 2014 - 04:32 PM
Reason for edit:: You can't apply bold and italics within code tags

Was This Post Helpful? 1
  • +
  • -

#9 andrewsw   User is online

  • Entwickler
  • member icon

Reputation: 6602
  • View blog
  • Posts: 26,900
  • Joined: 12-December 12

Re: link combobox with sql column

Posted 19 October 2014 - 01:44 PM

Glad you sorted, although it is preferable to use the name of the column when it is known (using Item). Relying on column order can prove problematic, and using the column-name makes the code easier to read.
Was This Post Helpful? 3
  • +
  • -

#10 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: link combobox with sql column

Posted 20 October 2014 - 08:13 AM

Relying on column order when also using a select * query is asking for all kinds of problems. I suggest following andrewsw's advice. I also suggest not using a select * query, but rather specifying which columns you want returned by your query, in what order. In fact, applications should almost never select *. Even if you're pulling all columns back, it doesn't take hardly any extra effort to type the whole query out, and that way you'll never get unexpected results if you change your table later down the road.

I also suggest you check the tutorial in my signature about querying databases. It'll show you how to filter your queries properly using prepared statements.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1