locked
upload file and date RRS feed

  • Question

  • User66371569 posted

    Hi I have grideview   inside grideview   have  uploadfile

    in row updating I am using  update query

    I want when I update and leave uploadfile null     update normally   I don't want error appears  " null reference"

    I want to write if upload file is empty    take path from  row called laebel5    else  do update query 

    my code

    Using sqlCon As SqlConnection = New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("PHPDPORTALDB").ToString())


    Dim quli As DropDownList = (TryCast(gvPhoneBook.Rows(e.RowIndex).FindControl("DropDownlist3"), DropDownList))
    Dim fupload As FileUpload = (TryCast(gvPhoneBook.Rows(e.RowIndex).FindControl("FileUpload2"), FileUpload))
    Dim afile As Label = (TryCast(gvPhoneBook.Rows(e.RowIndex).FindControl("Label5"), Label))

    Dim str4 As String = fupload.PostedFile.FileName
    str4 = System.IO.Path.GetFileName(str4)


    fupload.PostedFile.SaveAs(Server.MapPath("~/HpdQualification/") + str4)

    sqlCon.Open()
    Dim query As String = "update HpdQualification set Qualification=@Qualification,attachment=@attachment where id=@id"
    Dim sqlCmd As SqlCommand = New SqlCommand(query, sqlCon)
    sqlCmd.Parameters.AddWithValue("@Qualification", quli.SelectedValue.ToString)
    sqlCmd.Parameters.AddWithValue("@id", Convert.ToInt32(gvPhoneBook.DataKeys(e.RowIndex).Value.ToString()))


    sqlCmd.Parameters.AddWithValue("@attachment", afile.Text)
    sqlCmd.ExecuteNonQuery()
    gvPhoneBook.EditIndex = -1
    PopulateGridview()
    lblSuccessMessage.Text = "Selected Record Updated"
    lblErrorMessage.Text = ""
    '
    End Using

    second question I am using  code to get days deffrece between 2 dates

    cobnumbers.Text = DateDiff(DateInterval.Day, DateTime.ParseExact(startDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture), DateTime.ParseExact(endDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture))

    now I want to get years different between months  not days  how can I change it

    Thank you so much

    Friday, January 18, 2019 9:24 AM

Answers

  • User-1952516322 posted
    // jQuery
    function check() { if ($('#fileUploadID').val() != '') { alert('has file'); } else { alert('no files'); } }
    // If you want codebehind
    protected void check(object sender,EventArgs e) { if(fu.HasFile) { // Run Code } else { // Run Code } }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 19, 2019 1:52 PM

All replies

  • User66371569 posted

    I have fileupload    just  I want to  check if empty  then  below massage show  but I tried    always  error show     null reference    but  work fine when  I attach file

    ElseIf fupload.PostedFile.FileName = "" Then
    lblErrorMessage.Attributes("style") = "color:red; font-weight:bold;"
    lblErrorMessage.Text = "* Please attach your file."

    Saturday, January 19, 2019 7:01 AM
  • User-2054057000 posted

    You can use .val() method:

    if ($("#fileUpload").val()=="")
      alert("No file selected");

    Saturday, January 19, 2019 11:48 AM
  • User-1952516322 posted
    // jQuery
    function check() { if ($('#fileUploadID').val() != '') { alert('has file'); } else { alert('no files'); } }
    // If you want codebehind
    protected void check(object sender,EventArgs e) { if(fu.HasFile) { // Run Code } else { // Run Code } }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 19, 2019 1:52 PM
  • User-893317190 posted

    Hi thepast,

    Don't clear why you put your fileupload in a gridview's gridview. If your fileupload is in a gridview , you couldn't get the fileload by  (TryCast(gvPhoneBook.Rows(e.RowIndex).

    You should first find your girdview.

    Dim gird as GridView =(TryCast(gvPhoneBook.Rows(e.RowIndex).FindControl("id of your gridview"), GridView))

    And the find the fileupload through the gridview's row.

    Dim fupload As FileUpload = (TryCast(grid.Rows(index of the row where your fileupload is ).FindControl("FileUpload2"), FileUpload))

    If you want to ensure the fileupload has file, you could write the code below.


    If Not String.IsNullOrEmpty(fupload.FileName) Then
    // upload your file and do update query Else //  take path from  row called laebel5  End If

    About your second question, because  number of days in a month is not sure, the api doesn't provide the method.

    You should define how many days there are in a month (30,31 or 29) and divide the number of days between the two dates by  your customized number of days in a month.

    Best regards,

    Ackerly Xu

    Monday, January 21, 2019 2:33 AM