Results 1 to 12 of 12

Thread: insert from text box to SQL please help RESOLVED

  1. #1

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: insert from text box

    VB Code:
    1. Dim myCommand As New SqlCommand("INSERT INTO tablename(fieldname) VALUES('" & txt_name.Text & "';", myConnectionObject)
    2.     myCommand.Connection.Open()
    3.     myCommand.ExecuteNonQuery()
    4.     myConnection.Close()

  3. #3

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: insert from text box

    What's the error? Where did you place the code?

  5. #5

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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)

  7. #7

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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:
    1. Private Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
    2.  
    3.         Dim sqlInsertmob As New SqlCommand("INSERT INTO user_table(Mobile) VALUES('" & txt_mobile_number.Text & "');", oSQLConn)
    4.         Dim sqlInsertname As New SqlCommand("INSERT INTO user_table(Name) VALUES('" & txt_name.Text & "');", oSQLConn)
    5.  
    6.         sqlInsertmob.Connection.Open()
    7.         'sqlInsertname.Connection.Open()
    8.         sqlInsertmob.ExecuteNonQuery()
    9.         sqlInsertname.ExecuteNonQuery()
    10.         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.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  9. #9

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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()

  10. #10

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: insert from text box to SQL please help PLEASE

    THATS THE WHOLE CODE, ANY IDEAS WOULD BE APPRECIATED

    VB Code:
    1. Imports System
    2. Imports System.data
    3. Imports System.Data.SqlClient
    4.  
    5. Public Class WebForm1
    6.     Inherits System.Web.UI.Page
    7.  
    8. #Region " Web Form Designer Generated Code "
    9.  
    10.     'This call is required by the Web Form Designer.
    11.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    12.  
    13.     End Sub
    14.     Protected WithEvents txt_mobile_number As System.Web.UI.WebControls.TextBox
    15.     Protected WithEvents txt_name As System.Web.UI.WebControls.TextBox
    16.     Protected WithEvents btn_submit As System.Web.UI.WebControls.Button
    17.     Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    18.     Protected WithEvents lbl_time As System.Web.UI.WebControls.Label
    19.     Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    20.     Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    21.  
    22.     'NOTE: The following placeholder declaration is required by the Web Form Designer.
    23.     'Do not delete or move it.
    24.     Private designerPlaceholderDeclaration As System.Object
    25.  
    26.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    27.         'CODEGEN: This method call is required by the Web Form Designer
    28.         'Do not modify it using the code editor.
    29.         InitializeComponent()
    30.     End Sub
    31.  
    32. #End Region
    33.  
    34.     ' declare the data set and data adapters
    35.     'declare connection variable to database
    36.     Private dsUsers As New DataSet
    37.     Private daUsers As New SqlDataAdapter
    38.     Dim oSQLConn As SqlConnection = New SqlConnection
    39.    
    40.    
    41.  
    42.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    43.         'call load users
    44.         loadusers()
    45.        
    46.  
    47.  
    48.     End Sub
    49.  
    50.     Private Sub loadusers()
    51.         'connect to database
    52.  
    53.         'Dim oSQLConn As SqlConnection = New SqlConnection
    54.         oSQLConn.ConnectionString = "Data Source=(local);" & _
    55.         "Initial Catalog=Con_Test;" & _
    56.         "Integrated Security=SSPI"
    57.         oSQLConn.Open()
    58.  
    59.  
    60.         'databind the infromation
    61.         'If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
    62.      
    63.         daUsers.SelectCommand = New SqlCommand("SELECT * from user_table ORDER BY Name", oSQLConn)
    64.         daUsers.Fill(dsUsers)
    65.  
    66.         'DataGrid()
    67.         DataGrid1.DataSource = dsUsers.Tables(0)
    68.         DataGrid1.DataBind()
    69.         oSQLConn.Close()
    70.        
    71.        
    72.  
    73.     End Sub
    74.  
    75.  
    76.  
    77.  
    78.    
    79.     Private Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
    80.  
    81.  
    82.         Dim sqlInsertname As New SqlCommand("INSERT INTO user_table([Name]) VALUES('" & txt_name.Text & "');", oSQLConn)
    83.         Dim sqlInsertmob As New SqlCommand("INSERT INTO user_table(Mobile) VALUES('" & txt_mobile_number.Text & "');", oSQLConn)
    84.        
    85.         'sqlInsertmob.Connection.Open()
    86.         sqlInsertname.Connection.Open()
    87.         sqlInsertmob.ExecuteNonQuery()
    88.         sqlInsertname.ExecuteNonQuery()
    89.         oSQLConn.Close()
    90.         'Server.Transfer("user_table.aspx")
    91.  
    92.        
    93.  
    94.  
    95.     End Sub
    96. End Class

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: insert from text box to SQL please help

    Did you step through the code?

  12. #12

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    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
  •  



Click Here to Expand Forum to Full Width