Results 1 to 5 of 5

Thread: Link vb.net to sql server

  1. #1
    Join Date
    Mar 2009
    Posts
    1,221

    Link vb.net to sql server

    I use Visual Basic 2008. I have a table in sql server. DDL:
    Code:
    CREATE TABLE Names
    (
    Id int PRIMARY KEY NOT NULL ,
    Name varchar(255)
    )
    On my form I have 2 textbox (id and name) that I complete. I want when I press the button (button3_Click) (it takes me the specific lines) in Table Test. (Each press has introduced what he had written in 2 textbox). In button3_Click is my problem. I attach the complete source (vb 2008) to be easier to help.

    Example output:
    In the 2 textboxes I placed 1 Michel

    Then I press the button to introduce the first line in table names

    After that I introduced 2 Christian

    Then I press for the second time the button to automatically introduce the second line in table names in sql server.

    So desire output:

    1 Michel (which was introduced by example in the 2 textboxes on my form)

    2 Christian
    I give you an example here. I put in textbox1 Test1, in textbox2 Test2. I press the button insert into table. But it is not properly inserted. Normally in this case I want the output:

    Code:
      Name Surname
    
     Test1 Test2

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Link vb.net to sql server

    I think you put a lot of questions, each accompanied by a block that nobody wants to read. It would be best that you read a course/tutorials, several are already present on internet. And the best is to have clear and simple questions on issues that are not explained in the faq or in the tutorials.

  3. #3
    Join Date
    Mar 2009
    Posts
    1,221

    Re: Link vb.net to sql server

    Ok this is probably my fault so I give you the code. So what is the problem in my code? 'Button to insert 2 values from 2 textboxes in table names created in sql server.

    Code:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
     
            'Public Class account
            'myconnection=connection
            'comandasql=mycommand
            'dr- citestedate
            '
     
         Imports System.Data
         Imports System.Data.SqlClient
     
            Dim connection As SqlConnection
            Dim comandasql As SqlCommand
            Dim citestedate As SqlDataReader
            Dim ra As Integer 
     
            connection = New SqlConnection("server=HOME-D16ABD96A8\PRIMAVERA;uid=mihaispr;pwd=mihai;database=ParcAuto")
            connection.Open()
            comandasql = New SqlCommand("insert into Names([Id],[Name]) values (' " & Textbox1.Text & "','" & Textbox2.Text & " ')", connection)
            comandasql.ExecuteNonQuery()
            MessageBox.Show("New Row Inserted" & ra)
            connection.Close()
        End Sub
    The basis of the give table called Test Names.
    Code:
    CREATE DATABASE Test
     
    CREATE TABLE Names
    (Id int PRIMARY KEY NOT NULL,
    Name varchar(255)
    )
     
    SELECT * FROM Names

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Link vb.net to sql server

    It's nice to give us the code, saying there is a problem, but it would clarify what the problem is, if an error message as to give with the line that goes into error, is no soothsayer!

    At first sight I see is that you insert in your specific you'll define 3 fields and the values you only gave 2. It's nice to give a zip code with your source, but I do not think anyone will open, however, giving the few lines of code with the worries, and more on request

    I would add that you should not append a query with the values of textbox, that can easily be bugger and is very dangerous. You should use SQLPARAMETERS. And finally, put the try catch as much as possible, especially on access to a database that has a lot of error!

  5. #5
    Join Date
    Nov 2008
    Posts
    1,185

    Re: Link vb.net to sql server

    The first problem is that you copy the code without foolishly trying to understand the principle. You do an INSERT INTO 3 fields on your table but you specify only 2 values. Remove the ID field in the INSERT INTO and change this field to make it in automatic numbering in the database (IDENTITY (1,1) I think).

    Then thank you for using parametrized queries, the string concatenation...

Similar Threads

  1. D-Link DNS-321 media server create problem when accessing the files
    By feNRE123 in forum Networking & Security
    Replies: 4
    Last Post: 04-04-2011, 04:34 AM
  2. Windows 2003 server Standard Edition 180 day Trial software link
    By Jason Blake in forum Windows Server Help
    Replies: 1
    Last Post: 29-06-2010, 05:54 PM
  3. Changing link color when moving mouse over link in HTML
    By Cade in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 07:46 PM
  4. D-Link Media Server Issue
    By tecHAMMER in forum Networking & Security
    Replies: 4
    Last Post: 04-11-2009, 01:43 PM
  5. D-Link DIR-655, Intel WiFi Link 4965a/g/n, Vista and 802.11n
    By CaspaR in forum Windows Vista Network
    Replies: 1
    Last Post: 14-07-2007, 04:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,307,078.97507 seconds with 16 queries