I am using a database to relate a Department's Name with it's unique mailing code i.e. Accounting has mailing code 1337. ShortName and ID are the fields of the database that are being queried.
The Schema is
ID int
ShortName nvarchar(255)
This is done on the backend of a webpage using C#. I have used a SqlDataReader
object elsewhere in the same code but no error was caused by this. I think I have a syntax error somewhere or I am trying to stick a string into an int or something silly but I can't see it.
The SqlConnection
is set up correctly as it is used for a query upon page load and works when the indicated line is commented out.
SqlDataReader SQl_Reader;
string cmdString = "SELECT ShortName FROM departments WHERE (ID = " +
department_Text.SelectedValue.ToString() + ")"; //a selected value in department_text would be the mailing code such as 1337, or 1304.
SqlCommand SQl_Command2 = new SqlCommand(cmdString, SQl_Connection);
SQl_Reader = SQl_Command.ExecuteReader();
string deptName = SQl_Reader["ShortName"].ToString();//This should assign the deptName = the value in SQl_Reader but it causes the page not to load. Not sure what is wrong with it
SQl_Reader.Close();
SQl_Connection.Close();
I have tried this multiple different ways even using a proprietary Connection, Command, and Reader just for this query. I have used a SqlDataReader
for a similar purpose with the same two fields in another function of the code and it works perfectly. Any insights would be very appreciated.