Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was looking for a way to make combobox1 only activate if the connection to the database is successful if not it remains disabled( I set it to be disabled from properties).
C#
  1  using System;
  2  using System.Collections.Generic;
  3  using System.ComponentModel;
  4  using System.Data;
  5  using System.Data.SqlClient;
  6  using System.Drawing;
  7  using System.Linq;
  8  using System.Text;
  9  using System.Threading.Tasks;
 10  using System.Windows.Forms;
 11  
 12  namespace connectionserver1
 13  {
 14      public partial class Form1 : Form
 15      {
 16          string server, databaseName, userid, password;
 17  
 18          SqlConnection connection;
 19          SqlConnection connection1;
 20          public Form1()
 21          {
 22              InitializeComponent();
 23          }
 24  
 25          private void Form1_Load(object sender, EventArgs e)
 26          {
 27              textBox4.PasswordChar = '*';
 28              textBox8.PasswordChar = '*';
 29          }
 30  
 31          private void button1_Click(object sender, EventArgs e)
 32          {
 33              server = textBox1.Text;
 34              databaseName = textBox2.Text;
 35              userid = textBox3.Text;
 36              password = textBox4.Text;
 37              if (server == "" && databaseName == "" && userid == "" && password == "")
 38              {
 39                  MessageBox.Show("Please fill in the database connection information completely");
 40              }
 41              else
 42              {
 43                  if (OpenConnection(connection, server, databaseName, userid, password))
 44                  {
 45                      MessageBox.Show("Database connection successed");
 46                  }
 47                  else
 48                  {
 49                      MessageBox.Show("Database connection failed");
 50                  }
 51              }
 52          }
 53  
 54          private void Button2_Click(object sender, EventArgs e)
 55          {
 56              textBox1.Text = "";
 57              textBox2.Text = "";
 58              textBox3.Text = "";
 59              textBox4.Text = "";
 60              try
 61              {
 62                  CloseConnection(connection , databaseName, userid, password);
 63                  MessageBox.Show("Database disconnected");
 64              } catch { 
 65                  MessageBox.Show("Please connect to the database first");
 66              }
 67          }
 68  
 69          private bool OpenConnection(SqlConnection connection, string server, string databaseName, string userid, string password)
 70          {
 71              try
 72              {
 73                  string strConn = "server=" + server +
 74                                   ";database=" + databaseName +
 75                                  ";uid=" + userid +
 76                                  ";pwd=" + password;
 77                  connection = new SqlConnection(strConn);
 78                  connection.Open();
 79              }
 80              catch (Exception)
 81              {
 82                  return false;
 83  
 84  
 85              }
 86              return true;
 87          }
 88  
 89  
 90          private bool CloseConnection(SqlConnection connection, string server, string databaseName, string userid)
 91          {
 92              try
 93              {
 94                  connection.Dispose();
 95                  return true;
 96              }
 97              catch (Exception)
 98              {
 99                  return false;
100              }
101          }
102                                  
103          private void button3_Click(object sender, EventArgs e)
104          {
105              server = textBox5.Text;
106              databaseName = textBox6.Text;
107              userid = textBox7.Text;
108              password = textBox8.Text;
109              if (server == "" && databaseName == "" && userid == "" && password == "")
110              {
111                  MessageBox.Show("Please fill in the database connection information completely");
112              }
113              else
114              {
115                  if (OpenConnection(connection1,server,databaseName,userid,password))
116                  {
117                      MessageBox.Show("Database connection successed");
118                  }
119                  else
120                  {
121                      MessageBox.Show("Database connection failed");
122                  }
123  
124              }
125          }
126          private void button4_Click(object sender, EventArgs e)
127          {
128              textBox5.Text = "";
129              textBox6.Text = "";
130              textBox7.Text = "";
131              textBox8.Text = "";
132              try
133              {
134                  CloseConnection(connection1,databaseName,userid,password);
135                  MessageBox.Show("Database disconnected");
136              }
137              catch
138              {
139                  MessageBox.Show("Please connect to the database first");
140              }
141          }
142          private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
143          {
144              if (OpenConnection(connection1, server, databaseName, userid, password))
145              {
146                  comboBox1.SelectedValue = true;
147              }     
148              else
149              {
150                  comboBox1.SelectedValue = false;
151              }
152          }
153      }
154   }


What I have tried:

I tried this command :
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (OpenConnection(connection1, server, databaseName, userid, password))
    {
        comboBox1.SelectedValue = true;
    }     
    else
    {
        comboBox1.SelectedValue = false;
    }
}
Posted
Updated 20-Apr-23 0:03am
v2
Comments
Richard Deeming 20-Apr-23 4:13am    
You forgot to explain what the problem is. Perhaps it's something to do with the fact that you're setting the SelectedValue property rather than the Disabled property?
Mirko Nastasi 20-Apr-23 5:27am    
I'll try it now and let you know
Mirko Nastasi 20-Apr-23 5:29am    
the problem would be that I can't get the combobox to enable only when I can connect to the database and otherwise it remains disabled

1 solution

You have chosen the wrong event in which to connect to the database - if the combo box is disabled then you can never change the selection so you can't connect to the database! It's a vicious circle.

Try making the connection in the Form Load event. Then set the ComboBox Enabled property to false if the connection fails

Edit after OP comments:
If I assume button1 is to connect to the database then something like this would work - I've highlighted the changes I've made

C#
private void button1_Click(object sender, EventArgs e)
{
    server = textBox1.Text;
    databaseName = textBox2.Text;
    userid = textBox3.Text;
    password = textBox4.Text;
    if (server == "" && databaseName == "" && userid == "" && password == "")
    {
        MessageBox.Show("Please fill in the database connection information completely");
    }
    else
    {
        if (OpenConnection(connection, server, databaseName, userid, password))
        {
            MessageBox.Show("Database connection succeeded");
            comboBox1.Enabled = true;
        }
        else
        {
            MessageBox.Show("Database connection failed");
            comboBox1.Enabled = false;
        }
    }
}
In the Form designer you can click on the combo box control and disable it from the Properties window there to ensure it is always disabled. Or you could use
C#
private void Form1_Load(object sender, EventArgs e)
{
    textBox4.PasswordChar = '*';
    textBox8.PasswordChar = '*';
    comboBox1.Enabled = false;
}

One final comment - I've had to guess that button1 is what you are using to connect to the database because you have left the default names on all of your controls (Form1, Button1, Button2, Button3, comboBox1). Get into the habit of giving your controls meaningful names as soon as you have added them to your form/canvas/page - it will make your life a lot easier without having to remember what each of the controls is.
 
Share this answer
 
v2
Comments
Mirko Nastasi 20-Apr-23 6:18am    
in what breast excuse me, I'm a novice and don't understand much about it.
CHill60 20-Apr-23 6:49am    
I'm sorry I don't understand your question - can you rephrase it
Mirko Nastasi 21-Apr-23 2:37am    
I should only activate combobox1 if the program can log in to the database, otherwise it remains deactivated
CHill60 21-Apr-23 6:52am    
So disable it in the Form Designer so it is disabled by default. Attempt to connect to the database in the form Load event and if that is successful then Enable the combobox. It's essentially the same effect as my original suggestion.
Mirko Nastasi 21-Apr-23 8:15am    
I didn't understand it sorry, could you explain the how to do it, what steps should I do?

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