How do I get and insert data from vb code

I have upload functionality in my asp.net application and everything is working fine.
I want to store the data and time every time a file gets uploaded to the server. Does any one know how to do this from the VB file (passing the parameter config_key)

There are 2 tables in my SQL:

Upload_Config table:
Config_key (PK)
File_Name
File_Type_Key (FK)
File_Title
File_Description
File_Path
Auto_Email
Active

The second table is where I want to store a date and time every time an upload occurs for a file:

Upload_History table:
Config_Key (FK)
Uploaded_DateTime

The tableview is written in the VB file.

My aspx file looks like this:


<asp:Content ID="cMain" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
<div id="body" >
    <div class="container">
        <h3>Upload : Upload Files </h3>
        <div class="spacer">&nbsp;</div>  
        <asp:Table ID="tUpload" runat="server">
        </asp:Table>
        <asp:Label ID="lblError" runat="server" Text="" Visible="false"></asp:Label>
    </div>
</div>
</asp:Content>
Private Sub Upload_Click(ByVal source As Object, ByVal e As System.EventArgs)
 
        Dim Config_Key As String = CType(source, Button).CommandArgument.ToString
        Dim DA As New DataAccess
        Dim DS As New DataSet
 
        Dim q As String
 
        q = "View_Upload_Config_By_Config_Key " + Config_Key
 
        Try
            DA.RunQueryDataSetResult(DS, ConfigurationManager.ConnectionStrings("KHConnectionString").ToString, q, "Upload")
        Catch ex As Exception
            Exit Sub
        End Try
 
        Dim fuUpload As FileUpload = tUpload.FindControl("fuUpload" + Config_Key)
 
        If fuUpload.HasFile = False Then
            lblError.Text = "You must choose a file!"
            lblError.Visible = True
            Exit Sub
        End If
 
        q = Server.MapPath(DS.Tables("Upload").Rows(0).Item("File Path").ToString) + DS.Tables("Upload").Rows(0).Item("File Name").ToString + "." + DS.Tables("Upload").Rows(0).Item("File Extension").ToString
 
        Try
            fuUpload.SaveAs(Server.MapPath(DS.Tables("Upload").Rows(0).Item("File Path").ToString) + DS.Tables("Upload").Rows(0).Item("File Name").ToString + "." + DS.Tables("Upload").Rows(0).Item("File Extension").ToString)
        Catch ex As Exception
            lblError.Text = "Unable to upload file!"
            lblError.Visible = True
            Exit Sub
        End Try
 
        lblError.Visible = False
 
    End Sub

Open in new window

JessyRobinson1234Asked:
Who is Participating?
 
JessyRobinson1234Connect With a Mentor Author Commented:
I solved it:

 Dim conString As String = ConfigurationManager.ConnectionStrings("KHConnectionString").ToString

        Dim objConn As SqlConnection = New SqlConnection(conString)
     
        objConn.Open()

        Dim cmd = New SqlCommand("Insert_Upload_History", objConn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@Config_Key", SqlDbType.Int).Value = Config_Key

        cmd.ExecuteNonQuery()

        objConn.Close()
0
 
JessyRobinson1234Author Commented:
I added this code to it but it doesn't like it


        Dim conString As String = "Data Source=EGLSMSQL23\Logistics;Initial Catalog=KnowledgeHarvest;Persist Security Info=True;User ID=knowledge;Password=tesVraH"

        Dim objConn As SqlConnection = New SqlConnection(conString)
     
        objConn.Open()

          Dim cmd = New SqlCommand("Insert_Upload_History", objConn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add(Config_Key) ----------> Error: The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects.
        cmd.ExecuteNonQuery()

        objConn.Close()
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.