Solved

Add additional fields to code behind Insert Statement

Posted on 2014-07-28
6
279 Views
Last Modified: 2014-07-29
The following code performs an insert of an MP4 file into a database table. I also have code that streams the binary file from SQL server into a media player. Works flawlessly. My issue is I have additional fields that I want to insert into the database table in addition to Name, ContentType and Data. My problem is I do not know how to write the code to make the additional fields part of the below insert statement. I tried to write some code but my additional code caused two records to be created. One record was created with my new fields and the other with the Name, ContentType and Data (mp4 binary file) fields. I would absolutely need the information in the same row of data.

The fields I want to add are username or userid of logged in user(I know this one may be more complicated,

savedfor varchar (150)

bioname varchar (150)

sharewith varchar (150)

description varchar (500)

Here is my current code:

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
' Read the file and convert it to Byte Array
Dim filePath As String = FileUpload1.PostedFile.FileName
Dim filename As String = Path.GetFileName(filePath)
Dim ext As String = Path.GetExtension(filename)
Dim contenttype As String = String.Empty

'Set the contenttype based on File Extension
Select Case ext

Case ".mp4"
contenttype = "video/mp4"
Exit Select

End Select
If contenttype <> String.Empty Then
Dim fs As Stream = FileUpload1.PostedFile.InputStream
Dim br As New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(fs.Length)

'insert the file into database
Dim strQuery As String = "insert into tblFiles" _
& "(Name, ContentType, Data)" _
& " values (@Name, @ContentType, @Data)"
Dim cmd As New SqlCommand(strQuery)
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value _
= contenttype
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes
InsertUpdateData(cmd)
lblMessage.ForeColor = System.Drawing.Color.Green
lblMessage.Text = "File Uploaded Successfully"
Else
lblMessage.ForeColor = System.Drawing.Color.Red
lblMessage.Text = "" _
& " Upload MP4 format files only"
End If

End Sub

Here is the Connection:

Public Function InsertUpdateData(ByVal cmd As SqlCommand) As Boolean
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("SQL2008R2_504887_golivefitnesConnectionString").ConnectionString
Dim con As New SqlConnection(strConnString)
cmd.CommandType = CommandType.Text
cmd.Connection = con
Try
con.Open()
cmd.ExecuteNonQuery()
Return True
Catch ex As Exception
Response.Write(ex.Message)
Return False
Finally
con.Close()
con.Dispose()
End Try
End Function
0
Comment
Question by:derrekdeveloper
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 40225839
It would be useful if you could post your code that was creating 2 rows so we can see where you were going wrong and how you are getting the data for the additional fields. We will then be able to give you a more complete solution.

Please use the code button on the Icon Bar above the Comment box (When posting a comment) to surround your code as it is then easier to review.
0
 

Author Comment

by:derrekdeveloper
ID: 40225848
Additional fields are being created by form fields-asp:text box controls.
0
 

Author Comment

by:derrekdeveloper
ID: 40225850
If all else fails I will just do an after trigger with scope identity.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 40225861
Should be something like below, obviously ensuring the column names in the query match your database and the textbox control names are correct where they are being assigned to the parameters.

...
'insert the file into database 
 Dim strQuery As String = "insert into tblFiles" _
 & "(Name, ContentType, Data, Username, SavedFor, BioName, Sharewith, Description)" _
 & " values (@Name, @ContentType, @Data, @UserName, @SavedFor, @BioName, @ShareWith, @Description)"
 Dim cmd As New SqlCommand(strQuery)
 cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename
 cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value _
 = contenttype
 cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes
cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = username.Text
cmd.Parameters.Add("@SavedFor", SqlDbType.VarChar).Value = savedfor.Text
cmd.Parameters.Add("@BioName", SqlDbType.VarChar).Value = bioname.Text
cmd.Parameters.Add("@ShareWith", SqlDbType.VarChar).Value = sharewith.Text
cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = Description.Text
 InsertUpdateData(cmd)
...

Open in new window

0
 

Author Closing Comment

by:derrekdeveloper
ID: 40226926
Awesome Paul! Exactly what I needed. Thanks so much!
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 40227058
No problem, glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiple file Upload asp.net 2 36
Programmatically signing Word macros 4 61
Server Error 11 47
Run time Error 4 34
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

937 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now