I have two tables, Vehicle_Status and Trans_Income.
Vehicle_Status contain two columns, Vehicle_Number and Status, and the Vehicle_Number consists the data which are the column name of Trans_Income.
I want to load the data from Vehicle_Number into an array and use it in the insert statement.
string[] vehicleActive = new string[100];
SqlCommand cmd = new SqlCommand("Select Vehicle_Number from [dbo].[Vehicle_Status] Where Status = 'Inactive'", con);
cmd.CommandType = CommandType.Text;
con.Open();
int a = 0;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
vehicleActive[a] = dr["Vehicle_Number"].ToString();
a++;
}
con.close();
for (int b = 0; b < a; b++)
{
cmd = new SqlCommand("Insert Into [dbo].[Trans_Income](Month, Particular,'"+vehicleActive[b]+"') VALUES (@Month, @Particular, @vehicleNo )", con);
con.Open();
cmd.Parameters.AddWithValue("@Month", textBox2.Text);
cmd.Parameters.AddWithValue("@Particular", textBox2.Text);
cmd.Parameters.AddWithValue("@vehicleNo", textBox3.Text);
cmd.ExecuteNonQuery();
con.Close();
}
But I get the error
System.Data.SqlClient.SqlException: 'Incorrect syntax near 'BJW6719'.'
One of the column names is BJW6719 - can anyone help me with this? Or is there any other, better way for doing this? Thank you in advance.