Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to solve the problem of repeated save in this code of asp.net with C#

Posted on 2012-08-15
4
Medium Priority
?
585 Views
Last Modified: 2012-08-28
bcc1.aspx.csbcc1.aspx  I have the following code attached in file.
The data gets saved on save button, and the form is reset. But if the user click on back button of browser he can again click on save. How to disable that or remove this problem so that on back button the values are not stored in textboxes.
0
Comment
Question by:searchsanjaysharma
  • 3
4 Comments
 
LVL 4

Expert Comment

by:Friman001
ID: 38299154
Go out and check to see if the record already exists.  If it does, disable the button.  Otherwise, allow the user to click the button.  One easy way to see if the record exists is to use a Primary Key, which would increment an ID value for each record!

Ryan F
0
 
LVL 4

Expert Comment

by:Friman001
ID: 38310567
Get at this code!

protected void btnsave_Click(object sender, EventArgs e)
    {
[b]if (record is already save and I can find the data)
{[/b]
        btnsave.Enabled = false;
        if (chkconfirm.Checked == true)
        {
            cmd = new SqlCommand("savebcc1", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = con;
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            cmd.Parameters.AddWithValue("@tsource",ddlsources.SelectedItem.Text.Trim());
            cmd.Parameters.AddWithValue("@tstate",ddlstates.SelectedItem.Text.Trim());
            cmd.Parameters.AddWithValue("@tdistrict",ddldistricts.SelectedItem.Text.Trim());
            cmd.Parameters.AddWithValue("@tvillage", ddlvillages.SelectedItem.Text.Trim());
            cmd.Parameters.AddWithValue("@tid",Convert.ToInt32(txtuin.Text.Trim()));
            cmd.Parameters.AddWithValue("@tchv_name",txtchv_name.Text.Trim());
            cmd.Parameters.AddWithValue("@tblock",txtblock.Text.Trim());
            
            cmd.Parameters.AddWithValue("@tphc_chc_name",txtphcchcname.Text.Trim());
            cmd.Parameters.AddWithValue("@tsub_centre", txtsubcentre.Text.Trim());
            cmd.Parameters.AddWithValue("@tccid",Convert.ToInt32(ddlcc.SelectedItem.Text.Trim()));  
            cmd.Parameters.AddWithValue("@tccname",txtccname.Text.Trim());
            cmd.Parameters.AddWithValue("@tmonth",txtmonth.Text.Trim());
            cmd.Parameters.AddWithValue("@tyear", txtyear.Text.Trim());
            cmd.Parameters.AddWithValue("@tdoi", txtdoi.Text.Trim());
            if(ddlinfotainmenttype.SelectedItem.Text=="Any other please specify")
            {
                cmd.Parameters.AddWithValue("@ttypeofinfotainment", txtotherinfotainment.Text.Trim());
            }
            else if(ddlinfotainmenttype.SelectedItem.Text!="Any other please specify")
            {
                cmd.Parameters.AddWithValue("@ttypeofinfotainment", ddlinfotainmenttype.SelectedItem.Text.Trim());
            }
            else
            {
                cmd.Parameters.AddWithValue("@ttypeofinfotainment", ddlinfotainmenttype.SelectedItem.Text.Trim());
            }
            if(ddlinfotainmentpoint.SelectedItem.Text.Trim()=="Any other please specify")
            {
                cmd.Parameters.AddWithValue("@tpointofinfotainment", txtpointofinfotainment.Text.Trim());
            }
            else if (ddlinfotainmentpoint.SelectedItem.Text.Trim()!="Any other please specify")
            {
                cmd.Parameters.AddWithValue("@tpointofinfotainment", ddlinfotainmentpoint.SelectedItem.Text.Trim());
            }
            else
            {
                cmd.Parameters.AddWithValue("@tpointofinfotainment", ddlinfotainmentpoint.SelectedItem.Text.Trim());
            }

            if (txtnopmale.Text == "")
            {
                cmd.Parameters.AddWithValue("@tmale", Convert.ToInt32("0"));
            }
            else if(txtnopmale.Text!="")
            {
                cmd.Parameters.AddWithValue("@tmale",txtnopmale.Text.Trim());
            }
            else
            {
                   cmd.Parameters.AddWithValue("@tmale", Convert.ToInt32("0"));
            }
            if(txtnopfemale.Text=="")
            {
            cmd.Parameters.AddWithValue("@tfemale", Convert.ToInt32("0"));
            }
            else if(txtnopfemale.Text!="")
            {
                cmd.Parameters.AddWithValue("@tfemale", Convert.ToInt32(txtnopfemale.Text.Trim()));
            }
            else
            {
                cmd.Parameters.AddWithValue("@tfemale", Convert.ToInt32("0"));
            }
            cmd.Parameters.AddWithValue("@tmcap", Convert.ToInt32(txtqtycap.Text.Trim()));
            cmd.Parameters.AddWithValue("@tmleaflet",Convert.ToInt32(txtqtyleaflet.Text.Trim()));
            if(txtmatertialothers.Text.Trim()!="")
            {
                cmd.Parameters.AddWithValue("@tmothers", txtmatertialothers.Text.Trim());
                cmd.Parameters.AddWithValue("@tqtyothers", Convert.ToInt32(txtqtyothers.Text.Trim()));
            }
            else if(txtmatertialothers.Text.Trim()=="")
            {
                cmd.Parameters.AddWithValue("@tmothers", "");
                cmd.Parameters.AddWithValue("@tqtyothers", Convert.ToInt32("0"));
            }
            else
            {
                cmd.Parameters.AddWithValue("@tmothers", "");
                cmd.Parameters.AddWithValue("@tqtyothers", Convert.ToInt32("0"));
            }
            
            





            cmd.ExecuteNonQuery();
            Response.Write("<script>alert('Record Saved Successfully')</script>");
            con.Close();
            lockall();
        }
    }
[b]}[/b]

Open in new window

0
 
LVL 4

Accepted Solution

by:
Friman001 earned 1000 total points
ID: 38310570
This way, if a user goes back and the code executes again, it will see the data is already saved.  You could also look into a sort of Data Validation.  There is something out there that can see if the data has been change, and if it has, it will allow you to notify the users.
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 38344247
ok
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question