Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 加载表单时,组合框的显示值(不是ID值)。并应准备数据,以便通过其ID发送到数据库。确保您的数据库组织为接受字段中的ID。 private void cbViewServices_SelectedIndexChanged(object sender, Ev_C#_Mysql_Combobox_Sqldataadapter - Fatal编程技术网

C# 加载表单时,组合框的显示值(不是ID值)。并应准备数据,以便通过其ID发送到数据库。确保您的数据库组织为接受字段中的ID。 private void cbViewServices_SelectedIndexChanged(object sender, Ev

C# 加载表单时,组合框的显示值(不是ID值)。并应准备数据,以便通过其ID发送到数据库。确保您的数据库组织为接受字段中的ID。 private void cbViewServices_SelectedIndexChanged(object sender, Ev,c#,mysql,combobox,sqldataadapter,C#,Mysql,Combobox,Sqldataadapter,加载表单时,组合框的显示值(不是ID值)。并应准备数据,以便通过其ID发送到数据库。确保您的数据库组织为接受字段中的ID。 private void cbViewServices_SelectedIndexChanged(object sender, EventArgs e) { if (cbViewServices.SelectedIndex >- 1) { string lastName = cbViewServices.SelectedValue.ToString()

加载表单时,组合框的显示值(不是ID值)。并应准备数据,以便通过其ID发送到数据库。确保您的数据库组织为接受字段中的ID。
private void cbViewServices_SelectedIndexChanged(object sender, EventArgs e)
{
  if (cbViewServices.SelectedIndex >- 1)
  {
    string lastName = cbViewServices.SelectedValue.ToString();                
    MySqlConnection conn = new MySqlConnection("server=localhost;uid=******;password=**********;database=dboserviceinfo;");
    conn.Open();

    SqlDataAdapter da = new SqlDataAdapter("select distinct LastName from tserviceinfo where LastName='" + lastName + "'", conn);

    DataSet ds = new DataSet();
    da.Fill(ds); conn.Close();
  }
}
public void ListCat()
{
    DataTable linkcat = new DataTable("linkcat");
    using (SqlConnection sqlConn = new SqlConnection(@"Connection stuff;"))
    {
        using (SqlDataAdapter da = new SqlDataAdapter("SELECT LastName FROM list WHERE LastName <> 'NULL'", sqlConn))
        {
            da.Fill(linkcat);
        }
    }
    foreach (DataRow da in linkcat.Rows)
    {
        comboBox1.Items.Add(da[0].ToString());
    }
}
MySqlDataAdapter da = new MySqlDataAdapter("select distinct LastName from tserviceinfo where LastName='" + lastName + "'", conn);
//Load customer ID to a combobox
private void LoadCustomersId()
{
    var connectionString = "connection string goes here";
    using (var connection = new MySqlConnection(connectionString))
    {
        connection.Open();
        var query = "SELECT Id FROM Customers";
        using (var command = new MySqlCommand(query, connection))
        {
            using (var reader = command.ExecuteReader())
            {
                //Iterate through the rows and add it to the combobox's items
                while (reader.Read())
                {
                    CustomerIdComboBox.Items.Add(reader.GetString("Id"));    
                }
            }    
        }
    }
}

//Load customer details using the ID
private void LoadCustomerDetailsById(int id)
{
    var connectionString = "connection string goes here";
    using (var connection = new MySqlConnection(connectionString))
    {
        connection.Open();
        var query = "SELECT Id, Firstname, Lastname FROM Customer WHERE Id = @customerId";
        using (var command = new MySqlCommand(query, connection))
        {
            //Always use SQL parameters to avoid SQL injection and it automatically escapes characters
            command.Parameters.AddWithValue("@customerId", id);
            using (var reader = command.ExecuteReader())
            {
                //No customer found by supplied ID
                if (!reader.HasRows)
                    return;

                CustomerIdTextBox.Text = reader.GetInt32("Id").ToString();
                FirstnameTextBox.Text = reader.GetString("Firstname");
                LastnameTextBox.Text = reader.GetString("Lastname");
            }
        }
    }
}

//Pass the selected ID in the combobox to the customer details loader method 
private void CustomerIdComboBox_SelectedIndexChanged(object s, EventArgs e)
{
    var customerId = Convert.ToInt32(CustomerIdComboBox.Text);
    LoadCustomerDetailsById(customerId);
}
//USING
        using System;
        using System.Drawing;
        using System.Windows.Forms;
        using System.Data.SqlClient;
        using System.Data;

namespace YourNamespace
{
//Initialization
        string connetionString = null;
        SqlConnection cnn;
        SqlCommand cmdDataBase;
        SqlDataReader reader;
        DataTable dt;

public frmName()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
            FillComboNameOfCombo();
        }

void FillcmbNameOfCombo()
{
    string sqlQuery = "SELECT * FROM DATABASENAME.[dbo].[TABLENAME];";
            connetionString = "Data Source=YourPathToServer;Initial Catalog=DATABASE_NAME;User ID=id;Password=pass";
                    cnn = new SqlConnection(connetionString); 
                    cmdDataBase = new SqlCommand(sqlQuery, cnn);
            try { 
                    cnn.Open(); 

                    reader = cmdDataBase.ExecuteReader();
                    dt = new DataTable();

                    dt.Columns.Add("ID", typeof(string));
                    dt.Columns.Add("COLUMN_NAME", typeof(string));
                    dt.Load(reader);
                    cnn.Close();

                    cmbGender.DataSource = dt;
                    cmbGender.ValueMember = "ID";
                    cmbGender.DisplayMember = "COLUMN_NAME";

                    dt = null;
                    cnn = null;
                    cmdDataBase = null;
                    connetionString = null;
                    reader = null;
                }
            catch (Exception ex) { 
                    MessageBox.Show("Can not open connection ! " + ex.ToString());
                }
}
}
  while (userRead.Read())
   {
        cboxReportNo.Items.Add(userRead[1].ToString());
   }
this.comboBox1.DataSource = ds;
this.comboBox1.DisplayMember = "LastName";
this.comboBox1.ValueMember = "Id";
this.comboBox1.SelectedIndex = -1;
this.comboBox1.AutoCompleteMode = AutoCompleteMode.Append;
this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;