Question

Locked

no data in my database

By ofemigan ·
Hello i am a young programmer with a lot of pressure to deliver fast.
My problem is with a simple code i wrote to insert data into a database and it is runs thru but there is no data in my database . i dont know what could be wrong
Imports System.Data.SqlClient

Public Class Form1

Public Sub addNewRecords_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addNewRecords.Click

If InsertData(assetcode.Text, fullName.Text, modelNo.Text, unitNo.Text, make.Text, yearOfManufacture.Text) = True Then
MessageBox.Show("Insert successful")

Else
MessageBox.Show("An error Has occured During Insertion")
End If

End Sub
Public Function InsertData(ByVal assetCode As Integer, ByVal fullName As String, ByVal modelNo As String, ByVal unitNo As String, ByVal make As String, ByVal yearOfManufacture As Date) As Boolean

Try
Dim strSql As String = "Insert Into Asset2(AssetCode,Fullname,ModelNo,UnitNo,Make,YearOfManufacture)Values(@AssetCode,@Fullname,@ModelNo,@UnitNo,@Make,@YearOfManufacture)"
Using cnn As New SqlConnection(My.Settings.KEngDatabase2ConnectionString)
Using cmd As New SqlCommand(strSql, cnn)
cmd.CommandType = CommandType.Text
Dim prm As SqlParameter

prm = New SqlParameter("@AssetCode", SqlDbType.Int)
prm.Value = assetCode
cmd.Parameters.Add(prm)

prm = New SqlParameter("@Fullname", SqlDbType.NVarChar, 50)
prm.Value = fullName
cmd.Parameters.Add(prm)

prm = New SqlParameter("@ModelNo", SqlDbType.NVarChar, 50)
prm.Value = modelNo
cmd.Parameters.Add(prm)


prm = New SqlParameter("@UnitNo", SqlDbType.NVarChar, 50)
prm.Value = unitNo
cmd.Parameters.Add(prm)

prm = New SqlParameter("@Make", SqlDbType.NVarChar, 50)
prm.Value = make
cmd.Parameters.Add(prm)


'Dim myString As String
'myString =


prm = New SqlParameter("@YearOfManufacture", SqlDbType.DateTime)
prm.Value = yearOfManufacture
cmd.Parameters.Add(prm)

cnn.Open()
cmd.ExecuteNonQuery()


End Using
End Using

Return True
Catch ex As Exception

Return (False)
End Try


End Function


End Class

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Debug

by Shellbot In reply to no data in my database

Can you "step through" this in debug mode and see what is happening? Should be able to spot quick enough.

Been ages since done any of this, but maybe just make sure your executing properly.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx

Collapse -

Can't see anything obviously wrong with that code wise

by Tony Hopkinson In reply to no data in my database

Suggest you comment out the try catch, swallowing exception like you are is an extremely bad idea.

Can't tell from from the code but something as simple as misname column in the table would give the result you are seeing.

Back to Windows Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums