I want to avoid postback when my button event fired. Due to postback my List gets empty again. Kindly Help me out with this. What should i do to avoid postback. I am using ASP button.
Here is my button method
id = Convert.ToInt32(Request.QueryString["id"].ToString());
venues.Add(id);
foreach (int item1 in venues)
{
if (!hashset.Add(item1))
{
Response.Write("<script>alert('Duplicate Items');</script>");
break;
}
else
{
foreach (var items in venues)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from Venues where VenueID = '" + items + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter ds = new SqlDataAdapter(cmd);
ds.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
Name = dr["Name"].ToString();
Address = dr["Address"].ToString();
Picture = dr["Picture"].ToString();
Price = dr["Price"].ToString();
}
con.Close();
if (Request.Cookies["aa"] == null)
{
Response.Cookies["aa"].Value = Name.ToString() + "," + Address.ToString() + "," + Picture.ToString() + "," + Price.ToString();
Response.Cookies["aa"].Expires = DateTime.Now.AddDays(1);
}
else
{
Response.Cookies["aa"].Value = Request.Cookies["aa"].Value + "|" + Name.ToString() + "," + Address.ToString() + "," + Picture.ToString() + "," + Price.ToString();
Response.Cookies["aa"].Expires = DateTime.Now.AddDays(1);
}
}
}
}
}
When I m writing this query in the code on button click to insert the mkey in the xxacl_pn_new_cha_part_h table
it gives me error as
"ORA-00904: "A": invalid identifier"
Here is my code:-
try
{
conn.Open();
OracleDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
cmd.CommandText = "insert into xxacl_pn_new_cha_part_h select sysdate, a.* from xxacl_pn_new_cha_part where mkey= " + sdr[0].ToString(); // this query gives error
cmd.ExecuteNonQuery();
strQuery = "UPDATE xxacl_pn_new_cha_part set Rating='" + sdr[2].ToString() + "', RATING_UPDATE_DATE=convert(datetime,'" + System.DateTime.Now.ToString() + "',103) WHERE mkey = " + sdr[0].ToString();
cmd.CommandText = strQuery;
cmd.ExecuteNonQuery();
}
ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage", "alert('Broker rating updated succesfully');", true);
}
I am using Oracle
UPDATE
protected void btnUpdate_Click(object sender, EventArgs e)
{
OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["OracleConn"].ConnectionString);
string strQuery = "SELECT distinct ab.mkey, ab.broker_id,CASE WHEN SYSDATE - la.creation_date <= 180 THEN 'A' WHEN SYSDATE - cef_dt <= 30 THEN " +
"'B' ELSE 'C'END rating FROM xxacl_pn_new_cha_part ab,xxacl_pn_lease_det ld,xxacl_pn_leases_all la, " +
"xxcus.xxacl_pn_customer_enquiry_v ce WHERE ab.broker_id = ld.broker_id(+) AND ld.booking_no = la.booking_no(+) " +
" AND ab.broker_id = ce.broker_id(+)";
OracleCommand cmd = new OracleCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = conn;
try
{
conn.Open();
OracleDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
cmd.CommandText = "insert into xxacl_pn_new_cha_part_h select sysdate, a.* from xxacl_pn_new_cha_part a where a.mkey= " + sdr[0].ToString();
cmd.ExecuteNonQuery();
strQuery = "UPDATE xxacl_pn_new_cha_part set Rating='" + sdr[2].ToString() + "', RATING_UPDATE_DATE=convert(datetime,'" + DateTime.Now.ToString() + "',103) WHERE mkey = " + sdr[0].ToString();
cmd.CommandText = strQuery;
cmd.ExecuteNonQuery();
}
ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage", "alert('Broker rating updated succesfully');", true);
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
conn.Dispose();
}
}
You are missing the alias name here, try this one
cmd.CommandText = "insert into xxacl_pn_new_cha_part_h select sysdate, a.* from xxacl_pn_new_cha_part a where a.mkey= " + sdr[0].ToString();
UPDATE:
protected void btnUpdate_Click(object sender, EventArgs e)
{
OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["OracleConn"].ConnectionString);
string strQuery = "SELECT distinct ab.mkey, ab.broker_id,CASE WHEN SYSDATE - la.creation_date <= 180 THEN 'A' WHEN SYSDATE - cef_dt <= 30 THEN " +
"'B' ELSE 'C'END rating FROM xxacl_pn_new_cha_part ab,xxacl_pn_lease_det ld,xxacl_pn_leases_all la, " +
"xxcus.xxacl_pn_customer_enquiry_v ce WHERE ab.broker_id = ld.broker_id(+) AND ld.booking_no = la.booking_no(+) " +
" AND ab.broker_id = ce.broker_id(+)";
OracleCommand cmd = new OracleCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = conn;
try
{
conn.Open();
OracleDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
cmd.CommandText = "insert into xxacl_pn_new_cha_part_h select 0, sysdate, a.* from xxacl_pn_new_cha_part a where a.mkey= " + sdr[0].ToString();
cmd.ExecuteNonQuery();
strQuery = "UPDATE xxacl_pn_new_cha_part set Rating='" + sdr[2].ToString() + "', RATING_UPDATE_DATE=to_date('" + DateTime.Now.ToString() + "','dd-mm-yyyy hh:mi:ss am') WHERE mkey = " + sdr[0].ToString();
cmd.CommandText = strQuery;
cmd.ExecuteNonQuery();
}
ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage", "alert('Broker rating updated succesfully');", true);
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
conn.Dispose();
}
}
You missed the alias name of a table
"insert into xxacl_pn_new_cha_part_h select sysdate, a.* from xxacl_pn_new_cha_part a where a.mkey= " + sdr[0].ToString();
I need help with "retaining" values from one page to another example i answered the first page and proceed to the 2nd if i click "previous page" how can i keep my answers there.
Below are my codes:
protected void Button1_Click(object sender, EventArgs e)
{
//string sesrbl1;
string selectedvalue = RadioButtonList2.SelectedValue.ToString();
string selectedvalue2 = RadioButtonList4.SelectedValue.ToString();
string selectedvalue3 = RadioButtonList6.SelectedValue.ToString();
string selectedvalue4 = RadioButtonList8.SelectedValue.ToString();
string selectedvalue5 = RadioButtonList14.SelectedValue.ToString();
string selectedvalue6 = RadioButtonList11.SelectedValue.ToString();
bool risk;
SqlCommand cmd = new SqlCommand();
SqlConnection sqlcon = new SqlConnection("Data Source=DBASE;Initial Catalog=TumorRegistry;User ID=sa");
try
{
//Session["rb1"] = RadioButtonList12.SelectedValue;
//Session["rb2"] = RadioButtonList3.SelectedValue;
//Session["rb3"] = RadioButtonList5.SelectedValue;
//Session["rb4"] = RadioButtonList7.SelectedValue;
//Session["rb5"] = RadioButtonList13.SelectedValue;
if (RadioButtonList12.SelectedItem.Text== "FALSE")
{
if (bool.TryParse("False", out risk))
{
risk = false;
Session["rb1"] = risk;
}
}
if (RadioButtonList12.SelectedItem.Text == "TRUE")
{
if (bool.TryParse(RadioButtonList12.SelectedItem.Text, out risk))
{
risk = true;
Session["rb1"] = risk;
}
}
if (RadioButtonList3.SelectedItem.Text == "FALSE")
{
if (bool.TryParse(RadioButtonList3.SelectedItem.Text, out risk))
{
risk = false;
Session["rb2"] = risk;
}
}
if (RadioButtonList3.SelectedItem.Text== "TRUE")
{
if (bool.TryParse(RadioButtonList3.SelectedItem.Text, out risk))
{
risk = true;
Session["rb2"] = risk;
}
}
if (RadioButtonList5.SelectedItem.Text == "FALSE")
{
if (bool.TryParse(RadioButtonList5.SelectedItem.Text, out risk))
{
risk = false;
Session["rb3"] = risk;
}
}
if (RadioButtonList5.SelectedItem.Text == "TRUE")
{
if (bool.TryParse(RadioButtonList5.SelectedItem.Text, out risk))
{
risk = true;
Session["rb3"] = risk;
}
}
if (RadioButtonList7.SelectedItem.Text == "FALSE")
{
if (bool.TryParse(RadioButtonList7.SelectedItem.Text, out risk))
{
risk = false;
Session["rb4"] = risk;
}
}
if (RadioButtonList7.SelectedItem.Text == "TRUE")
{
if (bool.TryParse(RadioButtonList7.SelectedItem.Text, out risk))
{
risk = true;
Session["rb4"] = risk;
}
}
if (RadioButtonList13.SelectedItem.Text == "FALSE")
{
if (bool.TryParse(RadioButtonList13.SelectedItem.Text, out risk))
{
risk = false;
Session["rb5"] = risk;
}
}
if (RadioButtonList13.SelectedItem.Text == "TRUE")
{
if (bool.TryParse(RadioButtonList13.SelectedItem.Text, out risk))
{
risk = true;
Session["rb5"] = risk;
}
}
if (selectedvalue.ToString() == "0")
{
selectedvalue = null;
}
else if (selectedvalue2.ToString() == "0")
{
selectedvalue2 = null;
}
else if (selectedvalue3.ToString() == "0")
{
selectedvalue3 = null;
}
else if (selectedvalue4.ToString() == "0")
{
selectedvalue4 = null;
}
else if (selectedvalue5.ToString() == "0")
{
selectedvalue6 = null;
}
//Session["rb1"] = RadioButtonList12.SelectedValue;
//Session["rb2"] = RadioButtonList3.SelectedValue;
//Session["rb3"] = RadioButtonList5.SelectedValue;
//Session["rb4"] = RadioButtonList7.SelectedValue;
//Session["rb5"] = RadioButtonList13.SelectedValue;
//sending values to the submit button next page
Session["Holdtextbox1"] = RadioButtonList2.SelectedValue.ToString();
Session["Holdtextbox2"] = RadioButtonList4.SelectedValue.ToString();
Session["Holdtextbox3"] = RadioButtonList6.SelectedValue.ToString();
Session["Holdtextbox4"] = RadioButtonList8.SelectedValue.ToString();
Session["Holdtextbox5"] = RadioButtonList14.SelectedValue.ToString();
Session["Holdtextbox6"] = RadioButtonList11.SelectedValue.ToString();
selectedvalue = Session["Holdtextbox1"].ToString();
selectedvalue2 = Session["Holdtextbox2"].ToString();
selectedvalue3 = Session["Holdtextbox3"].ToString();
selectedvalue4 = Session["Holdtextbox4"].ToString();
selectedvalue5 = Session["Holdtextbox5"].ToString();
selectedvalue6 = Session["Holdtextbox6"].ToString();
Response.Redirect("WebForm1.aspx");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
that's for the first page
here's for the 2nd page i have 2 buttons button 1 and 2
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
////Button1.Attributes.Add("onClick", "javascript:history.back(); return false;");
// Response.Redirect("Default.aspx");
}
public void SaveRecord()
{
try
{
Holdtextbox1.Text = Session["Holdtextbox1"].ToString();
Holdtextbox2.Text = Session["Holdtextbox2"].ToString();
Holdtextbox3.Text = Session["Holdtextbox3"].ToString();
Holdtextbox4.Text = Session["Holdtextbox4"].ToString();
Holdtextbox5.Text = Session["Holdtextbox5"].ToString();
Holdtextbox6.Text = Session["Holdtextbox6"].ToString();
sqlcon.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlcon;
SqlDataAdapter DA = new SqlDataAdapter();
// string SqlInsert = "Insert into tbTRcBase (HPN,ISH, Asthma, DM, OtherCo,HPNTreatment, ISHTreatment,AsthmaTreatment, DMTreatment, OtherCoTreatment,SecondHandSmoke,Smoker, StopSmoking , Occupation , CancerFamilyHistory, FamilyWithCancer,ParentWithCancer) values ('" + bitsess.Text + "','" + bitsess2.Text + "','" + bitsess3.Text + "','" + bitsess4.Text + "','" + bitsess5.Text + "','" + Holdtextbox1.Text + "','" + Holdtextbox2.Text + "','" + Holdtextbox3.Text + "','" + Holdtextbox4.Text + "','" + Holdtextbox5.Text + "','" + Holdtextbox6.Text + "','" + rb1.SelectedValue.ToString() + "','" + rb2.SelectedValue.ToString() + "','" + cb1.Text + "','" + rb3.Text + "','" + cb2.Text + "','" + rb4.Text + "')";
string SqlInsert = "Insert into tbTRcBase (HPN,ISH, Asthma, DM, OtherCo,HPNTreatment, ISHTreatment,AsthmaTreatment, DMTreatment, OtherCoTreatment,SecondHandSmoke,Smoker, StopSmoking , Occupation , CancerFamilyHistory, FamilyWithCancer,ParentWithCancer) values ('" + Session["rb1"] + "','" + Session["rb2"] + "','" + Session["rb3"] + "','" + Session["rb4"] + "','" + Session["rb5"] + "','" + Holdtextbox1.Text + "','" + Holdtextbox2.Text + "','" + Holdtextbox3.Text + "','" + Holdtextbox4.Text + "','" + Holdtextbox5.Text + "','" + Holdtextbox6.Text + "','" + rb1.SelectedValue.ToString() + "','" + rb2.SelectedValue.ToString() + "','" + cb1.Text + "','" + rb3.Text + "','" + cb2.Text + "','" + rb4.Text + "')";
// string SqlInsert = "Insert into tbTRcBase (HPN, HPNTreatment) values ('" + Session["rb1"] + "','" + Holdtextbox1.Text + "')";
DA = new SqlDataAdapter(SqlInsert, sqlcon);
DataTable dt = new DataTable();
DA.Fill(dt);
sqlcon.Close();
Response.Write("<script language='javascript'>alert('" + " Data has been saved " + "')</script>");
}
catch (Exception e)
{
Response.Write("<script language='javascript'>alert('" + e.Message + "')</script>");
}
}
For retaining the values on the previous page, you would have to make use of those Session variables that you have stored on the same page before you pass them onto the next page.
On the Page_Load event of the previous page, make a function that just binds the respective controls based on the values of those Session variables.
Something like this :
public void BindData()
{
Holdtextbox1.Text = Session["Holdtextbox1"].ToString();
.
.
.
//all your controls and the Session values.
}
Hope this helps.
the date in excel is of the date time format and the selected date is also date time format but wots the problem
OleDbDataAdapter da1 = new OleDbDataAdapter("SELECT [ProjectId],[ProjectName],[ManagerID],[ManagerName],[AllocationStartDate],[AllocationEndDate],[horizontalshortname] FROM [" + getExcelSheetName + #"] where [horizontalshortname]="+ "'" + TextBox_Horizontal.Text + "'"
+ " AND [Isonsite]=" + "'" + DropDownList_Location.SelectedItem.Text + "'"
+ " AND [AllocationStartDate]>="+Calendar1.SelectedDate
+ " AND [AllocationEndDate]<="+Calendar2.SelectedDate
, conn);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
//if (ds.Tables[0] != null )
if (ds1.Tables[0].Rows.Count > 0)
{
GridView_Utilization_Search.Visible = true;
GridView_Utilization_Search.DataSource = ds1.Tables[0];
GridView_Utilization_Search.DataBind();
}
else
{
GridView_Utilization_Search.Visible = false;
}
the coding
foreach (TreeNode item in this.TreeView1.CheckedNodes)
{
if (item.Checked == true)
{
int strTreeValue = Convert.ToInt32(item.Value);
SqlCommand com = new SqlCommand("Update Role_Menu Set Role_id=('" + roleid + "')
Where Menu_id=('" + strTreeValue + "')", con);
com.ExecuteNonQuery();
da = new SqlDataAdapter(com);
ds = new DataSet();
da.Fill(ds);
if (ds.Tables.Count == 0)
{
com = new SqlCommand("insert into Role_Menu(Role_id,Menu_id)
values('" + roleid + "','" + strTreeValue + "')", con);
com.ExecuteNonQuery();
}
}
in the above if(ds.tables.count == 0) i am trying to find if the rows have been updated or not but it is wrong and does not work. if a row is updated then how can we come to know it is updated and which one is updated.
You can use the code as follow
foreach (TreeNode item in this.TreeView1.CheckedNodes)
{
if (item.Checked == true)
{
int strTreeValue = Convert.ToInt32(item.Value);
SqlCommand com = new SqlCommand("Update Role_Menu Set Role_id=('" + roleid + "')
Where Menu_id=('" + strTreeValue + "')", con);
int i=com.ExecuteNonQuery();
if (i<= 0)
{
com = new SqlCommand("insert into Role_Menu(Role_id,Menu_id)
values('" + roleid + "','" + strTreeValue + "')", con);
com.ExecuteNonQuery();
}
}
Edit 1
You don't need to use SqlDataAdapter and DataSet
If you run the SQL from your question in a SqlCommand and check the return value of ExecuteNonQuery it should tell you how many records were affected.
From the documentation:
Return Value
Type: System.Int32
The number of rows affected.