Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2011
    Posts
    3

    Question Unanswered: SQL server "error messages"

    hello all,,,,
    The first time that the post here and I hope to find help
    I am having trouble in the error messages in the SQL server, I want to write them using my own words
    I have tested this code but it does not work

    **Dim cmd As New SqlCommand
    * * * * Dim con As New SqlConnection
    * * * * con.ConnectionString = "Data Source= (local);Initial Catalog=TASS_DATA;User Id=sa;Password=400;"
    * * * * cmd.CommandType = CommandType.StoredProcedure
    * * * * cmd.CommandText = "updateTBLCollege_1"
    * * * * cmd.Parameters.Add("@ID_1", SqlDbType.Int).Value = TextBox1.Text.Trim()
    * * * * cmd.Parameters.Add("@CollegeName_2 ", SqlDbType.NVarChar).Value = TextBox4.Text.Trim()
    * * * * cmd.Connection = con
    *
    * * * * If TextBox1.Text = Nothing And TextBox4.Text = Nothing Then
    * * * * * * MessageBox.Show("you must enter ID and *college name")
    * * * * End If
    * * * * If TextBox1.Text = Nothing Then
    * * * * * * MessageBox.Show("you must enter ID")
    * * * * End If
    * * * * If IsInputChar(TextBox1.Text) Then
    * * * * * * MessageBox.Show("you must enter number for ID")
    * * * * End If
    *
    *
    * * * * If TextBox4.Text = Nothing Then
    * * * * * * MessageBox.Show("you must enter college name")
    * * * * End If
    * * * * If IsNumeric(TextBox4.Text) Then
    * * * * * * MessageBox.Show("you must enter character for college name")
    * * * * End If
    *
    * * * * Try
    * * * * * * con.Open()
    * * * * * * cmd.ExecuteNonQuery()
    *
    * * * * Catch ex As SqlException
    * * * * * * Select Case ex.Number
    * * * * * * * * Case 4060
    * * * * * * * * * * MessageBox.Show("Invaild Database ")
    * * * * * * * * Case 2627
    * * * * * * * * * * MessageBox.Show("Primary key Violation")
    * * * * * * * * Case 18456
    * * * * * * * * * * MessageBox.Show("login failed ")
    * * * * * * * * Case 547
    * * * * * * * * * * MessageBox.Show("ForeignKey Violation")
    * * * * * * * * Case 2601
    * * * * * * * * * * MessageBox.Show(" Unique Index/Constriant Violation ")
    *
    *
    * * * * * * End Select
    *
    * * * * Finally
    * * * * * * con.Close()
    * * * * * * con.Dispose()
    * * * * End Try
    * * * * DataGridView1.DataSource = GetData()
    * * * * TextBox1.Clear()
    * * * * TextBox4.Clear()
    *
    * * End Sub
    Is there a tip may be useful to me

  2. #2
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,799
    Provided Answers: 11
    what about the code does not work? i would certainly add a catch-all at the end of your SELECT - CASE statement, that would just send back the value of ex.Message.

  3. #3
    Join Date
    Oct 2011
    Posts
    3

    SQL error messages

    well , when it start debug ,it highlighted these * * * * * * cmd.ExecuteNonQuery()
    *and show message that it can not convert from string to Int32
    some thing else ,i need the name of excption for null values

  4. #4
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,799
    Provided Answers: 11
    Then this line will have to change:
    Code:
            cmd.Parameters.Add("@ID_1", SqlDbType.Int).Value = TextBox1.Text.Trim()
    you need to add int.TryParse, in order to convert the text to an integer.

  5. #5
    Join Date
    Sep 2011
    Posts
    71
    hello you can try below as MCrowley mentioned

    cmd.Parameters.Add("@ID_1", SqlDbType.Int).Value =int.parse( TextBox1.Text.Trim())

  6. #6
    Join Date
    Oct 2011
    Posts
    3

    SQL server "error masseges"

    Hello all
    MCrowley, paultech thank you much ,but again its highlighted the "cmd.ExecuteNonQuery" and shows "FormatException was unhandled"
    and We i tried with Int.parse it showed "Overload resolution failed because no accessible 'Int' accepts this number of arguments."
    THIS IS THE WHOLE CODE
    PLEASE HELP
    Private Sub But_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Add.Click

    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    con.ConnectionString = "Data Source= (local);Initial Catalog=TASS_DATA;User Id=sa;Password=200700760;"
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "insert_TBL_Visa_1"
    cmd.Parameters.Add("@ID_1", SqlDbType.Int).Value = TextBox1.Text.Trim()
    cmd.Parameters.Add("@TransactionTypeID_2 ", SqlDbType.Int).Value = TextBox2.Text.Trim()
    cmd.Parameters.Add("@VisaTypeID_3 ", SqlDbType.Int).Value = TextBox3.Text.Trim()
    cmd.Parameters.Add("@EmpID_4 ", SqlDbType.Int).Value = TextBox4.Text.Trim()
    cmd.Connection = con
    Try
    cmd.Connection = con


    con.Open()
    cmd.ExecuteNonQuery()

    Catch ex As SqlException
    Select Case ex.Number
    Case 4060
    MessageBox.Show("Invaild Database ")
    Case 2627
    MessageBox.Show("Primary key Violation")
    Case 18456
    MessageBox.Show("login failed ")
    Case 547
    MessageBox.Show("ForeignKey Violation")
    Case 2601
    MessageBox.Show(" Unique Index/Constriant Violation ")


    End Select

    Finally
    con.Close()
    con.Dispose()
    End Try
    If TextBox1.Text = Nothing And TextBox2.Text = Nothing And TextBox3.Text = Nothing And TextBox4.Text = Nothing Then
    MessageBox.Show("Fill The Field please")
    End If
    If TextBox1.Text = Nothing Then
    MessageBox.Show("Enter The ID number please")
    End If
    If IsInputChar(TextBox1.Text) Then
    MessageBox.Show("Enter The ID number please")
    End If
    If IsInputChar(TextBox2.Text) Then
    MessageBox.Show(" ID Number, please")
    End If
    If IsInputChar(TextBox3.Text) Then
    MessageBox.Show("Number please")
    End If
    If IsInputChar(TextBox4.Text) Then
    MessageBox.Show("Number please")
    End If
    'If IsNumeric(TextBox1.Text + TextBox2.Text + TextBox3.Text + TextBox4.Text) Then

    'Else
    ' MessageBox.Show("يرجى ادخال ارقام فقط ")

    ' TextBox1.Text = ""
    ' TextBox2.Text = ""
    ' TextBox3.Text = ""
    ' TextBox4.Text = ""

    'End If


    Try
    con.Open()
    cmd.ExecuteNonQuery()
    MessageBox.Show("Record insert successfully")
    Catch x As Exception
    MessageBox.Show(x.Message & "error")
    Finally
    con.Close()
    con.Dispose()
    End Try
    DataGridView1.DataSource = GetData()



    TextBox1.Clear()
    TextBox2.Clear()
    TextBox3.Clear()
    TextBox4.Clear()
    If TextBox1.Text = "" Then


    But_new.Enabled = True
    ElseIf Not TextBox1.Text = "" Then

    But_new.Enabled = True

    End If

    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •