I'm trying to learn Vb.Net and using sql together and currently I ran into an issue when trying to insert information into my database, it always displays the same error when trying to insert information from a textbox into de DB.
System.Data.SqlClient.SqlException: 'Invalid column name 'nombre'.
Invalid column name 'calle'.
Invalid column name 'colonia'.
Invalid column name 'ciudad'.
Invalid column name 'cp'.
Invalid column name 'Tel1'.
Invalid column name 'Tel2'.
Invalid column name 'RFC'.'
Here is my code
Imports System.Data.Common
Imports System.Data.SqlClient
Public Class Agregar
Private myConn As SqlConnection
Private myCmd As SqlCommand
Private myReader As SqlDataReader
Private results As String
Private Sub Agregar_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myConn = New SqlConnection("Initial Catalog=Database1;" &
"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\sergi\source\repos\AppsPadre\AppsPadre\Database1.mdf;Integrated Security=True")
myConn.Open()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Me.Close()
Inicio.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Dim connectionString As String = "Data Source= DB\Database1;Initial Catalog=Orders;Integrated Security=True"
'Using connection As New SqlConnection(connectionString)
Dim nombre As String = txtNombre.Text
Dim calle As String = txtCalle.Text
Dim colonia As String = txtColonia.Text
Dim ciudad As String = txtCiudad.Text
Dim cp As String = txtCP.Text
Dim Tel1 As String = txtTel1.Text
Dim Tel2 As String = txtTel2.Text
Dim RFC As String = txtRFC.Text
If txtNombre.Text.Equals("") Then
MessageBox.Show("Porfavor escriba un Nombre.")
ElseIf txtCalle.Text.Equals("") Then
MessageBox.Show("Porfavor escriba una Calle.")
ElseIf txtColonia.Text.Equals("") Then
MessageBox.Show("Porfavor escriba una Colonia.")
ElseIf txtCiudad.Text.Equals("") Then
MessageBox.Show("Porfavor escriba una Ciudad.")
ElseIf txtCP.Text.Equals("") Then
MessageBox.Show("Porfavor escriba un Codigo Postal.")
ElseIf txtTel1.Text.Equals("") Then
MessageBox.Show("Porfavor escriba un Telefono.")
ElseIf txtRFC.Text.Equals("") Then
MessageBox.Show("Porfavor escriba un RFC.")
Else
/* Dim MyValues As String = String.Format("'{0}', '{1}', '{2}', {3}', '{4}', '{5}', '{6}', '{7}'", txtNombre.Text, txtCalle.Text, txtColonia.Text, txtCiudad.Text, txtCP.Text, txtTel1.Text, txtTel2.Text, txtRFC.Text)*/
Dim insertCommand As String = "INSERT INTO Remitente([nombre], [calle], [colonia], [ciudad], [cp], [tel_1], [tel_2], [rfc]) VALUES(nombre, calle, colonia, ciudad, cp, Tel1, Tel2, RFC)"
Dim cmd As New SqlCommand(insertCommand, myConn)
cmd.ExecuteNonQuery()
End If
End Sub
End Class
And here is my Database code/script:
CREATE TABLE [dbo].[Remitente] (
[Id_remitente] INT NOT NULL IDENTITY,
[nombre] VARCHAR (50) NOT NULL,
[calle] VARCHAR (100) NOT NULL,
[colonia] VARCHAR (50) NOT NULL,
[ciudad] VARCHAR (50) NOT NULL,
[cp] SMALLINT NOT NULL,
[tel_1] SMALLINT NOT NULL,
[tel_2] SMALLINT NULL,
[rfc] SMALLINT NOT NULL,
CONSTRAINT [PK_Table] PRIMARY KEY CLUSTERED ([Id_remitente] ASC)
);
I already look around and can't find a solution to my problem, any help would be appreciated.