Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Imports Microsoft.SqlServer
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Public con As New SqlConnection
Public cmd As New SqlCommand
Public Sub clearData()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
TextBox10.Text = ""
TextBox11.Text = ""
TextBox12.Text = ""
TextBox13.Text = ""
End Sub

Public Sub CreateCommand(ByVal queryString As String, ByVal connectionString As String)
con = New SqlConnection(connectionString)
con.Open()
cmd = New SqlCommand(queryString, con)
cmd.ExecuteNonQuery()
End Sub

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Dim connetionString As String
Dim sqlquery As String
connetionString = "Data Source=Home-pc\SQLEXPRESS; Initial Catalog=School; Integrated Security=True"
Dim ID As String
Dim StudentName As String
Dim FatherName As String
Dim Address1 As String
Dim Address2 As String
Dim City As String
Dim Pincode As String
Dim Standard As String
Dim Section As String
Dim Fees As String
Dim Joining As String
Dim Stream As String
Dim Phone As String

ID = TextBox1.Text
StudentName = TextBox2.Text
FatherName = TextBox3.Text
Address1 = TextBox4.Text
Address2 = TextBox5.Text
City = TextBox6.Text
Pincode = TextBox7.Text
Standard = TextBox8.Text
Section = TextBox9.Text
Fees = TextBox10.Text
Joining = TextBox11.Text
Stream = TextBox12.Text
Phone = TextBox13.Text

sqlquery = "insert into StudentMaster(Studentname,fathername,address1,address2,City,Pincode,standard,section,fees,joining,stream,phone) Values (TextBox1.Text,'" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "',"
'" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "',TextBox10.Text,
'" + TextBox11.Text + "','" + TextBox12.Text + "','" + TextBox13.Text + "')"
Try
CreateCommand(sqlquery, connetionString)
MsgBox("Data is successfully stored ! ")
clearData()
Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally
con.Close()
End Try
End Sub

Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcancel.Click
Close()
End Sub

Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
TextBox10.Text = ""
TextBox11.Text = ""
TextBox12.Text = ""
TextBox13.Text = ""

End Sub

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

End Sub

Private Sub Cmdsave_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmdsave.Click

End Sub

Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged

End Sub

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

End Sub

Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click

End Sub
End Class
Posted

While Mehdi is right - that will probably start to solve your immediate problem, it leave you wide open to SQL Injection attacks, which can damage or destroy your database.
Never concatenate strings to form an SQL command - it's really dangerous! Always use parameterized queries instead.

BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it is three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
Share this answer
 
v2
You need to put quotes around the TextBox1.Text -> ...('"+TextBox1.Text+"',...
 
Share this answer
 

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