Click here to Skip to main content
16,021,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim connection As SqlConnection = Nothing
        Try
            Dim img As FileUpload = CType(QuestionUp, FileUpload)
            Dim imgByte As Byte() = Nothing
            If img.HasFile AndAlso Not img.PostedFile Is Nothing Then
                'To create a PostedFile
                Dim File As HttpPostedFile = QuestionUp.PostedFile
                'Create byte Array with file len
                imgByte = New Byte(File.ContentLength - 1) {}
                'force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength)
            End If
            ' Insert the employee name and image into db
            Dim conn As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
            connection = New SqlConnection(conn)

            connection.Open()

            Dim sql As String = "INSERT INTO QN_QUESTION_MAST(QUES_CODE,Ques_ImageData) VALUES(@QUES_CODE, @Ques_ImageData) SELECT @@IDENTITY"

            Dim cmd As SqlCommand = New SqlCommand(sql, connection)
            cmd.Parameters.AddWithValue("@QUES_CODE", txtQuestionCode.Text)
            cmd.Parameters.AddWithValue("@Ques_ImageData", imgByte)
            Dim id As Integer = Convert.ToInt32(cmd.ExecuteScalar())
            lblMsg.Text = String.Format("QUES_CODE is {0}", id)
            Image2.ImageUrl = "~/ShowImage.ashx?id=" & id
        Catch
            lblMsg.Text = "There was an error"
        Finally
            connection.Close()
        End Try
    End Sub



** I am getting the 'id' value as'_page' when debugging and not the ques_code int value,but record is inserting well,& the query in running ok in sql, please help me...
Posted
Comments
_Amy 6-Aug-12 7:31am    
Do you have an identity column specified for the table? Because your code seems to correct.

1 solution

Use stored procedure. you are trying to insert as well as select. It cannot be done with one query. you have to write a stored proc and use it instead of normal query
 
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