protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] != null)
{
if (Session["roles"].ToString() != "Admin")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Only Admin Can Access this Page');window.location ='HomePageAdmin.aspx';", true);
}
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Please Login To Proceed');window.location ='HomePageAdmin.aspx';", true);
}
}
protected void btnRegister_Click(object sender, EventArgs e)
{
SqlConnection SqlConn;
SqlCommand cmdInsert, cmdSelect, cmdSelect1, cmdSelect2;
SqlDataReader dtrRead;
SqlDataReader dtra;
SqlDataReader dtrb;
string connStr = ConfigurationManager.ConnectionStrings["ConnectionNWind"].ConnectionString;
SqlConn = new SqlConnection(connStr);
SqlConn.Open();
String strSearch = "Select SUserName From Staff_Login Where SUserName=@userName";
cmdSelect = new SqlCommand(strSearch, SqlConn);
cmdSelect.Parameters.AddWithValue("@userName", txtSUN.Text);
String stra = "Select SIC From Staff_Login where SIC =@IC";
cmdSelect1 = new SqlCommand(stra, SqlConn);
cmdSelect1.Parameters.AddWithValue("@IC", txtSIC.Text);
String strb = "Select SEmailAdd From Staff_Login where SEmailAdd =@Add";
cmdSelect2 = new SqlCommand(strb, SqlConn);
cmdSelect2.Parameters.AddWithValue("@Add", txtSEA.Text);
dtrRead = cmdSelect.ExecuteReader();
dtra = cmdSelect1.ExecuteReader();
dtrb = cmdSelect2.ExecuteReader();
if (dtrRead.Read())
{
Response.Write("<script>alert(' - User already exist')</script>");
}
else if (dtra.Read())
{
Response.Write("<script>alert(' - IC already exist')</script>");
}
else if(dtrb.Read()){
Response.Write("<script>alert(' - Email already exist')</script>");
}
else
{
dtrRead.Close();
dtra.Close();
dtrb.Close();
String strInsert = "Insert Into Staff_Login (SUserName,SPassword,SfirstName,SlastName,SIC,gender,ShomeAdd,SemailAdd,SContactNum,StaffStatus)Values (@userName, @passW,@fName, @lName, @icNo, @gender, @hAdd, @eAdd, @SCN, @StaffStatus )";
cmdInsert = new SqlCommand(strInsert, SqlConn);
cmdInsert.Parameters.AddWithValue("@userName", txtSUN.Text);
cmdInsert.Parameters.AddWithValue("@fName", txtSFN.Text);
cmdInsert.Parameters.AddWithValue("@lName", txtSLN.Text);
cmdInsert.Parameters.AddWithValue("@icNo", txtSIC.Text);
cmdInsert.Parameters.AddWithValue("@Gender", rblSG.SelectedItem.ToString());
cmdInsert.Parameters.AddWithValue("@hAdd", txtSHA.Text);
cmdInsert.Parameters.AddWithValue("@eAdd", txtSEA.Text);
cmdInsert.Parameters.AddWithValue("@SCN", txtSCN.Text);
cmdInsert.Parameters.AddWithValue("@passW", txtSP.Text);
cmdInsert.Parameters.AddWithValue("@StaffStatus", ddlStatus.SelectedItem.ToString());
cmdInsert.ExecuteNonQuery();
lblMessage.Text = "You have been successfully register as member!";
txtSUN.Text = "";
txtSFN.Text = "";
txtSLN.Text = "";
txtSIC.Text = "";
txtSHA.Text = "";
txtSEA.Text = "";
txtSCN.Text = "";
txtSP.Text = "";
}
SqlConn.Close();
dtrRead.Close();
//dtra.Close();
//dtrb.Close();
}
}
I wan use the diffrent datareader to display out the diffrent message bu i get an error protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] != null)
{
if (Session["roles"].ToString() != "Admin")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Only Admin Can Access this Page');window.location ='HomePageAdmin.aspx';", true);
}
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Please Login To Proceed');window.location ='HomePageAdmin.aspx';", true);
}
}
protected void btnRegister_Click(object sender, EventArgs e)
{
SqlConnection SqlConn;
SqlCommand cmdInsert, cmdSelect, cmdSelect1, cmdSelect2;
SqlDataReader dtrRead;
SqlDataReader dtra;
SqlDataReader dtrb;
string connStr = ConfigurationManager.ConnectionStrings["ConnectionNWind"].ConnectionString;
SqlConn = new SqlConnection(connStr);
SqlConn.Open();
String strSearch = "Select SUserName From Staff_Login Where SUserName=@userName";
cmdSelect = new SqlCommand(strSearch, SqlConn);
cmdSelect.Parameters.AddWithValue("@userName", txtSUN.Text);
String stra = "Select SIC From Staff_Login where SIC =@IC";
cmdSelect1 = new SqlCommand(stra, SqlConn);
cmdSelect1.Parameters.AddWithValue("@IC", txtSIC.Text);
String strb = "Select SEmailAdd From Staff_Login where SEmailAdd =@Add";
cmdSelect2 = new SqlCommand(strb, SqlConn);
cmdSelect2.Parameters.AddWithValue("@Add", txtSEA.Text);
dtrRead = cmdSelect.ExecuteReader();
dtra = cmdSelect1.ExecuteReader();
dtrb = cmdSelect2.ExecuteReader();
if (dtrRead.Read())
{
Response.Write("<script>alert(' - User already exist')</script>");
}
else if (dtra.Read())
{
Response.Write("<script>alert(' - IC already exist')</script>");
}
else if(dtrb.Read()){
Response.Write("<script>alert(' - Email already exist')</script>");
}
else
{
dtrRead.Close();
dtra.Close();
dtrb.Close();
String strInsert = "Insert Into Staff_Login (SUserName,SPassword,SfirstName,SlastName,SIC,gender,ShomeAdd,SemailAdd,SContactNum,StaffStatus)Values (@userName, @passW,@fName, @lName, @icNo, @gender, @hAdd, @eAdd, @SCN, @StaffStatus )";
cmdInsert = new SqlCommand(strInsert, SqlConn);
cmdInsert.Parameters.AddWithValue("@userName", txtSUN.Text);
cmdInsert.Parameters.AddWithValue("@fName", txtSFN.Text);
cmdInsert.Parameters.AddWithValue("@lName", txtSLN.Text);
cmdInsert.Parameters.AddWithValue("@icNo", txtSIC.Text);
cmdInsert.Parameters.AddWithValue("@Gender", rblSG.SelectedItem.ToString());
cmdInsert.Parameters.AddWithValue("@hAdd", txtSHA.Text);
cmdInsert.Parameters.AddWithValue("@eAdd", txtSEA.Text);
cmdInsert.Parameters.AddWithValue("@SCN", txtSCN.Text);
cmdInsert.Parameters.AddWithValue("@passW", txtSP.Text);
cmdInsert.Parameters.AddWithValue("@StaffStatus", ddlStatus.SelectedItem.ToString());
cmdInsert.ExecuteNonQuery();
lblMessage.Text = "You have been successfully register as member!";
txtSUN.Text = "";
txtSFN.Text = "";
txtSLN.Text = "";
txtSIC.Text = "";
txtSHA.Text = "";
txtSEA.Text = "";
txtSCN.Text = "";
txtSP.Text = "";
}
SqlConn.Close();
dtrRead.Close();
//dtra.Close();
//dtrb.Close();
}
}
I wan use the diffrent data reader to display the diffrent erro message but i get an error There is already an open DataReader associated with this Command which must be closed first.
How i wan to solve it?
ku.heng.7 0 Newbie Poster
BhuvanRam 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.