0

Please I need help for my thesis. I'm creating a Scheduling for enrollment system. I'm using the Table "classsubjects" for my Testing.

this is my sample code.

    Try
        con.Open()
        cmd.Connection = con
        cmd.CommandType = CommandType.Text
        cmd.CommandText = String.Format("SELECT * " & _
                             "FROM classsubjects " & _
                             "WHERE subject_code='{0}' " & _
                             "AND subject_name='{1}' " & _
                             "AND year_level='{2}' ", subjcode.Text, subjname.Text, subjyearlevel.Text)
        dr = cmd.ExecuteReader

        If dr.HasRows Then
            'MsgBox("Conflict Found, please check !", MsgBoxStyle.Critical)
            DataGridView1.Rows.Add("subject_ID,subject_code,subject_name,year_level", subjID.Text, subjcode.Text, subjname.Text, subjyearlevel.Text) '

            con.Close()
        Else
            con.Close()
            con.Open()
            cmd.Connection = con
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "INSERT into classsubjects(subject_ID, subject_code, subject_name, year_level)VALUES('" + subjID.Text + "','" + subjcode.Text + "','" + subjname.Text + "','" + subjyearlevel.Text + "')"
            cmd.ExecuteNonQuery()
            MsgBox("Successfully Saved")
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        con.Close()
    End Try
CaBz
  • 1
  • 2
  • If I understood correctly you can use [ToolTip](http://www.help-info.de/en/Visual_Basic_net/vbnet_tooltip.htm) control instead of a `MsgBox()` – Vivek S. Nov 19 '15 at 11:47
  • In place of the commented out MsgBox, you could use a Label or a StatusBar to report the data already exists. The *other* MsgBox ("successful") can come up even when the data *isnt* saved for one reason or another. `ExecuteNonQuery` is a function returning the number of rows affected. If you check that the return is 1, *then* it is successful. Some of those DB objects like COnnection, Command, and DataReader should be created, used and disposed of each time rather than global ones reused. And of course, you should be using SQL Parameters instead of String.Format. – Ňɏssa Pøngjǣrdenlarp Nov 19 '15 at 13:40
  • how to display in datagrid sir ? – CaBz Nov 19 '15 at 17:17

0 Answers0