asp.net中的cmd.ExecuteScalar()问题 [英] problem with cmd.ExecuteScalar() in asp.net

查看:75
本文介绍了asp.net中的cmd.ExecuteScalar()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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




**我在调试时将``id''值作为``_page''而不是ques_code int值,但记录插入得很好,&在sql中运行正常的查询,请帮助我...




** 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...

推荐答案

使用存储过程.您正在尝试插入以及选择.一个查询无法完成.您必须编写一个存储的proc并使用它来代替普通查询
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


这篇关于asp.net中的cmd.ExecuteScalar()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆