Question Saving to DataBase

elianeasmar

Well-known member
Joined
Oct 3, 2013
Messages
76
Programming Experience
Beginner
Hello everyone. I have been trying for a while to save records to sql.
i have created a database called SchoolDB. i hv the feild listed below in the table StudentInfo.
unfortunately i haven't succeeded. I tried many ways.

Here is one of them.

Dim _cn As New SqlConnection
Dim _cnstring As String = "Data Source=ELIANE-VAIO\SQLEXPRESS;Initial Catalog=SchoolDB;Integrated Security=True;"
Dim _cn As New SqlConnection(_cnstring)
Dim cmd As New SqlCommand
'Try
_cn.Open()
cmd.Connection = _cn
cmd.CommandText = "INSERT INTO StudentInfo(
VB.NET:
, [Full-Name], [Position], [Title], [Phone-Number], [Address], [Date-Time]) VALUES('" & Form1.txtCode.Text &              "', '" & Form1.txtName.Text & "', '" & Form1.cmbPosition.Text & "', '" & Form1.cmbTitle.Text & "','" & Form1.txtPhoneNumber.Text & "',' " & Form1.txtAddress.Text &               "', '" & Form1.DateTimePicker1.Value & "');"
      cmd.ExecuteNonQuery()


  'Catch ex As Exception
        '    MessageBox.Show("Error while inserting record on table...")
        'Finally
        _cn.Close()
        'End Try
[/QUOTE]



Can you help me please :/

Any help would be appreciated :)
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

When posting, it is important to remember to add any error messages that you may be getting to help us narrow down your issue so that we can help you more efficiently, otherwise, we just have to guess.

So, guessing a bit, I can see two issues with what you have:-

1) Your have declared your _cn variable twice so that will give you an error

2) When saving Dates to a Database, and assuming that the field type in your Database is set to either Date or DateTime, then you must not use String values to save your Date information since this is held in Binary format in the Database. Knowing this, get rid of the single quotes when trying to save your DateTimePicker value.

So, that's my first guess on your issues with not knowing anything about what the error is that you are getting. If this does not help to get your issue solved then please post the errors your are getting for us to help you further.

A couple of other points for you:-

1) If you want to post code then please do use Code Tags rather than Quote Tags to makes things more readable. The code tags can be found by clicking on the "Go Advanced" button.

2) You should be using Parameters when building your SQL queries. Do take the time to read through this link to find out how and why:-

John McIlhinney .NUT: Using Parameters in ADO.NET

Hope that helps,

Cheers,

Ian
 

elianeasmar

Well-known member
Joined
Oct 3, 2013
Messages
76
Programming Experience
Beginner
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
cmd.CommandText = "INSERT INTO StudentInfo(
VB.NET:
, [Full-Name], [Position], [Title], [Phone-Number], [Address], [Date-Time]) VALUES (@Code, @Name, @Position, @Title, @PhoneNumber, @Address, @DateTime);"[/COLOR]
cmd.Parameters.AddWithValue([COLOR=purple]"[/COLOR][COLOR=purple]@Code"[/COLOR], Form1.txtCode.Text)
cmd.Parameters.AddWithValue([COLOR=purple]"[/COLOR][COLOR=purple]@Name"[/COLOR], Form1.txtName.Text)
cmd.Parameters.AddWithValue([COLOR=purple]"[/COLOR][COLOR=purple]@Position"[/COLOR], Form1.cmbPosition.Text)
cmd.Parameters.AddWithValue([COLOR=purple]"[/COLOR][COLOR=purple]@Title"[/COLOR], Form1.cmbTitle.Text)
cmd.Parameters.AddWithValue([COLOR=purple]"[/COLOR][COLOR=purple]@PhoneNumber"[/COLOR], Form1.txtPhoneNumber.Text)
cmd.Parameters.AddWithValue([COLOR=purple]"[/COLOR][COLOR=purple]@Address"[/COLOR], Form1.TxtAddress.Text)
cmd.Parameters.AddWithValue([COLOR=purple]"[/COLOR][COLOR=purple]@DateTime"[/COLOR], Form1.DateTimePicker1.Value)
End sub

What if i have this code?
 I managed to write it. but still won't save in my database. But won't give me any error. It just freezes.
Is there any error in the code above?
Thank you for your time :)
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

You need to be more specific with your comments and your code.

The only thing I can say here is that what you have posted looks fine, but, you do not declare any Connection or Command objects, so that could be an issue, and you do not actually Execute that query against a Database so that would never work anyway.

Other than that, I am not sure what you expect us to magically pull out of the air for you.

By the way, where are the code tags?

Cheers,

Ian
 

elianeasmar

Well-known member
Joined
Oct 3, 2013
Messages
76
Programming Experience
Beginner
I am sorry if i'm asking so much questions. But i've been stuck with this problem 7 hours. and don't know what to do about it. i came up with several things. But all i can get is negativ results.If the last code i posted doesn't contain any errors, than maybe the visual basic does :/Thank you so much for your time :)I really appreciate your reply :)
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

I have no problem with you asking questions. If I did I would not be here, so keep asking questions to help us to help you solve your problem.

The issue here is that you have posted two blocks of code, the first of which had issues, which I explained, and the second of which looked fine, and you have not described any errors that you are getting. Due to that, I am currently playing guessing games with you.

Jumping around with various bits of code never works and just confuses the mind so the first thing to do is to Stick with One Block of code that you want to use to solve your saving issue (that being the block of code with parameters). Once you have that then lets explore that code, IN FULL, meaning the declaration and connection etc until we can identify what the issue is that you are having.

Cheers,

Ian
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Oh Sugar,

I missed the obvious in your code in post #3. If you are running this code from Form1 then you need to get rid of the word Form1 in all you Parameter declarations. You can just use the name of the control but if you want to explicitly refer to the current Form then you would use the Keyword Me.

Does that get you any closer to solving your issue?

Cheers,

Ian
 
Last edited:
Top Bottom