Skip to content

Can't use '+' to append text??

Matt Butler

Asked Oct 29, 2010, 11:49 AM EDTSolved
Share
LinkedInX
I am using the following code to attempt to get a list of tables from a list of databases that I previously fetched. I cannot get the tables to work because I cannot split the connection string for some odd reason. I should tell you that I am very new to C#. I was taking Java at school and decided that C# was much better for me.

This is the code to get my list of databases from the SQL server:
TypeScript
 
        private void frmImport_Load(object sender, EventArgs e)
        {
           
            comboBox2.Items.Clear();
            SqlConnection SqlConn = new SqlConnection("Data Source=10.100.1.6; Trusted_Connection=True;");
            SqlConn.Open();
       
            SqlCommand SqlCom = new SqlCommand();
            SqlCom.Connection = SqlConn;
            SqlCom.CommandType = CommandType.StoredProcedure;
            SqlCom.CommandText = "sp_databases";
       
            SqlDataReader SqlDR;
            SqlDR=SqlCom.ExecuteReader();
       
            while (SqlDR.Read())
            {
                comboBox1.Items.Add(SqlDR.GetString(0));
            }
        }


This is my code to get the tables from the database that I just fetched:
TypeScript
         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            SqlConnection SqlConn = new SqlConnection("Data Source=10.100.1.6; Initial Catalog=" +comboBox1.SelectedItem.ToString +";Trusted_Connection=True;");
            SqlConn.Open();

            SqlCommand SqlCom = new SqlCommand();
            SqlCom.Connection = SqlConn;
            SqlCom.CommandType = CommandType.StoredProcedure;
            SqlCom.CommandText = "sp_tables";

            SqlDataReader SqlDR;
            SqlDR = SqlCom.ExecuteReader();

            while (SqlDR.Read())
            {
                comboBox2.Items.Add(SqlDR.GetString(0));
            }
        }


I having a problem with the second line. I am getting this error:
Code
 Error    1    Operator '+' cannot be applied to operands of type 'string' and 'method group'

Any help you can provide me would be greatly appreciated.

Kind Regards

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mahesh Chand
    ✓ Accepted answer0
    Welcome Matt. You certainly made a better choice choosing C# :).

    ToString() is a method.

    Add closing brackets after ToString.

    + comboBox1.Selectedtem.ToString() +
    0
  • 0
    Wow, you would think the Code tag button would allow me to save my formatting.

    Here is the code again.

    This is the code to get my list of databases from the SQL server:
            private void frmImport_Load(object sender, EventArgs e)
            {
               
                comboBox2.Items.Clear();
                SqlConnection SqlConn = new SqlConnection("Data Source=10.100.1.6; Trusted_Connection=True;");
                SqlConn.Open();
           
                SqlCommand SqlCom = new SqlCommand();
                SqlCom.Connection = SqlConn;
                SqlCom.CommandType = CommandType.StoredProcedure;
                SqlCom.CommandText = "sp_databases";
           
                SqlDataReader SqlDR;
                SqlDR=SqlCom.ExecuteReader();
           
                while (SqlDR.Read())
                {
                    comboBox1.Items.Add(SqlDR.GetString(0));
                }
            }

    This is my code to get the tables from the database that I just fetched:
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                comboBox2.Items.Clear();
                SqlConnection SqlConn = new SqlConnection("Data Source=10.100.1.6; Initial Catalog=" +comboBox1.SelectedItem.ToString +";Trusted_Connection=True;");
                SqlConn.Open();

                SqlCommand SqlCom = new SqlCommand();
                SqlCom.Connection = SqlConn;
                SqlCom.CommandType = CommandType.StoredProcedure;
                SqlCom.CommandText = "sp_tables";

                SqlDataReader SqlDR;
                SqlDR = SqlCom.ExecuteReader();

                while (SqlDR.Read())
                {
                    comboBox2.Items.Add(SqlDR.GetString(0));
                }
            }

    0
  • 0
    Mahesh,

    That was the problem. Thanks so much for your assistance on this. It looks like I need to learn the nuances.

    Kind Regards,
    Matt
    0