I have a view that contains several tables. now I want to select Id from that view like below:
/////Get the Id of View_SubjectStudy_Lesson
SqlCommand cmd2 = new SqlCommand("select Id from View_SubjectStudy_Lesson WHERE LessonTittle LIKE '%" + ListLesson.SelectedItem.Text + "%' and SubjectStudyId = '" +
LblSubjectStudyID.Text + "'", con);
SqlDataReader dr2;
con.Open();
sqlcom.Connection = con;
dr2 = cmd2.ExecuteReader();
while (dr2.Read())
{
LblLessonId.Text = dr2["Id"].ToString();
}
sqlcom.Connection.Close();
But it does not return any value in LblLessonId.
Also, I try my code like below but it doesn't work
using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["KDUIS-v1ConnectionString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select Id from View_SubjectStudy_Lesson WHERE LessonTittle LIKE '%" + ListLesson.SelectedItem.Text + "%' and SubjectStudyId = '" +
LblSubjectStudyID.Text + "'", con))
{
SqlDataReader dr;
con.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
LblLessonId.Text = dr["Id"].ToString();
}
con.Close();
}
}
Did I write a correct code?