Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I want to insert image one time but my code inserting two images at a time, where is mistake in my code. Please provide the solution for the code.
VB
Try
      Dim sqlcon As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("cn").ConnectionString)
      sqlcon.Open()
      If FileUpload1.HasFile Then
          'Dim f(1) As String
          Dim f(0) As String
          Dim file() As FileUpload = {FileUpload1}
          Dim i As Integer
          Try
              cmd = New SqlClient.SqlCommand("Select max(id) from birthday", sqlcon)
              dr = cmd.ExecuteReader()
              While dr.Read
                  adcode = dr(0)
              End While
              dr.Close()
          Catch ex As Exception
              adcode = 0
              dr.Close()
          End Try
          For i = 0 To 0
              ' i = i + 1
              'adcode = adcode + 1
              adcode = adcode
              f(i) = upfile(file(i), i, adcode)
          Next
                      End If

  Catch ex As Exception
  Finally
      sqlcon.Close()
  End Try

VB
Public Function upfile(ByVal file As FileUpload, ByVal i As Integer, ByVal cd As Integer) As String
        Dim sqlcon As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("cn").ConnectionString)
        sqlcon.Open()

        Dim f1, f As String
        f = file.FileName
        If f <> "" Then
            f1 = adcode
            f = f.Substring(f.LastIndexOf("."))
            f1 = "~/images/" + f1 + f
            file.SaveAs(Server.MapPath(f1))
            cmd = New SqlClient.SqlCommand("insert into birthday(title,photo,namewithdate) values('" & TextBox1.Text & "','" & f1 & "','" & TextBox2.Text & "')", sqlcon)
            cmd.ExecuteNonQuery()
            labMessage.Text = "Record Saved Sucessfully..."
            clear()
        End If
        Return 0
    End Function
Posted
Updated 14-Apr-11 2:40am
v2
Comments
Indivara 14-Apr-11 8:40am    
Edited title and fixed HTML

1 solution

My word. Do you want a list?

Why create this as an array, if you are only going to have a single element?
Dim f(0) As String

Why do you bother to read from a database, if when it doesn't work you default to zero and cause a problem later on?
Catch ex As Exception
    adcode = 0

Why are you concatenating strings to make an SQL statement? Use parametrized queries, or loose your database to Little Bobby Tables...
cmd = New SqlClient.SqlCommand("insert into birthday(title,photo,namewithdate) values('" & TextBox1.Text & "','" & f1 & "','" & TextBox2.Text & "')", sqlcon)

Why do you have a loop?
For i = 0 To 0
How many times will this be executed?

I could go on, but that code is making me feel queasy...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900