Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I want update when I click sumit it to line:
Catch ex As Exception<br />
            MsgBox("Can not Update Data : " & & ex.Message, MsgBoxStyle.Critical, "Error Meaage")<br />
                Exit Sub  


MsgBox error:

Can not Update Data: Conversion from string "update Employee set  minit='ppp'" to type 'Integer' is not valid.


I don't know what it mean ?

my code:

VB
    Protected Sub submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
        Dim con As SqlConnection
        Dim da As SqlDataAdapter = New SqlDataAdapter
        Dim ds As DataSet = New DataSet()
        Dim sqlcommand As String
        Dim sqlcmd As SqlCommand
        Dim rowaffected As Integer

        con = New SqlConnection(connStr)

        If (Request.QueryString("edit") <> "") Then
            sqlcommand = "update Employee set "
            sqlcommand &= "fname= '" & FirstName.Text.Trim & "',"
            sqlcommand &= "minit= '" & minit.Text.Trim & "',"
            sqlcommand &= "LNAME= '" & LastName.Text.Trim & "',"

            sqlcommand &= "ADDRESS= '" & Address.Text.Trim & "',"

            If male.Checked = True Then
                sqlcommand &= "sex = 'M' , "
            Else
                sqlcommand &= "sex = 'F' , "
            End If
            sqlcommand &= "salary= '" & Salary.Text.Trim & "',"
            sqlcommand &= "dno= '" & Department.SelectedValue & "' "
            sqlcommand &= "where ssn= '" & Request.QueryString("edit").ToString & "'"


            Try
                sqlcmd = New SqlCommand
                con.Open()
                sqlcmd.CommandType = CommandType.Text
                sqlcmd.CommandType = sqlcommand
                sqlcmd.Connection = con
                rowaffected = sqlcmd.ExecuteNonQuery

            Catch ex As Exception
            MsgBox("Can not Update Data : " & & ex.Message, MsgBoxStyle.Critical, "Error Meaage")
                Exit Sub
            Finally
                con.Close()
                Response.Redirect("sql8.aspx")
            End Try
        Else
            sqlcommand = " insert into employee (ssn, fname,minit, lname,"
            sqlcommand &= " address,sex,salary, dno) values("
            sqlcommand &= "'" & SSN.Text.Trim & "',"
            sqlcommand &= "'" & firstName.Text.Trim & "',"
            sqlcommand &= "'" & minit.Text.Trim & "',"
            sqlcommand &= "'" & LastName.Text.Trim & "',"

            sqlcommand &= "'" & Address.Text.Trim & "',"
            If male.Checked = True Then
                sqlcommand &= "'M',"
            Else
                sqlcommand &= "'F',"

            End If
            sqlcommand &= "'" & Salary.Text.Trim & "',"
            sqlcommand &= "'" & Department.Text.Trim & "')"
            Try
                sqlcmd = New SqlCommand
                con.Open()

                sqlcmd.CommandType = CommandType.Text
                sqlcmd.CommandText = sqlcommand
                sqlcmd.Connection = con
                rowaffected = sqlcmd.ExecuteNonQuery

            Catch ex As Exception
                MsgBox("Can not Insert Data : " & ex.Message, MsgBoxStyle.Critical, "Error Meaage")
                Exit Sub
            Finally
                con.Close()

            End Try
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Redirect("sql8.aspx")
    End Sub

End Class
Posted
Updated 20-Aug-11 9:08am
v2

First of all you should not be using unvalidated user input in your SQL statements and you should certainly not be using string concatenation to form the statements. There has been so much written about the dangers of either of these methods. Why do people continue to use them?
Use a parametrized query at least.
 
Share this answer
 
v2
try This

You are sending values before converting them into integer you need to convert them first.

Convert.ToInt32(minit.Text)

/* Or Other Vlues which needed convertion according to the table columnname DataTypes
 
Share this answer
 
Agree with Mark. Plain concatenated SQL string may invite SQL-Injection problem.

Your Code. You have assigned "SQL Query" to CommandType which is wrong.
VB
sqlcmd = New SqlCommand
con.Open()
sqlcmd.CommandType = CommandType.Text
sqlcmd.CommandType = sqlcommand
sqlcmd.Connection = con
rowaffected = sqlcmd.ExecuteNonQuery


Try by changing it as below.

VB
sqlcmd = New SqlCommand
con.Open()
sqlcmd.CommandType = CommandType.Text
sqlcmd.CommandText = sqlcommand
sqlcmd.Connection = con
rowaffected = sqlcmd.ExecuteNonQuery
 
Share this answer
 
v2

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