Button to insert values into sql server table

Link between winforms vb.net and sql server

Page 1 of 1

2 Replies - 13082 Views - Last Post: 16 November 2009 - 05:48 AM Rate Topic: -----

#1 mihaispr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 29-September 09

Button to insert values into sql server table

Posted 14 November 2009 - 10:48 AM

Hi everyone and thanks for reading my question!

I want to implement a button that sends values typed in textboxes of gui(vb.net i'm using visual basic 2008) into a sql server table called Noms.

Example:

2 textboxes

id=1
nom = michael

Press button send the the 2 values into the 2 columns (id and name) of Noms table

Then I enter in textbox(gui vb.net) id=2 and nom=mike

So the output in my Noms table of sql server should be:

Id Nom

1 michael
2 mike



Here's the code implemented in button to send the values into the specified table:


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
 
		'Public Class account
		'myconnection=conexiune
		'comandasql=mycommand
		'dr- citestedate
		'
 
	 Imports System.Data
	 Imports System.Data.SqlClient
 
		Dim conexiune As SqlConnection
		Dim comandasql As SqlCommand
		Dim citestedate As SqlDataReader
		Dim ra As Integer 'pt.un rand nou introdus
 
		conexiune = New SqlConnection("server=HOME-D16ABD96A8\PRIMAVERA;uid=mihaispr;pwd=mihai;database=Test")
		conexiune.Open()
		comandasql = New SqlCommand("insert into Noms([Id],[Nom]) values (' " & Textbox1.Text & "','" & Textbox2.Text & " ')", conexiune)
		comandasql.ExecuteNonQuery()
		MessageBox.Show("New Row Inserted" & ra)
		conexiune.Close()
	End Sub
 



Hope anyone has an idea to solve this.

:^:

I don't know what I'm wrong my name of database is ParcAuto my table name is Noms.

Name of server is the server name from sql server , username:mihaispr and password:mihai.

Here is the DDL of table sql server:

 
CREATE DATABASE Test
 
CREATE TABLE Noms
(Id int PRIMARY KEY NOT NULL,
Nom varchar(255)
)
 
SELECT * FROM Noms
 


This post has been edited by mihaispr: 14 November 2009 - 11:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Button to insert values into sql server table

#2 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Button to insert values into sql server table

Posted 16 November 2009 - 02:44 AM

Hi,

Your code looks all ok,

Do you get an error?
Was This Post Helpful? 0
  • +
  • -

#3 mihaispr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 29-September 09

Re: Button to insert values into sql server table

Posted 16 November 2009 - 05:48 AM

View Postmotcom, on 16 Nov, 2009 - 01:44 AM, said:

Hi,

Your code looks all ok,

Do you get an error?



Hi! I have some errors (I attach you the file to better see); when I press the button insert from vb.net form it doesn't inserts the 2 values from my 2 textboxes in the specified columns of table called Noms(the 2 columns are Nom and Prenom).


I attach here the .sql file to automatically generate the table structure, and the vb.net application.


The user types in Nom label (Mike) and in Prenom Label (Bottomley) and when I press the button Insert in column Nom(it's Mike and in column Prenom it's Bottomley) so it inserts in table Noms a first row.


I attach you here the 2 files (first creates automatically the structure of table in sql server), the second is for vb.net. ( i have some errors of syntax which I don't know how to correct them I attach the second file in order to simply see my errors).



Attached File(s)


This post has been edited by mihaispr: 16 November 2009 - 05:57 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1