0
 public void SPROC_LoadGroups()
        { 
            //This gets the table name.
            string tablename = cboNetChannel.SelectedItem.ToString();

            SqlConnection sqlConnectionCmdString = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Rick\Documents\Visual Studio 2010\Projects\Server\database\ClientRegit.mdf;Integrated Security=True;User Instance=True");

            //This is the table name and Query that identifies with the selected table
            string Command = "SELECT Client_Groups" + "FROM" + tablename;

            SqlCommand sqlCommand = new SqlCommand(Command, sqlConnectionCmdString);

            SqlDataAdapter objDA = new SqlDataAdapter(sqlCommand);

            DataSet dsGroups = new DataSet();

            objDA.Fill(dsGroups, "dtGroup");

            cboExistingG.DataSource = dsGroups.Tables["dtGroup"];
            cboExistingG.DisplayMember = "Client_Groups";
            //cboExistingG.ValueMember = "ID";


        }

Error I am getting is this {"Incorrect syntax near '-'."}

I got a situation is it possible to query as table with a name similar to a GUID value my table name is 43d5377-0dcd-40e6-b95c-8ee980b1e248

I am generating groups that are identified with a Networking Data table that is named 43d5377-0dcd-40e6-b95c-8ee980b1e248 The table name is allowed and SQL does not prohibit such table names.

This is my code I am getting an error, I am table mapping with this by creating a Query that allows me to identify the query with the selected table value.

3 Answers 3

3

If your table name is similar as a GUID add [] block

something like:

     string Command = "SELECT Client_Groups FROM [" + tablename+ "]";

Best Regards

Sign up to request clarification or add additional context in comments.

Comments

1

You were missing a space between the concatination of these two strings:

 "SELECT Client_Groups" + "FROM"

change to

"SELECT Client_Groups " + "FROM "

2 Comments

That is one of the problems - however he will run into issues with the - in the Guid too if the table name not enclosed by a double quote.
Thanks guys for your help awesome I thought it was my table but I am allowed to use - characters just not _ Characters thanks
0

SqlCommand cmd;

cmd = new SqlCommand("SELECT client_Groups FROM Table name where name='" + txtbox. Text + "' , lastname='" + txtbox. Text + "'", con);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.