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;
}
}
Related
i want to update the datagridview list after clicking on button update here is image
when i select raw heading its shows on textboxes and after changing the value and clicking update its showing msg record updated successfully. here's image.
but its not updating in datagridview..
private void UpdateButton_Click(object sender, EventArgs e)
{
if (ValueTextBox.Text != "" && TypeTextBox.Text != "")
{
cmd = new OleDbCommand("update Sflorotype set Sflorovalues=#value,Sflorotypes=#type where ID=#id", con);
con.Open();
cmd.Parameters.AddWithValue("#id", ID);
cmd.Parameters.AddWithValue("#value", ValueTextBox.Text);
cmd.Parameters.AddWithValue("#type", TypeTextBox.Text);
var returnValue =
cmd.ExecuteNonQuery();
MessageBox.Show("Record Updated Successfully");
con.Close();
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Update");
}
}
OleDbDataAdapter adapt;
private void DisplayData()
{
con.Open();
DataTable dt = new DataTable();
adapt = new OleDbDataAdapter("select * from Sflorotype", con);
adapt.Fill(dt);
dataGridViewList.DataSource = dt;
con.Close();
}
private void ClearData()
{
ValueTextBox.Text = "";
TypeTextBox.Text = "";
ID = 0;
}
private void dataGridViewList_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
ID = Convert.ToInt32(dataGridViewList.Rows[e.RowIndex].Cells[0].Value.ToString());
ValueTextBox.Text = dataGridViewList.Rows[e.RowIndex].Cells[1].Value.ToString();
TypeTextBox.Text = dataGridViewList.Rows[e.RowIndex].Cells[2].Value.ToString();
}
Sorry can't add comment - I am new here.
cmd = new OleDbCommand("update Sflorotype set Sflorovalues=#value,Sflorotypes=#type where ID=#id", con);
Are you sure as ID is 94? (at your image, there is ID 94) Because I don't see where you get this value.
I found ID only here, and you set it to 0:
private void ClearData()
{
ValueTextBox.Text = "";
TypeTextBox.Text = "";
ID = 0;
}
I see only here some ID value. And if you don't change it elsewhere, it will update ID with value 0.
You can easily get info what you are executing:
string command = "update Sflorotype set Sflorovalues=#value,Sflorotypes=#type where ID=#id";
MessageBox.Show(command);
cmd = new OleDbCommand(command, con);
EDIT:
try this in your UpdateButton_Click:
cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update Sflorotype set Sflorovalues=#value,Sflorotypes=#type where ID=#id";
cmd.Parameters.AddWithValue("#value", ValueTextBox.Text);
cmd.Parameters.AddWithValue("#type", TypeTextBox.Text);
cmd.Parameters.AddWithValue("#id", ID);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery(); {
MessageBox.Show("Record Updated Successfully");
con.Close();
}
DisplayData();
ClearData();
Parameters.AddWithValue("#id", ID) - this should be the last one in adding parameters.
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
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;
I would like ask if you could help me with my codes as I don't get the result that I want which is to load the details of a particular paper from drop down list.
Currently, when the page loads, the details of the selected item will load up. But when I try to choose another item from the drop down list, corresponding details won't show up, the previous one still remains instead.
Under the properties of drop down list, I also set autopostback to yes so that it will automatically load the corresponding details of the item selected.
Please see codes below
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetPaper();
GetInk();
GetPaperDetails(ddlPaperName.SelectedItem.Value);
//pnlPrinting.Visible = true;
}
}
protected void btnCompute_Click(object sender, EventArgs e)
{
}
protected void btnCancel_Click(object sender, EventArgs e)
{
}
private void GetPaper()
{
con.Open();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText =
"SELECT * FROM Papers";
SqlDataReader data = com.ExecuteReader();
ddlPaperName.DataSource = data;
ddlPaperName.DataValueField = "PaperID";
ddlPaperName.DataTextField = "PaperName";
ddlPaperName.DataBind();
data.Close();
con.Close();
}
private void GetPaperDetails(string paper)
{
con.Open();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText =
"SELECT * FROM Papers WHERE PaperID=" + paper;
SqlDataReader data = com.ExecuteReader();
while (data.Read())
{
lblPaperPrice.Text = data["PaperPrice"].ToString();
lblDescription.Text = data["PaperDescription"].ToString();
lblSpecification.Text = data["PaperSpecification"].ToString();
imgPaper.ImageUrl = "../" + data["PaperImage"].ToString();
}
data.Close();
con.Close();
}
private void GetInk()
{
con.Open();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText =
"SELECT * FROM Inks";
SqlDataReader data = com.ExecuteReader();
ddlPaperName.DataSource = data;
ddlPaperName.DataValueField = "InkID";
ddlPaperName.DataTextField = "InkName";
ddlPaperName.DataBind();
while (data.Read())
{
lblInkPrice.Text = data["InkPrice"].ToString();
}
data.Close();
con.Close();
}
protected void ddlPaperName_SelectedIndexChanged(object sender, EventArgs e)
{
GetPaperDetails(ddlPaperName.SelectedItem.Value);
}
Looking forward to your feedback here. Thanks!
Set AutoPostBack property to true of your drop down list.
us this code, for that,
if (!IsPostBack == true)
{
drpdownaccountnamebind();
drpdowncountrynamebind();
}
i hope this will be helpful,
I have a button on my asp.net webpage which I have coded to read the value of a boolean column in an access database on pageload and what the button does changes according to whether the value of the column is true or false.
Essentially this button is a show/hide button for a product (click it to hide the product, or if already hidden click it to make it visible).
I'm getting some strange behavior as when the product is hidden, clicking to make the product visible works (updates the database), however, hiding it does not and I can't work out why one would work but the other wouldn't.
Code is as follows:
if (!IsPostBack)
try
{
s = WebConfigurationManager.ConnectionStrings["LNDatabase"].ConnectionString;
conn = new OleDbConnection(s);
cmd = new OleDbCommand("SELECT * FROM products WHERE products.prod_id = #test", conn);
OleDbParameter test = new OleDbParameter("#test", OleDbType.Integer);
test.Value = Request.QueryString["prod_id"];
cmd.Parameters.Add(test);
conn.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dr.Read();
title.Text = dr["shortdesc"].ToString();
description.Text = dr["longdesc"].ToString();
price.Text = dr["price"].ToString();
productcat = dr["cat"].ToString();
product_live = dr["live"].ToString();
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
dr.Close();
conn.Close();
}
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
{
bool prod_live_bool = Convert.ToBoolean(product_live);
if (prod_live_bool == true)
{
live_label.Text = "This product is visible to customers";
livelabelbutton.Text = "Hide this product";
livelabelbutton.Click += new EventHandler(this.hideproduct_click);
}
else
{
live_label.Text = "This product is not visible to customers";
livelabelbutton.Text = "Make this product visible";
livelabelbutton.Click += new EventHandler(this.showproduct_click);
}
}
protected void hideproduct_click(object sender, EventArgs e)
{
string prodid = Request.QueryString["prod_id"];
s = WebConfigurationManager.ConnectionStrings["LNDatabase"].ConnectionString;
string str = "UPDATE products SET live = #hide WHERE prod_id=#product";
using (OleDbConnection conn = new OleDbConnection(s))
{
using (OleDbCommand cmd = new OleDbCommand(str, conn))
{
OleDbCommand mycommand = new OleDbCommand();
OleDbParameter hideparam = new OleDbParameter("#hide", OleDbType.Boolean);
hideparam.Value = false;
cmd.Parameters.Add(hideparam);
OleDbParameter product = new OleDbParameter("#product", OleDbType.VarChar);
product.Value = prodid;
cmd.Parameters.Add(product);
conn.Open();
cmd.ExecuteNonQuery();
}
}
Response.Redirect(Request.RawUrl);
}
protected void showproduct_click(object sender, EventArgs e)
{
string prodid = Request.QueryString["prod_id"];
s = WebConfigurationManager.ConnectionStrings["LNDatabase"].ConnectionString;
string str = "UPDATE products SET live = #show WHERE prod_id=#product";
using (OleDbConnection conn = new OleDbConnection(s))
{
using (OleDbCommand cmd = new OleDbCommand(str, conn))
{
OleDbCommand mycommand = new OleDbCommand();
OleDbParameter hideparam = new OleDbParameter("#show", OleDbType.Boolean);
hideparam.Value = true;
cmd.Parameters.Add(hideparam);
OleDbParameter product = new OleDbParameter("#product", OleDbType.VarChar);
product.Value = prodid;
cmd.Parameters.Add(product);
conn.Open();
cmd.ExecuteNonQuery();
}
}
Response.Redirect(Request.RawUrl);
}
Sorry for the long code.
If the point of the command button is to toggle the value of the [live] Yes/No field, let the db engine do what you need.
UPDATE products SET live = (Not live) WHERE prod_id=#product
Not returns the inverse of a Boolean value. So Not True returns False and Not False returns True. Therefore the UPDATE statement sets live to the inverse of whatever it contained before executing the UPDATE. Try it in an Access session as a new query to examine how it operates.
If that is satisfactory, you wouldn't need separate routines for hide and show.