CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2013
    Posts
    1

    Post How to link ComboBox with Database Values - I have two Errors

    Hi, I have tried to link my combobox to the database values but, i have two errors wherefore, i need your help. I am using vb.net 2008 and 3.5 framework.
    The code is below:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.IO;

    namespace Csharptutorial10
    {
    public partial class Form1 : Form
    {
    SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Blessed;Integrated Security=True;");
    SqlCommand cmd = new SqlCommand();
    SqlDataReader myReader;
    public Form1()
    {
    InitializeComponent();
    Fillcombo();
    }

    void Fillcombo()
    {
    try
    {
    cmd.CommandText = "select * from Employee;";
    con.Open();
    myReader = cmd.ExecuteReader();
    while (myReader.Read())
    {
    string sFirstName = myReader.GetValues("FirstName");
    comboBox1.Items.Add(sFirstName);
    }
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }

    }


    private void employeeBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
    this.Validate();
    this.employeeBindingSource.EndEdit();
    this.tableAdapterManager.UpdateAll(this.blessedDataSet);

    }

    private void Form1_Load(object sender, EventArgs e)
    {
    // TODO: This line of code loads data into the 'blessedDataSet.Employee' table. You can move, or remove it, as needed.
    this.employeeTableAdapter.Fill(this.blessedDataSet.Employee);

    }

    private void buttonExit_Click(object sender, EventArgs e)
    {
    this.Close();
    }

    private void buttonAddString_Click(object sender, EventArgs e)
    {
    string sFirstName = firstNameTextBox.Text;
    comboBox1.Items.Add(sFirstName);
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    eIDTextBox.Text = comboBox1.Text;
    }
    }
    }

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: How to link ComboBox with Database Values - I have two Errors

    Please use CODE Tags when posting code :

    http://forums.codeguru.com/showthrea...before-posting!)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured