Click here to Skip to main content
13,152,454 members (38,981 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form1
    Dim cnn As New SqlConnection
    Dim cmd As New SqlCommand
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        cmd.Connection = cnn
        cmd = New SqlCommand("insert into info(id, address) values('" & TextBox1.Text & "','" & TextBox2.Text & "')", cnn)
        MsgBox("data saved successfully.")
    End Sub
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cnn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & Application.StartupPath & "\Database1.mdf; Integrated Security=True;User Instance=True"
    End Sub
End Class

Do I need to add more to save the data from the textboxes to database?
Posted 26-Jan-13 5:00am
Mike Meinz 26-Jan-13 11:09am
Is there an error message?
If so, what is it?

You should use SQLParameter object to prevent SQL Injection Attacks and to handle the situation where there might be a ' inTextBox1.Text
Mike Meinz 26-Jan-13 11:13am
My ConnectionStrings look like this format:

"Database=MyDatabaseName;Server=MyPCName\SQLEXPRESS;Integrated Security=True;Net=dbmssocn;"
Richard MacCutchan 26-Jan-13 11:13am
Why do you display the message "data saved successfully." when you have not bothered to check if anything has been saved?
GeekBond 26-Jan-13 11:30am
Because at that point, the data is expected to have been saved in the database?
GeekBond 26-Jan-13 11:31am
What version of VS and SQL Server are you using?
suhan 2012 26-Jan-13 11:43am
No, there is not any error message I get when I run this program. The problem is, I do not see any data in the table.
suhan 2012 26-Jan-13 11:45am
vs 2008, SQL sever 2005
skydger 26-Jan-13 11:48am
Does identity exist on column 'id'? Are there any contraints onany of the table columns?
Mike Meinz 26-Jan-13 11:56am
Have you used the Visual Studio Debugger and stepped through the code in the Click event to be sure that it is getting executed?

If you execute the following in a SQL Server Management Studio query window, does the row appear in the table?
insert into info(id, address) values('TEST1','TEST2');
Richard MacCutchan 26-Jan-13 11:57am
There is a world of difference between what you expect to happen, and what does happen. Check the results of your operations properly, do not just assume that they do what you want.
suhan 2012 26-Jan-13 12:10pm
Err! I have found it. The problem is occurring just because I changed the database path to application.startuppath. It was working fine when it was in its default path. So, can you please tell me what should I do in this moment?
suhan 2012 26-Jan-13 12:25pm
I've found it. The problem is occurring just because I changed the connection string to application.startuppath. It was working fine when the project folder was on the desktop and connection string was set to like, users\desktop\project folder\\ etc. So, can you please help me to keep the database in the debug folder and use it by application.startuppath?
skydger 26-Jan-13 12:32pm
You don't need to use AttachDbFilename in your connection string. But you need to add database name such as following
Also, please use try/catch block to get an exception messages.
Mike Meinz 26-Jan-13 12:43pm
Remove the AttchDBFilename clause within the ConnectionString!!!

If you would use ConnectionString in this type of format, you would not have this problem:

"Database=MyDatabaseName;Server=MyPCName\SQLEXPRESS;Integrated Security=True;Net=dbmssocn;"
suhan 2012 26-Jan-13 13:06pm
I add that message box because I know that the data will be saved at that moment.
Richard MacCutchan 26-Jan-13 16:00pm
No you don't, because you are not checking the result. Please read the documentation and learn how to check results of your method calls properly.
Richard MacCutchan 27-Jan-13 7:17am
And if you had put proper error checking in your code, as I suggested, you would have found it the first time you tried to run the program. I think there is a lesson to be learned here.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

0) Please put all the data access code in a separate class -- a Data Access Layer.
1) Please use a parameterized query; do not use concatenation to form your queries.
2) Do not open the connection and leave it open for the duration of the application.
3) Use a try/finally
4) Your click will close the connection and a second click will fail.

Other than that, it looks fine.
suhan 2012 26-Jan-13 11:39am
The same code concept works fine with my access database!! But what's wrong with sql server? I see a blank table when I open it.
PIEBALDconsult 26-Jan-13 11:59am
First, fix the architecture, then see if you also fixed the problem. If not, come back.
Richard MacCutchan 26-Jan-13 16:01pm
This is going to be a long hard road. :(
PIEBALDconsult 26-Jan-13 16:05pm
Hey, it's a rainy Saturday.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170924.2 | Last Updated 26 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100