Bhavesh Vankar

Bhavesh Vankar

  • 1.5k
  • 511
  • 10.2k

Confirm message while record save in web c#

Dec 31 2020 10:27 AM
below is my code for record save in database but when i save record in being save without confirm message. i want to show first validation if any control is empty and when all the fields are filled and press save button confirm message should be fire and ask to save record. kidnly help me to solve this. where is error. 
 
record directly being save without confrm message. i want to fire confirm message using code behind i have used validation control to validate all the fileds. i dont want to use javascrit confirm function. kindly suggest me c# code begind for the web application. 
 
  1. if (IsPostBack)  
  2.            {   
  3.            if (Page.IsValid)  
  4.            {  
  5.                string image = simagehidden.Value;  
  6.                    if (string.IsNullOrEmpty(image))  
  7.                    {  
  8.                        lblimgerror.Visible = true;  
  9.                        lblimgerror.Text = "*";  
  10.                        lblimgerror.ForeColor = System.Drawing.Color.Red;  
  11.                    }  
  12.                    else  
  13.                    {  
  14.                        var WebClient = new WebClient();  
  15.                        byte[] imgbytes = WebClient.DownloadData(image);  
  16.                        MemoryStream ms = new MemoryStream();  
  17.                        byte[] imgtobinary = new byte[ms.Length];  
  18.                        ms.Position = 0;  
  19.                        ms.Read(imgtobinary, 0, (int)ms.Length);  
  20.   
  21.                        string _imgpath = Path.Combine(Server.MapPath("~/CroppedImage/" + DateTime.Now.ToString("dd-MM-yy")));  
  22.                        sImage.ImageUrl = _imgpath;  
  23.                        SqlConnection con = new SqlConnection(this.con);  
  24.                        SqlCommand cmd = new SqlCommand("studentrec_test", con);  
  25.                        cmd.CommandType = CommandType.StoredProcedure;  
  26.                        cmd.Parameters.AddWithValue("@S_ID", TxtAutovID.Text);  
  27.                        cmd.Parameters.AddWithValue("@S_NAMEOFVISITOR", txtfname.Text + " " + txtmname.Text + " " + txtlname.Text);  
  28.                        cmd.Parameters.AddWithValue("@S_ADDRESS", txtaddress.Text);  
  29.                        cmd.Parameters.AddWithValue("@S_CITY", txtcity.Text);  
  30.                        cmd.Parameters.AddWithValue("@S_TALUKA", txttaluka.Text);  
  31.                        cmd.Parameters.AddWithValue("@S_DIST", txtdistric.Text);  
  32.                        cmd.Parameters.AddWithValue("@S_PINCODE", txtpincode.Text);  
  33.                        cmd.Parameters.AddWithValue("@S_GENDER", DDgender.SelectedItem.ToString());  
  34.                        cmd.Parameters.AddWithValue("@S_BIRTHDATE", txtbod.Text);  
  35.                        cmd.Parameters.AddWithValue("@S_IDTYPE", dropidtype.SelectedItem.ToString());  
  36.                        cmd.Parameters.AddWithValue("@S_IDREF", txtidno.Text);  
  37.                        cmd.Parameters.AddWithValue("@S_PHONE", txtphoneno.Text);  
  38.                        cmd.Parameters.AddWithValue("@S_AGE", txtage.Text);  
  39.                        cmd.Parameters.AddWithValue("@S_EMPDEPARTMENT", dropdepartment.SelectedItem.ToString());  
  40.                        cmd.Parameters.AddWithValue("@S_PURPOSE", txtpurpose.Text);  
  41.                        cmd.Parameters.AddWithValue("@S_TOMEET", txtmeet.Text);  
  42.                        cmd.Parameters.AddWithValue("@S_EMPDESIGNATION", dropdesignation.SelectedItem.ToString());  
  43.                        cmd.Parameters.AddWithValue("@S_BLOCKNO", txtblock.Text);  
  44.                        cmd.Parameters.AddWithValue("@S_FLOORNO", txtfloor.Text);  
  45.                        cmd.Parameters.AddWithValue("@S_VEHICLE", checkvehicale.Checked);  
  46.                        cmd.Parameters.AddWithValue("@S_VEHICLENO", txtvehicleno.Text);  
  47.                        cmd.Parameters.AddWithValue("@S_VEHICLETYPE", dropvehicletype.SelectedItem.ToString());  
  48.                        cmd.Parameters.AddWithValue("@S_IMAGE", imgtobinary);  
  49.   
  50.                        string MainDir = @"~/StudentImage/";  
  51.                        String SubDir = DateTime.Now.ToString("dd-MM-yyyy");  
  52.   
  53.                        if (!Directory.Exists(Server.MapPath(MainDir + SubDir + "/")))  
  54.                        {  
  55.                            Directory.CreateDirectory(Server.MapPath(MainDir + SubDir + "/"));  
  56.                        }  
  57.                        string imgname = (TxtAutovID.Text + ".jpg");  
  58.                        File.WriteAllBytes(Path.Combine(Server.MapPath(MainDir + SubDir), imgname), imgbytes);  
  59.   
  60.                        cmd.Parameters.AddWithValue("@V_REMARKS", txtremark.Text);  
  61.                        cmd.Parameters.AddWithValue("@v_username""user"/* Session["User"].ToString()*/);  
  62.                        cmd.Parameters.AddWithValue("@v_photoflag"true);  
  63.                        cmd.Parameters.AddWithValue("@v_gateno""5");  
  64.                         
  65.                        ClientScriptManager CSM = Page.ClientScript;  
  66.                        if (ReturnValue())  
  67.                        {  
  68.                            string strconfirm = "<script>if(window.confirm('Do You Want To Save Record.?'))</script>";  
  69.                            CSM.RegisterClientScriptBlock(this.GetType(), "Confirm", strconfirm, true);  
  70.                        }  
  71.                        con.Open();  
  72.                        cmd.ExecuteNonQuery();  
  73.   
  74.   
  75.                        generateautonumber();  
  76.                        FillListBox();  
  77.                        sImage.ImageUrl = null;  
  78.                        ClearControls();  
  79.                        con.Close();  
  80.                    }  
  81.                }  
  82.            }  
 

Answers (7)