0

I am assigning the value to the variable from the text box on the page during the pageupload event of AjaxFileUpload1.The problem is that, I am not getting the value from the text box to my variable even though no error throws.My variables are

    string scn = txtSCN.Text;
    string line1 = txtLineitem.Text;
    string aging1 = txtAging.Text;  

Any idea why AjaxFileUpload1_UploadComplete is not able to read text box value

My cs Code is:

protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
    string c = System.IO.Path.GetFileName(e.FileName);
    string dpath = "~/Profile/Images/";
    string scn = txtSCN.Text;
    string line1 = txtLineitem.Text;
    string aging1 = txtAging.Text;             
    AjaxFileUpload1.SaveAs(MapPath(Path.Combine(dpath,c)));
    dpath = dpath + c;
    string str1 = ConfigurationManager.ConnectionStrings["ProTracConnGMCH"].ConnectionString;
    SqlConnection cn = new SqlConnection(str1);
    cn.Open();
    string sql = "Update tbNoquoteFollowupupdate set MailreceivedURL = '" + dpath + "', chkMailreceived = 1 , Buyername =  '" + buyername + "'  where scn = '" + scn + "' AND lineItem = '" + line1 + "' and Aging ='" + aging1 + "' ";
    SqlCommand cmd = new SqlCommand(sql, cn);
    int i = cmd.ExecuteNonQuery();

    if (i > 0)
    {
        // AjaxFileUpload1.SaveAs(Path.Combine(dpath, e.FileName));
        //AjaxFileUpload1.SaveAs(MapPath(dpath));
    }
    cn.Close();
    BindGridviewData1();
    cn.Open();
    string cmd2 = "Insert Into tbMulitmailsreived (scn, lineItem,followupdate, Aging,MailreceivedURL) Values ('" + scn + "', '" + line1 + "','" + DateTime.Now + "','" + aging1 + "','" + dpath + "')";
    SqlCommand sqlCommand2 = new SqlCommand(cmd2, cn);
    sqlCommand2.ExecuteNonQuery();
    cn.Close();
}
Sergio
  • 6,900
  • 5
  • 31
  • 55
FaisalThayyil
  • 85
  • 1
  • 3
  • 14
  • 1
    I would advise you to read on [parameterized SQL](http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html) – Wahtever Feb 18 '14 at 13:15
  • You are not able to retrieve the textbox values, because it is a `partial post-back` and hence `ViewState` will not be available for the controls. You can use sessions to grab the textbox values on earlier post-backs. – NaveenBhat Feb 18 '14 at 13:33

2 Answers2

1

Please put the values from text box's into session and since you can reach the session variables from the UpLoadComplete everything will work in this way.

Aftab Ahmed
  • 1,727
  • 11
  • 15
0

You can customize AjaxFileUpload control and pass text boxes values to UploadCompleted event handler as below:

function uploadStarted(sender, args) {
     var latitude = $get("<%= tbUploaderLat.ClientID %>").value;
     var longitude = $get("<%= tbUploaderLon.ClientID %>").value;
     sender.contextKeys = { "latitude": latitude, "longitude": longitude };
}​

After that, you can get latitude & longitude values in UploadComplete handler:

    protected void AjaxFileUpload1_OnUploadComplete(object sender, AjaxFileUploadEventArgs file)
    {
       if (!string.IsNullOrEmpty(file.ContextKeys))
       {
            var longLat = new     System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Dictionary<string,   string>>(file.ContextKeys);
           var longitude = longLat["longitude"];
           var latitude = longLat["latitude"];
       }

       //code to save file

   }
Aftab Ahmed
  • 1,727
  • 11
  • 15