|
-
Aug 30th, 2005, 04:33 AM
#1
Thread Starter
Fanatic Member
insert from text box to SQL please help RESOLVED
i have my connections set up to the database
i have a textbox called txt_name
a btn called btn_submit
how do i insert the information in the text box on the button click to my SQL database
Last edited by d2005; Aug 31st, 2005 at 04:57 AM.
-
Aug 30th, 2005, 04:42 AM
#2
Re: insert from text box
VB Code:
Dim myCommand As New SqlCommand("INSERT INTO tablename(fieldname) VALUES('" & txt_name.Text & "';", myConnectionObject)
myCommand.Connection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
-
Aug 30th, 2005, 04:58 AM
#3
Thread Starter
Fanatic Member
Re: insert from text box
i seem to get an error with the execute nonqquery line.
should this code be placed in the button click event.
Thanks
-
Aug 30th, 2005, 05:01 AM
#4
Re: insert from text box
What's the error? Where did you place the code?
-
Aug 30th, 2005, 05:12 AM
#5
Thread Starter
Fanatic Member
Re: insert from text box
i but the code here
Dim sqlInsertmobile As New SqlCommand("INSERT INTO user_table(mobile) VALUES('" & txt_name.Text & "';", oSQLConn)
sqlInsertmobile.Connection.Open()
sqlInsertmobile.ExecuteNonQuery()
oSQLConn.Close()
in the btn_submit_click
the error is
(title) Line 1: Incorrect syntax near ';'.
Exception Details: System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near ';'.
then
Line 79: Dim sqlInsertmobile As New SqlCommand("INSERT INTO user_table(mobile) VALUES('" & txt_name.Text & "';", oSQLConn)
Line 80: sqlInsertmobile.Connection.Open()
Line 81: sqlInsertmobile.ExecuteNonQuery()
Line 82: oSQLConn.Close()
Line 83: End Sub
line 81 seems to be the problem.
is SQL case sensitive as the mobile column would be Mobile
Last edited by d2005; Aug 30th, 2005 at 05:58 AM.
-
Aug 30th, 2005, 06:06 AM
#6
Re: insert from text box
Oops, my mistake, that should be:
Dim sqlInsertmobile As New SqlCommand("INSERT INTO user_table(mobile) VALUES('" & txt_name.Text & "');", oSQLConn)
-
Aug 30th, 2005, 06:18 AM
#7
Thread Starter
Fanatic Member
Re: insert from text box
yeah i have adapted to this now,
on tis page i have 2 textboxes to go into a table with 2 columns
mobile and name
when click the button i get an error
VB Code:
Private Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
Dim sqlInsertmob As New SqlCommand("INSERT INTO user_table(Mobile) VALUES('" & txt_mobile_number.Text & "');", oSQLConn)
Dim sqlInsertname As New SqlCommand("INSERT INTO user_table(Name) VALUES('" & txt_name.Text & "');", oSQLConn)
sqlInsertmob.Connection.Open()
'sqlInsertname.Connection.Open()
sqlInsertmob.ExecuteNonQuery()
sqlInsertname.ExecuteNonQuery()
oSQLConn.Close()
then i get this error, it seems to be a problem with my table???
Cannot insert the value NULL into column 'Name', table 'Con_test.dbo.user_table'; column does not allow nulls. INSERT fails. The statement has been terminated.
Last edited by d2005; Aug 30th, 2005 at 06:27 AM.
-
Aug 30th, 2005, 06:45 AM
#8
Re: insert from text box
Step through the code and check whether a value exists in txt_name.Text or whether it is valid at all.
Second, change this:
Code:
INSERT INTO user_table(Name) VALUES('
to
Code:
INSERT INTO user_table([Name]) VALUES('
Use square brackets for reserved keywords.
-
Aug 30th, 2005, 06:55 AM
#9
Thread Starter
Fanatic Member
Re: insert from text box
i put on the square brackets,
the textbox txt_name is just a box i dragged on, when i click the button it is to insert the details entered into the textboxes mobile number and name.
the error returned is
Cannot insert the value NULL into column 'Name', table 'Con_test.dbo.user_table'; column does not allow nulls. INSERT fails. The statement has been terminated.
Exception Details: System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'Name', table 'Con_test.dbo.user_table'; column does not allow nulls. INSERT fails. The statement has been terminated.
Line 84: sqlInsertmob.Connection.Open()
Line 85: 'sqlInsertname.Connection.Open()
Line 86: sqlInsertmob.ExecuteNonQuery()
Line 87: sqlInsertname.ExecuteNonQuery()
Line 88: oSQLConn.Close()
-
Aug 30th, 2005, 08:43 AM
#10
Thread Starter
Fanatic Member
Re: insert from text box to SQL please help PLEASE
THATS THE WHOLE CODE, ANY IDEAS WOULD BE APPRECIATED
VB Code:
Imports System
Imports System.data
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents txt_mobile_number As System.Web.UI.WebControls.TextBox
Protected WithEvents txt_name As System.Web.UI.WebControls.TextBox
Protected WithEvents btn_submit As System.Web.UI.WebControls.Button
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents lbl_time As System.Web.UI.WebControls.Label
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
' declare the data set and data adapters
'declare connection variable to database
Private dsUsers As New DataSet
Private daUsers As New SqlDataAdapter
Dim oSQLConn As SqlConnection = New SqlConnection
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'call load users
loadusers()
End Sub
Private Sub loadusers()
'connect to database
'Dim oSQLConn As SqlConnection = New SqlConnection
oSQLConn.ConnectionString = "Data Source=(local);" & _
"Initial Catalog=Con_Test;" & _
"Integrated Security=SSPI"
oSQLConn.Open()
'databind the infromation
'If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
daUsers.SelectCommand = New SqlCommand("SELECT * from user_table ORDER BY Name", oSQLConn)
daUsers.Fill(dsUsers)
'DataGrid()
DataGrid1.DataSource = dsUsers.Tables(0)
DataGrid1.DataBind()
oSQLConn.Close()
End Sub
Private Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
Dim sqlInsertname As New SqlCommand("INSERT INTO user_table([Name]) VALUES('" & txt_name.Text & "');", oSQLConn)
Dim sqlInsertmob As New SqlCommand("INSERT INTO user_table(Mobile) VALUES('" & txt_mobile_number.Text & "');", oSQLConn)
'sqlInsertmob.Connection.Open()
sqlInsertname.Connection.Open()
sqlInsertmob.ExecuteNonQuery()
sqlInsertname.ExecuteNonQuery()
oSQLConn.Close()
'Server.Transfer("user_table.aspx")
End Sub
End Class
-
Aug 30th, 2005, 11:38 AM
#11
Re: insert from text box to SQL please help
Did you step through the code?
-
Aug 31st, 2005, 04:56 AM
#12
Thread Starter
Fanatic Member
Re: insert from text box to SQL please help
yeah buddy i got it workin,
i was using more than one connection and they must cancel each other out, the trick is to use one line of code for multiple inserts,
Dim sqlInsertuser As New SqlCommand("INSERT INTO tb_user(c_user_id,c_orig_addr, VALUES('" & txt_mobile_number.Text & "','" & txt_name.Text & "');", oSQLConn)
sqlInsertuser.Connection.Open()
sqlInsertuser.ExecuteNonQuery()
oSQLConn.Close()
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|