EDN Admin
Well-known member
<span style="line-height:115%; font-family:Calibri,sans-serif; font-size:11pt I’ve got the following sql query that fills a datagridview
<pre class="prettyprint string connectionString = equipmentTableAdapter.Connection.ConnectionString;
string SelectCommand = "SELECT DISTINCT [Type] FROM [Equipment].[dbo].[Equipment] WHERE ISNULL(Equipment.Type,)<>";
SqlConnection EquCon = new SqlConnection(connectionString);
SqlDataAdapter dataAdapter = new SqlDataAdapter(SelectCommand, connectionString);
SqlCommand sqlCommand = new SqlCommand(SelectCommand, EquCon );
dataAdapter.SelectCommand = sqlCommand;
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
TypeDataGridView.DataSource = table;
[/code]
That works fine but I want to change it to a parameterized query to select different columns from the table so I put the parameter @cType like so;
<pre class="prettyprint string connectionString = equipmentTableAdapter.Connection.ConnectionString;
string SelectCommand = "SELECT DISTINCT [@cType] FROM [Equipment].[dbo].[Equipment] WHERE ISNULL(Equipment.Type,)<>";
SqlConnection EquCon = new SqlConnection(connectionString);
SqlDataAdapter dataAdapter = new SqlDataAdapter(SelectCommand, connectionString);
SqlCommand sqlCommand = new SqlCommand(SelectCommand, EquCon );
dataAdapter.SelectCommand = sqlCommand;
sqlCommand.Parameters.Add("@cType", SqlDbType.NVarChar).Value = "Type";
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
TypeDataGridView.DataSource = table;
[/code]
The dataAdapter.Fill(table); <span> <span> returns an error “Invalid column name [@cType]”
<br/>
<br/><hr class="sig why767
View the full article
<pre class="prettyprint string connectionString = equipmentTableAdapter.Connection.ConnectionString;
string SelectCommand = "SELECT DISTINCT [Type] FROM [Equipment].[dbo].[Equipment] WHERE ISNULL(Equipment.Type,)<>";
SqlConnection EquCon = new SqlConnection(connectionString);
SqlDataAdapter dataAdapter = new SqlDataAdapter(SelectCommand, connectionString);
SqlCommand sqlCommand = new SqlCommand(SelectCommand, EquCon );
dataAdapter.SelectCommand = sqlCommand;
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
TypeDataGridView.DataSource = table;
[/code]
That works fine but I want to change it to a parameterized query to select different columns from the table so I put the parameter @cType like so;
<pre class="prettyprint string connectionString = equipmentTableAdapter.Connection.ConnectionString;
string SelectCommand = "SELECT DISTINCT [@cType] FROM [Equipment].[dbo].[Equipment] WHERE ISNULL(Equipment.Type,)<>";
SqlConnection EquCon = new SqlConnection(connectionString);
SqlDataAdapter dataAdapter = new SqlDataAdapter(SelectCommand, connectionString);
SqlCommand sqlCommand = new SqlCommand(SelectCommand, EquCon );
dataAdapter.SelectCommand = sqlCommand;
sqlCommand.Parameters.Add("@cType", SqlDbType.NVarChar).Value = "Type";
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
TypeDataGridView.DataSource = table;
[/code]
The dataAdapter.Fill(table); <span> <span> returns an error “Invalid column name [@cType]”
<br/>
<br/><hr class="sig why767
View the full article