I have:
string commandText = @"SELECT cn.companyName from Companies cn
INNER JOIN KeyProcesses uc ON uc.companyId = cn.companyId
WHERE uc.description like '%" + ProcessInputClause + "%';";
string addr = @"SELECT address FROM Companies where companyName = @companyName";
To execute that I tried:
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = commandText;
sqlCmd.Parameters.Clear();
string connectionString = ConfigurationSettings.AppSettings["connectionString"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
try
{
connection.Open();
sqlCmd = new SqlCommand(sqlCmd.CommandText, connection);
SqlDataReader sqlReader = sqlCmd.ExecuteReader();
DataTable dt = new DataTable();
sqlReader.Read();
dt.Load(sqlReader);
Label1.Text = dt.Rows[0][0].ToString();
sqlCmd.CommandText = addr;
SqlCmd.Parameters.AddWithValue("@companyName", "namehere");
SqlDataReader addressReader = sqlCmd.ExecuteReader();
addressReader.Read();
Label1.Text = Label1.Text + addressReader["address"].ToString() + addressReader.GetValue(1).ToString() + addressReader.GetString(0) + addressReader.GetString(1);
I can get only the first sql to execute, and get the companyName to Label1.text. But it looks like the second executeReade() gives me no result, although I tried the query alone successfully! I also tried the combine commandtext and use nextresult but no luck. Can you help me in how can I execute both command successfully ? Ps: added calling addressReader.Read(); I forget to copy this, tried with this but no result !
addressReader.Read. In any case, you really need to clean up the code, separate the two executions and and not try to update the UI in the same method that calls the database. AND don't concatenate values, pass the entire%something%thing as a parameterRead()abovedt.Load()is actually throwing away the top row of data; and, your second batch of code accesses 2 columns - but your query only requests 1.