I have a problem in data insert

Posted by Nuwan under ASP.NET on 11/25/2011 | Points: 10 | Views : 776 | Status : [Member] | Replies : 2
I created asp.net application.It save username,password,imagename and image path. but problem is click in to save button data save is twice.
this is my code

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click

If FileUpload1.PostedFile IsNot Nothing Then

Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)

Dim txt1 As String = Me.TextBox1.Text
Dim txt2 As String = Me.TextBox2.Text

'Save files to disk

FileUpload1.SaveAs(Server.MapPath("images/" & FileName))


'Add Entry to DataBase

Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("conString").ConnectionString()

Dim con As New SqlConnection(strConnString)
Dim strQuery As String = "insert into log (username, password,image,imagepath)" + " values(@Username,@Password,@ImageName, @ImagePath)"
Dim cmd As New SqlCommand(strQuery)

cmd.Parameters.AddWithValue("@Username", txt1)
cmd.Parameters.AddWithValue("@Password", txt2)
cmd.Parameters.AddWithValue("@ImageName", FileName)
cmd.Parameters.AddWithValue("@ImagePath", "images/" + FileName)

cmd.CommandType = Data.CommandType.Text
cmd.Connection = con

Try

con.Open()
cmd.ExecuteNonQuery()

Catch ex As Exception

Response.Write(ex.Message)

Finally

con.Close()

End Try

End If

End Sub


Please help me that matter.

Rathnayake


Responses

Posted by: RohiK on: 11/25/2011 [Member] Starter | Points: 25

Up
0
Down
is any error occur???

try with ExecuteScalar

Happy Programming!!
Rohi

Nuwan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 11/25/2011 [Member] Starter | Points: 25

Up
0
Down
use below line of code before adding parametes to command object
cmd.Parameters.Clear()


Regards,
Sunil

Nuwan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response