1

ijust finish my code for inserting data using the vb and mySQL but when i run my webpage it seem have an error Fatal Error Encounter During Command Execution . Please help some how to solve it. below is my code.

Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient


Partial Class Request

    Inherits System.Web.UI.Page

    Dim MessageBox As Object

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        txt1.Focus()
        txt2.Focus()
        txt3.Focus()
        txt4.Focus()
        txt5.Focus()
        txt6.Focus()
        txt7.Focus()
        ddl1.Focus()
        ddl2.Focus()
        ddl3.Focus()
        ddl4.Focus()
    End Sub

    Protected Sub btnsubmit_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click


        'Create sql connection and fetch data from database based on employee id

        Dim conn As New MySql.Data.MySqlClient.MySqlConnection

        Dim strConnectionString As String = ConfigurationManager.ConnectionStrings("testConnectionString").ConnectionString

        Try
            conn.ConnectionString = strConnectionString
            conn.Open()

        Catch ex As MySql.Data.MySqlClient.MySqlException
            MessageBox.Show(ex.Message)
        End Try
        '  Dim cr_id As String
        ' cr_id = "CR004"
        Dim iReturn As Boolean
        Using SQLConnection As New MySqlConnection(strConnectionString)
            Using sqlCommand As New MySqlCommand()
                sqlCommand.Connection = SQLConnection
                With sqlCommand




                    .CommandText = "INSERT INTO cr_record(idcr_record,Emplid,Nama,date,DeptDesc,email,change,reasonchange,problem,priority,reasondescription,systemrequest) VALUES (@IDCR,@Emplid,@Nama,@date,@DeptDesc,'@email,@change,@reasonchange,@problem,@priority,@reasondescription,@systemrequest)"
                    ' .CommandTimeout = 5000000
                    .CommandType = Data.CommandType.Text

                    .Parameters.AddWithValue("@Emplid", txt1.Text)
                    .Parameters.AddWithValue("@Nama", TextBox1.Text)
                    .Parameters.AddWithValue("@date", txt5.Text)
                    .Parameters.AddWithValue("@DeptDesc", txt2.Text)
                    .Parameters.AddWithValue("@email", txt4.Text)
                    .Parameters.AddWithValue("@change", ddl2.Text)
                    .Parameters.AddWithValue("@reasonchange", txt6.Text)
                    .Parameters.AddWithValue("@problem", ddl3.Text)
                    .Parameters.AddWithValue("@priority", rbl1.Text)
                    .Parameters.AddWithValue("@reasondescription", txt7.Text)
                    .Parameters.AddWithValue("@systemrequest", ddl4.Text)




                End With
                Try
                    SQLConnection.Open()
                    ' sqlCommand.ExecuteNonQuery()
                    sqlCommand.ExecuteNonQuery()
                    iReturn = True
                    MsgBox("Added Successfully")
                Catch ex As MySqlException
                    MsgBox(ex.Message.ToString & Err.Description)
                    iReturn = False
                Finally
                    SQLConnection.Close()
                End Try
            End Using
        End Using
        Return
    End Sub



End Class
7
  • Are you displaying message boxes from your website? Commented Mar 20, 2014 at 1:56
  • @Szymon i not really understand what you mean but in my code this message box is only for test connection. Commented Mar 20, 2014 at 2:04
  • You cannot display message boxes in a website. Message boxes will be displayed on the server (if at all) and there will be no one to click them. Commented Mar 20, 2014 at 2:06
  • @Szymon so how the solution that i must do?? Commented Mar 20, 2014 at 2:19
  • If you do it for testing only, put a breakpoint and step through the code. Commented Mar 20, 2014 at 2:21

3 Answers 3

2

you probably forgot to add this parameter @IDCR

.Parameters.AddWithValue("@IDCR", toyourvariable)
Sign up to request clarification or add additional context in comments.

1 Comment

yes i forget this command . then i should not set my dataname in database using the reserved word CHANGE. for now all the solution is done. thanks for guide. it works @codemunkeee
1

Syntax error in your query:

[...snip...]tDesc,'@email,@change,@rea[...snip...]
                  ^---mis-placed quote.

Reserved words:

[...snip...]c,email,change,reasonc[...snip...]
                    ^^^^^^---- quote with backticks: `change`

4 Comments

i have fix that error but it still give the Fatal Error again.?
That's not how this site works. You need to do debugging yourself too.
no dont be miss understand what i mean. i dont you to do my code ..but i want you see my code.. im still working by myself for my code but it still error on my code the same error even i have fix the error that come suggestion from you guys..
@March B thanks i found the error it in database because of using reserved word..the command it ok. i change it n functioning .
0

Solution that i used and it really works. This error is mostly caused by a MISSING or Incorrectly Spelled Parameter declaration. eg. @FirstName mistakenly spelled for @FirtName.

Make sure that all the parameters that are declared in the sql query are all declared in the AddwithValue Parameter declaration. (It helps to count the query versus the Addwithvalues).

The best solution is for visual studio to provide information about the missing Parameter. Use a Try-Catch block. In the catch block use Messagebox.show(ex.Innerexception.Message) instead of Messagebox.show(ex.message). This will show the exact Parameter that is missing. eg. below

Try

      conCommand.Parameters.Addwithvalue("@FirstName", txtFirstName.text)
      conCommand.Parameters.Addwithvalue("@MiddleName", txtMiddleName.text)
      conCommand.Parameters.Addwithvalue("@LastName", txtLastName.text)
      conCommand.Parameters.Addwithvalue("@PhoneNo", txtPhoneno.text) 

catch ex as exception          
Messagebox.show(ex.innerexception.Message)

End Try


Hope this helps. Its really great that we share our ideas in the world of programming.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.