Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, i am kind of stuck right now and need some help.

I have a combobox which get it's data from mssql table while form loads. There are some readonly textboxes which i need them to show other datafields when i select an item from combobox.


sql table row ----> combobox.
then i make a selection from combobox.
combobox change has to trigger an sql query with combobox's selected item.
that query puts data to each textbox.

I did search multiple days for a solution and couldn't find anything.

Thanks from now.


[EDIT - moved from comment]
Tried many things but no results. Please help :(

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;


namespace Mobiz_Takip_Destek.Arizatakip
{
    public partial class ArizatakipServisgirisi : Form
    {
        private string arackodu;
        SqlConnection connection;
        string connectionString;
        private object arackodu1;

        private void ArizatakipServisgirisi_Load(object sender, EventArgs e)
        {
            // ----------------------PERSONEL SORGUSU ------------------------------------------------------------
            string connStr = @"Data Source=ACER\SQLEXPRESS;Initial Catalog=mobiztakip;Integrated Security='True'";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            string sorgu = "Select pAdiSoyadi From personelBilgi";
            SqlCommand cmd = new SqlCommand(sorgu, conn);
            SqlDataReader cikti = cmd.ExecuteReader();

            while (cikti.Read())
            {
                personel1.Items.Add(cikti[0]);
                personel2.Items.Add(cikti[0]);
                personel3.Items.Add(cikti[0]);
                
            }
            personel1.SelectedIndex = 0;
            personel2.SelectedIndex = 0;
            personel3.SelectedIndex = 0;

            //-----------------------PERSONEL SORGUSU ---------------------------------------------------------------

            SqlConnection conn2 = new SqlConnection(connStr);
            conn2.Open();
            string sorgu2 = "Select aracKodu, kayitTarihi arizaKaydiDurum From servisTalebi where arizaKaydiDurum = 'True'";
            SqlCommand cmd2 = new SqlCommand(sorgu2, conn2);
            SqlDataReader cikti2 = cmd2.ExecuteReader();

            while (cikti2.Read())
            {
                acikTalepler.Items.Add(cikti2[0]);

            }
        }

        public ArizatakipServisgirisi()
        {
            InitializeComponent();

        }

        public ArizatakipServisgirisi(object arackodu1)
        {
            // TODO: Complete member initialization
            this.arackodu1 = arackodu1;
        }

        private void acikTalepler_SelectionIndexChanged(object sender, EventArgs e)
        {

            if (acikTalepler.SelectedValue != "Lütfen seçim yapın...")
                {

                    string secim = acikTalepler.SelectedValue.ToString();

                    string connStr = @"Data Source=ACER\SQLEXPRESS;Initial Catalog=mobiztakip;Integrated Security=True";
                    SqlConnection conn = new SqlConnection(connStr);
                    conn.Open();
                    string sorgu = "Select * From kayitliaraclar WHERE aracKodu ='" + secim + "'";
                    SqlCommand cmd = new SqlCommand(sorgu, conn);
                    SqlDataReader cikti = cmd.ExecuteReader();
                    if (cikti.Read())
                    {
                        plaka.Text = cikti["plaka"].ToString();
                    }
                    string sorgu2 = "Select * From servisTalebi WHERE aracKodu ='" + secim + "' and arizaKaydiDurum = True";
                    SqlCommand cmd2 = new SqlCommand(sorgu2, conn);
                    SqlDataReader cikti2 = cmd.ExecuteReader();
                    if (cikti2.Read())
                    {
                        talepTarihi.Text = cikti["plaka"].ToString();
                        talepPersonel.Text = cikti["kaydiAcanPersonel"].ToString();
                        talepAciklama.Text = cikti["arizaAciklama"].ToString();
                    }
                }
        }
     }
}


[/EDIT]
Posted
Updated 25-Apr-15 12:04pm
v3
Comments
Sascha Lefèvre 25-Apr-15 13:10pm    
What is your specific problem there?
Member 11641605 25-Apr-15 13:15pm    
The problem is i can't do it. I need some help with this code.
Sascha Lefèvre 25-Apr-15 13:27pm    
But you surely have tried something, haven't you? If so, please post your code, even if it doesn't work. Please use the "Improve question"-link below your question to add it to your question (not as a comment).
Member 11641605 25-Apr-15 13:38pm    
-- content has been moved to the question --

1 solution

Here is a simple example that loads the combobox and reacts to a selectedIndexChanged event.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormExamples.Models;

namespace WinFormExamples.Forms
{
    public partial class ComboExample : Form
    {
        private List<car> carList;

        private void ComboExample_Load(object sender, EventArgs e)
        {
            // Create the list and add some data
            // You can get your data via SQL instead
            carList = new List<car>();
            carList.Add(new Car(1000, 4, 6, "Mercedes"));
            carList.Add(new Car(1001, 2, 6, "Ferrari"));
            carList.Add(new Car(1002, 2, 12, "Jaguar"));        

            comboBox1.DisplayMember = "CarName";
            comboBox1.ValueMember = "CarId";
            comboBox1.DataSource = carList;
        }

        public ComboExample()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex > -1)
            {
                Car selectedCar = carList.Where(c => c.CarId == Convert.ToInt64(comboBox1.SelectedValue)).First();

                // You could also query the data here and use the SQL data
                txtName.Text = selectedCar.CarName;
                txtDoors.Text = selectedCar.Doors.ToString();
                txtCylinders.Text = selectedCar.Cylinders.ToString();
            }
        }        
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormExamples.Models
{
    public class Car
    {
        public long CarId {get; set;}
        public int Doors { get; set; }
        public int Cylinders { get; set; }
        public string CarName { get; set; }

        public Car(long id, int doors, int cylinders, string name)
        {
            CarId = id;
            Doors = doors;
            Cylinders = cylinders;
            CarName = name;
        }
    }
}</car></car>
 
Share this answer
 
Comments
Member 11641605 25-Apr-15 15:11pm    
Thank you very much for your help but it's quite different from getting combobox values from sql. I don't know how to get selected item from combobox which filled by sql rows...

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