Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys, I have got this code below which should on SQLCommand Select column pocsed from table busnaz, and select the value of pocsed which is in the same row as selected value of combobox(which is in column nazev). And insert this value into textbox klimax.

I hope someone will help me find the error, thanks!

C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {


            try
            {
                SqlCommand novahodnota = new SqlCommand("...", spojeni);
                spojeni.Open();
                SqlDataReader precti = novahodnota.ExecuteReader();

                precti.Read();
                zname.Text = precti.GetDecimal(2).ToString();
                spojeni.Close();
                
            }
            catch (Exception ex)
            {
                //something
            }
        }
Posted
Updated 15-Oct-13 4:23am
v3
Comments
[no name] 9-Jul-13 10:04am    
What error? Where is "@selecteditem" defined?
ZurdoDev 9-Jul-13 10:12am    
Sorry, I can't follow what you need. It seems like it should be easy but I can't understand you. If you want a different column just do precti[columnName]
saguptamca 9-Jul-13 10:16am    
Not clear at all

Considering your SELECT is only returning one column, your call to GetDecimal(2) is assuming that there is at least 3 columns being returned, numbered 0, 1 and 2. You're trying to return a value from a column that doesn't exist in the resultset.
 
Share this answer
 
I am not 100% sure on this, but I would change SqlCommond to:

"SELECT pocsed FROM busnaz WHERE nazev=" + comboBox1.SelectedIndex But I am not sure what nazev is?

else ComboBox1.SelectedValue?
 
Share this answer
 
Comments
Dave Kreskowiak 9-Jul-13 10:55am    
That returns a different row, not the value of a different column.
Jan Lehmkuhl 9-Jul-13 11:24am    
Hi Dave - no it doesn't necessarily, because I don't know the layout of the table. I believe nazev could be index... so if index remains the same as that selected in the combobox then item remains the same? He did not specify the error, but the "@selectedItem" seemed out of place. But as I said I am not 100% sure cause I did not know the exact error/ datatable
Dave Kreskowiak 9-Jul-13 11:36am    
Yea, I'm just going by the title of the post and what the code says...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900