SqlConnection myconn; SqlCommand cmd; string str = "Data Source=TIMSCDR\\SQLEXPRESS;Initial Catalog=IAP;Integrated Security=True"; SqlDataReader dr; protected void Page_Load(object sender, EventArgs e) { try { cmd = new SqlCommand("select stuid from attendance", myconn); myconn.Open(); ddlstuid.Items.Clear(); dr = cmd.ExecuteReader(); if (dr != null) { while (dr.Read()) { ddlstuid.Items.Add(dr["stuid"].ToString()); } } myconn.Close(); } catch (Exception e1) { Label45.Text = "Error ocurred" + e1; }
cmd = new SqlCommand("select stuid from attendance", myconn); myconn = new SqlConnection(str); // --- Add this line myconn.Open();
finally
myconn = new SqlConnection(str); // --- Add this line
Remove if(dr != null)
if(dr.hasRows)
SqlConnection myconn; SqlCommand cmd; string str = "Data Source=TIMSCDR\\SQLEXPRESS;Initial Catalog=IAP;Integrated Security=True"; SqlDataReader dr; protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { try { myconn = new SqlConnection(str); // --- Add this line cmd = new SqlCommand("select stuid from attendance", myconn); myconn.Open(); ddlstuid.Items.Clear(); dr = cmd.ExecuteReader(); if (dr != null) { while (dr.Read()) { ddlstuid.Items.Add(dr["stuid"].ToString()); } } myconn.Close(); } catch (Exception e1) { Label45.Text = "Error ocurred" + e1; } }
Dim strSQL As String Dim connection As SqlConnection = New SqlConnection("Data Source=TIMSCDR\\SQLEXPRESS;Initial Catalog=IAP;Integrated Security=True") strSQL = "Select [stuid] from [attendance]" connection.Open() Dim command As New SqlCommand(strSQL, connection) dddiscipline.DataSource = command.ExecuteReader ddlstuid.DataTextField = "stuid" ddlstuid.DataValueField = "stuid" ddlstuid.DataBind() connection.Dispose() connection.Close()
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)