Having prob with insertion of tuples to database using asp.net

manojkrishnaks

Disciple
hi friends..
Im inserting tuples with 3 attributes where one is an image..
when i click add button it inserts 2 identical tuples into database. Since i mentioned primary key it shows error and inserts one. If primary key is not mentioned it inserts twice the same values.. Please help me with tht..
My code is here..

If FileUpload1.PostedFile Is Nothing OrElse String.IsNullOrEmpty(FileUpload1.PostedFile.FileName) OrElse FileUpload1.PostedFile.InputStream Is Nothing Then
upld.Text = "No file specified."
Exit Sub

Else

Dim fileName As String = FileUpload1.PostedFile.FileName

Dim ext As String = fileName.Substring(fileName.LastIndexOf("."))

ext = ext.ToLower

Dim imgType = FileUpload1.PostedFile.ContentType

If ext = ".jpg" Then

ElseIf ext = ".bmp" Then

ElseIf ext = ".gif" Then

ElseIf ext = "jpg" Then

ElseIf ext = "bmp" Then

ElseIf ext = "gif" Then

Else

upld.Text = "Only gif, bmp, or jpg format files supported."

Exit Sub

End If


'Connect to the database and insert a new record into Products
Dim reader As IO.BinaryReader = New IO.BinaryReader(FileUpload1.PostedFile.InputStream)

Dim image() As Byte = reader.ReadBytes(FileUpload1.PostedFile.ContentLength)

Dim con As New System.Data.SqlClient.SqlConnection
Dim param As Data.SqlClient.SqlParameter

con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"

Dim myCommand As New System.Data.SqlClient.SqlCommand
param = New Data.SqlClient.SqlParameter("@image", Data.SqlDbType.Image)
param.Value = image
myCommand.Parameters.Add(param)
param = New Data.SqlClient.SqlParameter("@name", Data.SqlDbType.NVarChar)
param.Value = name.Text
myCommand.Parameters.Add(param)
param = New Data.SqlClient.SqlParameter("@id", Data.SqlDbType.Int)
param.Value = Cid.Text
myCommand.Parameters.Add(param)
myCommand.Connection = con
con.Open()
myCommand.CommandText = "INSERT INTO cat values(@name, @id, @image)"
myCommand.ExecuteNonQuery()
upld.Text = "Category added"
con.Close()
End If
 
Back
Top