Inserts into a DB with a Command

  • Thread starter Thread starter Teo
  • Start date Start date
T

Teo

Hi! Sorry for my many postings on this Newsgroup. I am new to ADO.NET and I
got Sceppa's book on ADO.NET. It's a great book but covers a lot on the
DataAdapter and Offline Data. Now, if I want to Insert Data into my MSDE
Database, can I do it without using the DataAdapter?
Here are the steps I use to insert the data.
Open Connection
Add Parameters, set direction and specify value.
Call my connection.ExecuteNonQuery()
Close the Connection.

Now, I keep getting this error on the Execute reader. I am in no point
calling a Reader for the table. I just specify my stored procedure (that
only have INSERT statements) and I just want to insert the values, not
retrieve anything.
This is the error I get "Additional information: ExecuteReader: Connection
property has not been initialized."
Do you know what might be causing this? I am using a SQLConnection and
SQLCommand. That's all.

Teo
 
I believe all those methods use the ExecuteReader method in the background,
so that is why the top level in the stack has that.

If you post some specific code, we can be more helpful. It sounds like your
are not setting the Connectio property of your command.
 
Hey Marina, sure, here's my VB code below, I am trying to just do a
ExecuteNonQuery, so all the values I assign before that go into the DB with
the stored procedure.
Here is the code.
Thanks so much,
Teo

Private Sub btnagregar_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnagregar.Click

Dim cedula, nombre, apellido, direccion, ciudad, estado, zip, apostal As
String

Dim estudiante, privada, comercial, instrumentos, multimotor, instructor,
atp As Boolean

Dim fechanacimiento As Date

Dim param As SqlParameter















'Asignar valores

cedula = txtid.Text

apellido = txtapellido.Text

nombre = txtnombre.Text

direccion = txtdireccion.Text

ciudad = txtciudad.Text

estado = txtestado.Text

zip = txtzip.Text

apostal = txtapostal.Text

fechanacimiento = CDate(dtpfechanacimiento.Value)

estudiante = chkestudiante.CheckState

privada = chkprivada.CheckState

comercial = chkcomercial.CheckState

instrumentos = chkinstrumentos.CheckState

multimotor = chkmulti.CheckState

instructor = chkinstructor.CheckState

atp = chkatp.CheckState



cn.Open()



param = cm.Parameters.Add("@id", SqlDbType.Char, 15)

param.Value = cedula

param.Direction = ParameterDirection.Input



param = cm.Parameters.Add("@apellido", SqlDbType.VarChar, 50)

param.Value = apellido

param.Direction = ParameterDirection.Input

param = cm.Parameters.Add("@nombre", SqlDbType.VarChar, 50)

param.Value = nombre

param.Direction = ParameterDirection.Input

param = cm.Parameters.Add("@direccion", SqlDbType.VarChar, 200)

param.Value = direccion

param.Direction = ParameterDirection.Input

param = cm.Parameters.Add("@ciudad", SqlDbType.VarChar, 50)

param.Value = ciudad

param.Direction = ParameterDirection.Input

param = cm.Parameters.Add("@estado", SqlDbType.VarChar, 50)

param.Value = estado

param.Direction = ParameterDirection.Input

param = cm.Parameters.Add("@zip", SqlDbType.VarChar, 10)

param.Value = zip

param.Direction = ParameterDirection.Input

param = cm.Parameters.Add("@apostal", SqlDbType.VarChar, 200)

param.Value = apostal

param.Direction = ParameterDirection.Input

param = cm.Parameters.Add("@fechanacimiento", SqlDbType.DateTime, 8)

param.Value = fechanacimiento

param.Direction = ParameterDirection.Input

param = cm.Parameters.Add("@estudiante", SqlDbType.Char, 10)

param.Value = estudiante

param.Direction = ParameterDirection.Input

param = cm.Parameters.Add("@privada", SqlDbType.Char, 10)

param.Value = privada

param.Direction = ParameterDirection.Input

param = cm.Parameters.Add("@comercial", SqlDbType.Char, 10)

param.Value = comercial

param.Direction = ParameterDirection.Input

param = cm.Parameters.Add("@instrumentos", SqlDbType.Char, 10)

param.Value = instrumentos

param.Direction = ParameterDirection.Input

param = cm.Parameters.Add("@multi", SqlDbType.Char, 10)

param.Value = multimotor

param.Direction = ParameterDirection.Input

param = cm.Parameters.Add("@instructor", SqlDbType.Char, 10)

param.Value = instructor

param.Direction = ParameterDirection.Input

param = cm.Parameters.Add("@atp", SqlDbType.Char, 10)

param.Value = atp

param.Direction = ParameterDirection.Input





cm.ExecuteNonQuery()











MsgBox("Su entrada ha sido procesada...")



cn.Close()

End Sub
 
Hey Marina, I usually do that on the object itself, not in code. I actually
added the cm.connection = cn where I initialize the connection. But now I
get a System error.

Teo
 
Nevermind, it's working now!!! It's a good feeling when you get something to
work!!! I'll throw a party now ;-)
Thanks so much Marina!

Teo
 
Back
Top