I'm trying to hide a row or disable a dropdown when a document is approved once.
This code does the approved/rejected/pending. Now, I want to make it so that when an admin has approved a document once, then when he/she logs in again, they should not be able to approve/reject the same document again. Here is my asp.net code:
protected void Button1_Click(object sender, EventArgs e)
{
string connStr =
ConfigurationManager.ConnectionStrings["mydms"].ConnectionString;
SqlConnection mySQLconnection = new SqlConnection(connStr);
if (mySQLconnection.State == ConnectionState.Closed)
{
mySQLconnection.Open();
}
foreach (GridViewRow row in GrdFileApprove.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
DropDownList DropDownListcontrol = row.FindControl("DropDownList4") as
DropDownList;
SqlCommand cmd = new SqlCommand("approveddd", mySQLconnection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#DocID", SqlDbType.Int).Value =
Convert.ToInt32((row.Cells[1].Text));
cmd.Parameters.Add("#ApproveID", SqlDbType.Int).Value =
Convert.ToInt32(DropDownListcontrol.SelectedValue);
cmd.Parameters.Add("#ApproveBy", SqlDbType.VarChar, 50).Value =
(Session["Login2"]);
cmd.ExecuteNonQuery();
}
else
{
apfi.Text = "Error";
}
}
if (mySQLconnection.State == ConnectionState.Open)
{
mySQLconnection.Close();
}
How do I do this?
Hide dropdown:
DropDownList1.Visible = false;
Disable dropdown:
DropDownList1.Enabled = false;
Related
I'd like to integrate an autocheck that prevent user to forget to save changes with a textbox 'Do you want to save change' Yes-No
If yes - > save
if no - > returns
Here's my code without the checking
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
OleDbConnection conn;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
conn = new
OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data
Source=" + #Application.StartupPath + "\\Database1.mdb");
fill_lb();
}
private void fill_lb()
{
listBox1.Items.Clear();
if (conn.State != ConnectionState.Open) { conn.Close();
conn.Open(); }
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM [table1] ORDER BY firstn";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
listBox1.Items.Add(dr["firstn"].ToString());
}
conn.Close();
}
private void listBox1_SelectedIndexChanged(object sender,
EventArgs e)
{
textBox_fn.Text = string.Empty;
textBox_ln.Text = string.Empty;
if (conn.State != ConnectionState.Open) { conn.Close();
conn.Open(); }
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM [table1] WHERE firstn='" +
listBox1.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
textBox_fn.Text = dr["firstn"].ToString();
textBox_ln.Text = dr["lastn"].ToString();
}
conn.Close();
}
private void button_savenew_Click(object sender, EventArgs e)
{
if (conn.State != ConnectionState.Open) { conn.Close();
conn.Open(); }
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO [table1] ([firstn],[lastn])
values ([#firstn],[#lastn])";
cmd.Parameters.AddWithValue("#firstn", textBox_fn.Text);
cmd.Parameters.AddWithValue("#lastn", textBox_ln.Text);
cmd.ExecuteNonQuery();
fill_lb();
conn.Close();
}
private void button_modify_Click(object sender, EventArgs e)
{
if (conn.State != ConnectionState.Open) { conn.Close();
conn.Open(); }
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "UPDATE [Table1] SET [firstn]=[#firstn],
[lastn]=[#lastn] WHERE firstn = '" +
listBox1.SelectedItem.ToString() + "'";
cmd.Parameters.AddWithValue("#firstn", textBox_fn.Text);
cmd.Parameters.AddWithValue("#lastn", textBox_ln.Text);
cmd.ExecuteNonQuery();
fill_lb();
conn.Close();
}
private void button_new_Click(object sender, EventArgs e)
{
textBox_fn.Text = string.Empty;
textBox_ln.Text = string.Empty;
}
}
}
What I've done with no success :
Bool modified = false
private void textBox_fn_TextChanged(object sender, EventArgs e)
{
modified = true;
}
private void textBox_ln_TextChanged(object sender, EventArgs e)
{
modified = true;
}
private void listBox1_SelectedIndexChanged(object sender,
EventArgs e)
{
if (modified.Equals(true))
{
DialogResult dialogr = MessageBox.Show("Do you want to
save change ?","", MessageBoxButtons.YesNo);
switch (dialogr)
{
case DialogResult.Yes:
button_savenew.PerformClick();
modifie = false;
break;
case DialogResult.No:
return;
}
}
modified = false;
textBox_fn.Text = string.Empty;
textBox_ln.Text = string.Empty;
}
This is not working because it ask to save everytime I click on listbox
What Can I do ?
I would look into using a MessageBox. It would greatly simplify what you are trying to do. Perform the check in the background, and if they didn't save do this:
string message = "Are you sure you don't want to save?";
string caption = "Error Detected in Input";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
// Displays the MessageBox.
result = MessageBox.Show(message, "Are you Sure", buttons);
if (result == System.Windows.Forms.DialogResult.Yes)
{
// Save file
}
if (result == System.Windows.Forms.DialogResult.No){
this.Close();
}
Remove the the associate code from listBox1_SelectedIndexChanged event and add to it the end of button_modify_Click. Try like:
private void Check()
{
if (modified.Equals(true))
{
DialogResult dialogr = MessageBox.Show("Do you want to
save change ?","", MessageBoxButtons.YesNo);
switch (dialogr)
{
case DialogResult.Yes:
button_savenew.PerformClick();
modifie = false;
break;
case DialogResult.No:
return;
}
}
modified = false;
textBox_fn.Text = string.Empty;
textBox_ln.Text = string.Empty;
}
private void button_modify_Click(object sender, EventArgs e)
{
if (conn.State != ConnectionState.Open) { conn.Close();
conn.Open(); }
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "UPDATE [Table1] SET [firstn]=[#firstn],
[lastn]=[#lastn] WHERE firstn = '" +
listBox1.SelectedItem.ToString() + "'";
cmd.Parameters.AddWithValue("#firstn", textBox_fn.Text);
cmd.Parameters.AddWithValue("#lastn", textBox_ln.Text);
cmd.ExecuteNonQuery();
fill_lb();
Check(); //<--added here
conn.Close();
}
Try resetting modified after DB update, at the end of button_modify_Click
I think I found the right way to do the job using the tag property.
first I add a new boolean that will check if I leave a textbox
bool left_txtbox = false; //when leaving textbox
then I add this code the the Leave property of textboxes
private void textBox_fn_Leave(object sender, EventArgs e)
{
name = listBox1.SelectedItem.ToString();
textBox_fn.Tag = textBox_fn.Text;
left_txtbox = true;
}
private void textBox_ln_Leave(object sender, EventArgs e)
{
name = listBox1.SelectedItem.ToString();
textBox_ln.Tag = textBox_ln.Text;
left_txtbox = true;
}
on listbox selected index change I add the check when leaving textbox
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (left_txtbox == true) Check(); // if txtbox has been left, do the check
textBox_fn.Text = string.Empty;
textBox_ln.Text = string.Empty;
if (conn.State != ConnectionState.Open) { conn.Close(); conn.Open(); }
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM [table1] WHERE firstn='" + listBox1.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
textBox_fn.Text = dr["firstn"].ToString();
textBox_ln.Text = dr["lastn"].ToString();
}
conn.Close();
}
finally the check itself, it will compare the Tag with the value stored in DB
private void Check()
{
if (conn.State != ConnectionState.Open) { conn.Close(); conn.Open(); }
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM [table1] WHERE firstn='" + name + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
try // ignore null values
{
if (textBox_fn.Tag.ToString() != dr["firstn"].ToString()) { modified = true; }
if (textBox_ln.Tag.ToString() != dr["lastn"].ToString()) { modified = true; }
}
catch { }
if (modified.Equals(true))
{
DialogResult dialogr = MessageBox.Show("Do you want to save change ? ", "", MessageBoxButtons.YesNo);
switch (dialogr)
{
case DialogResult.Yes:
button_savenew.PerformClick();
modified = false;
break;
case DialogResult.No:
modified = false;
return;
}
}
modified = false;
}
I think the code can be optimized
I have a combobox that loads data from database but i got an error that you cannot set the selectedValue in a ListControl Although i have set the selectValue to a Primary key of a Table. But still getting error on runtime.. Here is the Code..
private void FormAddStudent_Load(object sender, EventArgs e)
{
//For combobox Campuse
cBoxCampus.DataSource = GetAllCampuses();
cBoxCampus.DisplayMember = "campus_name";
cBoxCampus.SelectedValue = "campus_id";
//Foe ComboBox Department
cBoxDepartment.DataSource = GetAllDepartment();
cBoxDepartment.DisplayMember = "depname";
cBoxDepartment.SelectedValue = "depid";
}
and this is the code behind Insert Button
private void btnInsert_Click(object sender, EventArgs e)
{
string CS = ConfigurationManager.ConnectionStrings["UMSdbConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT ISNULL(MAX(std_id),0)+1 FROM Student", con);
cmd.CommandType = CommandType.Text;
tbID.Text = cmd.ExecuteScalar().ToString();
{
using (SqlCommand cmd1 = new SqlCommand("INSERT INTO Student (std_id,std_name,std_f_name,std_mob,std_gender,std_cnic,std_campus,std_dep,std_address,std_batch,std_batch_year)VALUES(#std_id,#std_name,#std_f_name,#std_mob,#std_gender,#std_cnic,#std_campus,#std_dep,#std_address,#std_batch,#std_batch_year)VALUES(#campus_id,#campus_name)", con))
{
cmd1.CommandType = CommandType.Text;
cmd1.Parameters.AddWithValue("#std_id", tbID.Text);
cmd1.Parameters.AddWithValue("#std_name", tbName.Text);
cmd1.Parameters.AddWithValue("#std_f_name", tbFatherName.Text);
cmd1.Parameters.AddWithValue("#std_mob", tbMobNumber.Text);
cmd1.Parameters.AddWithValue("#std_gender", GetGender());
cmd1.Parameters.AddWithValue("#std_cnic", tbMobNumber.Text);
cmd1.Parameters.AddWithValue("#std_campus",(cBoxCampus.SelectedIndex == -1) ? 0: cBoxCampus.SelectedValue);
cmd1.Parameters.AddWithValue("#std_dep", (cBoxDepartment.SelectedIndex == -1) ? 0 : cBoxDepartment.SelectedValue);
cmd1.Parameters.AddWithValue("#std_address", tbAddress.Text);
cmd1.Parameters.AddWithValue("#std_batch", tbBatchNo.Text);
cmd1.Parameters.AddWithValue("#std_batch_year", tbBatchYear.Text);
cmd1.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Saved");
}
}
}
}
Replace
cBoxCampus.SelectedValue = "campus_id";
With ListControl.ValueMember Property
cBoxCampus.ValueMember = "campus_id";
Do similar operation for cBoxDepartment
public void Bind_TimeSlots()
{
con.Open();
SqlCommand cmd = new SqlCommand("USP_GETAPPOINTMENTTIME", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#drid", SqlDbType.VarChar).Value = hdfid.Value;
cmd.Parameters.Add("#APPTDATE", SqlDbType.VarChar).Value = txtdate.Text;
SqlDataReader drAppointmentTimings = cmd.ExecuteReader();
rbtTimeSlots.DataSource = drAppointmentTimings;
rbtTimeSlots.Items.Clear();
rbtTimeSlots.DataTextField = "TimeSlot";
rbtTimeSlots.DataValueField = "id";
rbtTimeSlots.DataBind();
con.Close();
}
protected void btnAppointmentTime_Click(object sender, EventArgs e)
{
Bind_TimeSlots();
}
Here I've RadioButtonList.... and Bind in a Button Click event
Now after selecting the item in RadiButtonList, The item has to hide for next selection
I don't get the reason why you want to do this but :
First Approach
let's say Rating is the ID of the RadioButtonList
Yes, you can hide one by setting its Enabled property to false:
Rating.Items[0].Enabled = false;
Editing based on comment by OP.
To completely get rid of it you'll need to do this:
Rating.Items.RemoveAt(0);
and then when you want it back you'll need to do this:
Rating.Items.Insert(0, "0");
Second Approach
Use CSS i.e
RadioButtonList.Items(1).CssClass.Add("visibility", "hidden")
You can simply do select from text and also you can use FindByValue("val")
public void Bind_TimeSlots()
{
con.Open();
SqlCommand cmd = new SqlCommand("USP_GETAPPOINTMENTTIME", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#drid", SqlDbType.VarChar).Value = hdfid.Value;
cmd.Parameters.Add("#APPTDATE", SqlDbType.VarChar).Value = txtdate.Text;
SqlDataReader drAppointmentTimings = cmd.ExecuteReader();
rbtTimeSlots.DataSource = drAppointmentTimings;
rbtTimeSlots.Items.Clear();
rbtTimeSlots.DataTextField = "TimeSlot";
rbtTimeSlots.DataValueField = "id";
rbtTimeSlots.DataBind();
con.Close();
}
protected void btnAppointmentTime_Click(object sender, EventArgs e)
{
string selectedval = rbtTimeSlots.SelectedItem.Text; //if by value then SelectedValue.ToString()
Bind_TimeSlots();
if (rbtTimeSlots.Items.FindByText(selectedval) != null) //if by value then rbtTimeSlots.Items.FindByValue(selectedval)()
{
rbtTimeSlots.Items.FindByText(selectedval).Selected = true;
rbtTimeSlots.Items.FindByText(selectedval).Enabled = false;
}
}
I have searched a lot and failed to and finally arrived here...
I am creating a checkboxList dynamically from the database successfully. Then I want to submit these selcted checked items, I am unable to access. Please find the code....
protected void CreateCheckBoxListDynamically()
{
DataTable dt = new DataTable();
SqlConnection dBConnection = null;
try
{
dBConnection = new SqlConnection();
dBConnection.ConnectionString = ConfigurationManager.ConnectionStrings["***"].ConnectionString;
SqlDataAdapter dataAdapter = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("SP_GetDomesticCountryList", dBConnection);
cmd.CommandType = CommandType.StoredProcedure;
if (ddlTournamentType.SelectedValue != "Select" && ddlTournamentType.SelectedValue != "")
cmd.Parameters.Add("#TournamentId", SqlDbType.Int).Value = Convert.ToInt32(ddlTournamentType.SelectedValue);
else
cmd.Parameters.Add("#TournamentId", SqlDbType.Int).Value = DBNull.Value;
dBConnection.Open();
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dt);
if (dt.Rows.Count > 0)
{
CheckBoxList cblCountry = new CheckBoxList();
cblCountry.ID = "cblCountryTeam";
cblCountry.DataTextField = dt.Columns["CT_CountryTeamName"].ToString();
cblCountry.DataValueField = dt.Columns["CT_CountryTeamId"].ToString();
cblCountry.DataSource = dt;
cblCountry.DataBind();
//phCheckBoxList.Attributes.Add("class", "groupbox");
phCheckBoxList.Controls.Add(cblCountry);
phCheckBoxList.Controls.Add(new LiteralControl("<br />"));
}
dBConnection.Close();
}
catch (Exception Ex)
{
throw Ex;
}
finally
{
// Close data reader object and database connection
if (dBConnection.State == ConnectionState.Open)
dBConnection.Close();
}
}
protected void btnInsert_Click(object sender, EventArgs e)
{
SqlCommand cmd = null;
SqlConnection dBConnection = new SqlConnection();
SqlDataAdapter dataAdapter = new SqlDataAdapter();
string countryTeamIds = "";
try
{
dBConnection.ConnectionString = ConfigurationManager.ConnectionStrings["***"].ConnectionString;
cmd = new SqlCommand("SP_AddNewSeries", dBConnection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#MemberId", SqlDbType.Int).Value = memberId;
if (ddlTournamentType.SelectedValue != "Select")
cmd.Parameters.Add("#TournamentId", SqlDbType.Int).Value = Convert.ToInt32(ddlTournamentType.SelectedValue);
else
cmd.Parameters.Add("#TournamentId", SqlDbType.Int).Value = DBNull.Value;
if (!string.IsNullOrEmpty(txtSeriesStartDate.Text))
cmd.Parameters.Add("#SeriesStartDate", SqlDbType.Date).Value = Convert.ToDateTime(txtSeriesStartDate.Text);
else
cmd.Parameters.Add("#SeriesStartDate", SqlDbType.Date).Value = DBNull.Value;
if (!string.IsNullOrEmpty(txtSeriesEndDate.Text))
cmd.Parameters.Add("#SeriesEndDate", SqlDbType.Date).Value = Convert.ToDateTime(txtSeriesEndDate.Text);
else
cmd.Parameters.Add("#SeriesEndDate", SqlDbType.Date).Value = DBNull.Value;
// get values from dynamic controls
CheckBoxList cb = (CheckBoxList)phCheckBoxList.FindControl("cblCountryTeam");
if (cb != null)
{
foreach (ListItem li in cb.Items)
{
if (li.Selected)
countryTeamIds += li.Value + "~";
}
}
if (!string.IsNullOrEmpty(countryTeamIds))
cmd.Parameters.Add("#CountryTeamIds", SqlDbType.NVarChar).Value = countryTeamIds;
else
cmd.Parameters.Add("#CountryTeamIds", SqlDbType.NVarChar).Value = DBNull.Value;
dBConnection.Open();
dataAdapter.InsertCommand = cmd;
int i = cmd.ExecuteNonQuery();
//hdnseriesId.Value = cmd.Parameters["#SeriesId"].Value.ToString();
if (i == 0)
{
msgNoRecords.Visible = true;
}
else
{
msgNoRecords.Visible = false;
//Response.Redirect("~/country.aspx", false);
}
dBConnection.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
// Close data reader object and database connection
cmd.Dispose();
cmd = null;
if (dBConnection.State == ConnectionState.Open)
dBConnection.Close();
}
}
As per you code details object phCheckBoxList has created in aspx page.
If i am right then this will create problem adding checkboxs at server side.
why dont you create only checkbox at server side rather than checkboxLIST.
After creating checkbox you can add is <div>(this has to be declared at aspx page). this <div> should have an ID and make it runat="server" so you can get this.
After doing above task you can easy find every checkbox from that div at server side code.
I have a form wherein I populate content using stored procedure and I perform update actions on the content. Now my update button works fine. My problem is that each time I click on 'refresh' button in IE, the content gets updated and I don't want that to happen. I am new to .Net and all this ViewState stuff. Any help is appreciated..
Here is my code:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void BindGridView()
{
string constring = ConfigurationManager
.ConnectionStrings["shaConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("spd_pc", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.Parameters.Add("#city", SqlDbType.VarChar).Value = txt_city.Text;
con.Open();
IDataReader idr = cmd.ExecuteReader();
GridView1.DataSource = idr;
GridView1.DataBind();
idr.Close();
con.Close();
}
}
}
protected void Button1_click(object sender, EventArgs e)
{
BindGridView();
}
private void DeleteRecords(int id)
{
string constring = ConfigurationManager.
ConnectionStrings["shaConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("del_pc", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
conn.Open();
cmd.Parameters.Add("#id", SqlDbType.VarChar).Value = id;
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
protected void ButtonDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
CheckBox chkb = (CheckBox)row.FindControl("CheckBox1");
if (chkb.Checked)
{
int id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);
if (!(id.Equals(System.DBNull.Value)))
{
DeleteRecords(id);
}
}
}
BindGridView();
}
protected void Button2_Click(object sender, EventArgs e)
{
if (!IsCallback)
{
string active = "active";
string inactive = "inactive";
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkb = (CheckBox)row.FindControl("CheckBox1");
if (chkb.Checked)
{
int id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);
string status = row.Cells[5].Text;
if (!(id.Equals(System.DBNull.Value)))
{
if ((String.Equals(active, status)))
UpdateRecords(id);
if ((String.Equals(inactive, status)))
UpdateRecords(id);
}
}
}
}
}
private void UpdateRecords(int id)
{
string constring = ConfigurationManager.
ConnectionStrings["shaConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("upd_pc", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
conn.Open();
cmd.Parameters.Add("#id", SqlDbType.VarChar).Value = id;
cmd.ExecuteNonQuery();
conn.Close();
}
}
BindGridView();
}
}
yes, this will happen. the problem is you are refreshing the post event of the button click. the solution is to redirect back to page after the update is complete.
button_click(...)
{
//save to db
Response.Redirect(Request.Referrer);
}
or something like that.
Now if the user clicks refresh it will submit the GET request to load the page, rather the POST to issue the button click event.
If you search for Post Get Redirect (or something like that) you will find lots of articles on the topic. describing both the situation you encountered and why this solution works.