2 Replies - 3042 Views - Last Post: 09 October 2012 - 04:49 PM Rate Topic: -----

#1 George Rex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-April 08

Create A SQL Database and manipulate Records from Vb.Net 2012

Posted 09 October 2012 - 03:54 PM

I have tried creating a database table in sql server and to insert records.
But Every time I run the codes, it just don't do anything. It only shows the msgbox prompt which I put to indicate confirmation.
I have 2 text boxes and 3 combo boxes. how do I add records into the sql table.
I simply want to add records from the form to the sql database table
emphasis should also be laid on the combo boxes too... what properties should be used when adding records to a database table?
Below are the codes i have used

'If txtAddID.Text = "" Then
'            MsgBox("Abeg Complete d Form b4 I break ur head!", MsgBoxStyle.Information)
'        Else
'            On Error Resume Next
'            Dim MyMsg
'            Dim con As New SqlConnection("server = REXYSPY\SQLEXPRESS;Initial Catalog=master;Integrated Security=True")
'            Dim cmd As New SqlCommand("insert into dbo.tblSchools values ('" + txtAddID.Text + "','" + txtAddInstitution.Text + "','" + cboAddInstituteState.Text + "','" + cboAddInstituteRegion.Text + "','" + cboAddInstituteType.Text + "')")
'            cmd.Connection = con
'            con.Open()
'            cmd.Parameters.AddWithValue("@ID", Me.txtAddID.Text)
'            cmd.Parameters.AddWithValue("@Sname", Me.txtAddInstitution.Text)
'            cmd.Parameters.AddWithValue("@Sstate", Me.cboAddInstituteState.Text)
'            cmd.Parameters.AddWithValue("@Sregion", Me.cboAddInstituteRegion.Text)
'            cmd.Parameters.AddWithValue("@Stype", Me.cboAddInstituteType.Text)
'            Dim reader As SqlDataReader = Nothing
'            cmd.ExecuteNonQuery()
'
'           con.Close()
'           MyMsg = MsgBox("Your Information don enta. You wan enta again", 4, "Confirmation!")
'           If MyMsg = 7 Then
'               Me.Close()
'               Input.Show()
'           End If
'       End If




Is This A Good Question/Topic? 0
  • +

Replies To: Create A SQL Database and manipulate Records from Vb.Net 2012

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Create A SQL Database and manipulate Records from Vb.Net 2012

Posted 09 October 2012 - 04:36 PM

First did you check your connection string, I'm almost certain it's your connection string that is the problem. Make sure your SQL Server is at least of 2012. Build Connection Strings With the release of SQL Server 2012, so many issues went away. Before that even when you'd do things right you had a really good chance of crap going wrong. So make sure you've got the most recent SQL Server installed.

Second, is your table actually called dbo.tblschools?

Third, I strongly urge that you use the & operator instead of the + operator as you cannot add a string, but you can concatenate it(glue it on to the next one lol).

Fourth, is your paramenter @ID to establish an id for the record in the table? If so, there is already an easy way to do that, without having to do it yourself, that can be established with the sql database and the server.

This post has been edited by trevster344: 09 October 2012 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Create A SQL Database and manipulate Records from Vb.Net 2012

Posted 09 October 2012 - 04:49 PM

First it's a good practice to put the fields in your insert statement.

Second, why are you concatenating into the string but defining parameters (use the parameters)

Now you insert statement would be
"insert into dbo.tblSchools (put your fields here) values (@ID, @Sname, @Sstate, @Sregion, @Stype)"
             cmd.Parameters.AddWithValue("@ID", Me.txtAddID.Text)
             cmd.Parameters.AddWithValue("@Sname", Me.txtAddInstitution.Text)
             cmd.Parameters.AddWithValue("@Sstate", Me.cboAddInstituteState.Text)
             cmd.Parameters.AddWithValue("@Sregion", Me.cboAddInstituteRegion.Text)
             cmd.Parameters.AddWithValue("@Stype", Me.cboAddInstituteType.Text)


Then you use the ExecuteNonQuery method against the command object.

Also, for readability it is good practice to use all uppercase keywords in your statements.
"INSERT INTO dbo.tblSchools (put your fields here) VALUES (@ID, @Sname, @Sstate, @Sregion, @Stype)"
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1