shawn
shawn

Reputation: 113

SQL Query Problems with Tables

 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.

Upvotes: 0

Views: 124

Answers (3)

Airlyn Khey Talara
Airlyn Khey Talara

Reputation: 5

SqlCommand cmd;

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

Upvotes: 0

Cam Bruce
Cam Bruce

Reputation: 5689

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

 "SELECT Client_Groups" + "FROM"

change to

"SELECT Client_Groups " + "FROM "

Upvotes: 1

BizApps
BizApps

Reputation: 6130

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

something like:

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

Best Regards

Upvotes: 3

Related Questions