Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
there is a case which i download from projects table to combobox:

private void InsertStatus_Load(object sender, EventArgs e)
{
 connection = new MySqlConnection();
 connection.ConnectionString = "datasource=localhost;port=3306;username=root;password=";
 connection.Open();

 string selectQuery5 = "SELECT ID, PROJECT_NAME FROM projekt1.projects";
            MySqlCommand command5 = new MySqlCommand(selectQuery5, connection);
            MySqlDataReader reader5 = command5.ExecuteReader();
            while (reader5.Read())
            {
                comboBox4.Items.Add(reader5.GetString("ID") + " " + reader5.GetString("PROJECT_NAME"));
            }
            connection.Close();
}


After downloading data to combobox it displays all ID's and PROJECT_NAME's

ID      PROJECT_NAME 
----    ------------
1       dasa
2       dsag3
3       S-BOX
.       .
.       .
.       .
97      s-BOX12
98      S-DIX
99      S-DIX
100     DAFDS
101     DAF
102     DSA


In the case which ID's were from 1 to 99 it would solve the problem with display full names (in PROJECT_NAME column):

private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
             if (comboBox4.SelectedIndex != -1)
             {
                string[] parts = comboBox4.Items[comboBox4.SelectedIndex].ToString().Split(' ');
                textBox10.Text = parts[0];
                textBox4.Text = comboBox4.Text.Remove(0, 2).Trim();
             } 
        }


to text box will display full name without ID's for example:

SBOX12, DSA, S-BOX, SBO$, etc


But where the ID's are at least 100 I'm afraid that it won't display full names:

0 sbox, 1 das, 2 S-BOX,


I don't have an idea what i should do.

What I have tried:

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 MySql.Data.MySqlClient;
using System.Text.RegularExpressions;

namespace ControlDatabase
{
    public partial class InsertStatus : Form
    {
        MySqlConnection connection;
        MySqlCommand command;
        MySqlDataReader dr;
        Form1 frm1 = (Form1)Application.OpenForms["Form1"];
        public InsertStatus()
        {
            InitializeComponent();
        }
private void InsertStatus_Load(object sender, EventArgs e)
{
 connection = new MySqlConnection();
 connection.ConnectionString = "datasource=localhost;port=3306;username=root;password=";
 connection.Open();

 string selectQuery5 = "SELECT ID, PROJECT_NAME FROM projekt1.projects";
            MySqlCommand command5 = new MySqlCommand(selectQuery5, connection);
            MySqlDataReader reader5 = command5.ExecuteReader();
            while (reader5.Read())
            {
                comboBox4.Items.Add(reader5.GetString("ID") + " " + reader5.GetString("PROJECT_NAME"));
            }
            connection.Close();
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
             if (comboBox4.SelectedIndex != -1)
             {
                string[] parts = comboBox4.Items[comboBox4.SelectedIndex].ToString().Split(' ');
                textBox10.Text = parts[0];
                textBox4.Text = comboBox4.Text.Remove(0, 2).Trim();
             } 
        }
Posted
Updated 7-Mar-19 8:57am
Comments
RmcbainTheThird 7-Mar-19 14:53pm    
why aren't you setting textBox4.Text = parts[1]?. also there is a real strong assumption that the project name does not have spaces in it. Is that true? If so how do you know that is the case?
Member 10696161 8-Mar-19 1:15am    
ok, I've tried with textBox4.Text = parts[1] but it doesn't display full name with space because for example:

textBox4.Text = parts[1]

Exception -> S BOX
Reality -> S 

1 solution

ok the problem is the
comboBox4.Text.Remove(0, 2).Trim();

if you have the string "100 word"
and you remove 2 characters starting in the 0th place(removing "10" then you will get "0 word"

you have already split the string into 2 more pieces depending on how many spaces are in the text so use that
 
Share this answer
 

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