Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
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)
        cmd.ExecuteNonQuery()
        MsgBox("data saved successfully.")

        cnn.Close()

    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"
        cnn.Open()
    End Sub
End Class




Do I need to add more to save the data from the textboxes to database?
Posted
Comments
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
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 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?
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;"
Mike Meinz 26-Jan-13 11:13am    
My ConnectionStrings look like this format:

"Database=MyDatabaseName;Server=MyPCName\SQLEXPRESS;Integrated Security=True;Net=dbmssocn;"

1 solution

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.
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900