469,934 Members | 1,632 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

Inserts into a DB with a Command

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
Nov 19 '05 #1
5 1680
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.

"Teo" <no********@homsany.net> wrote in message
news:u$**************@TK2MSFTNGP12.phx.gbl...
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

Nov 19 '05 #2
Teo
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

"Marina" <zl*******@nospam.hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.

"Teo" <no********@homsany.net> wrote in message
news:u$**************@TK2MSFTNGP12.phx.gbl...
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


Nov 19 '05 #3
Where is the cm object being instantiated? That is what's important.

"Teo" <no********@homsany.net> wrote in message
news:e2**************@tk2msftngp13.phx.gbl...
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

"Marina" <zl*******@nospam.hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.

"Teo" <no********@homsany.net> wrote in message
news:u$**************@TK2MSFTNGP12.phx.gbl...
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



Nov 19 '05 #4
Teo
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
"Marina" <zl*******@nospam.hotmail.com> wrote in message
news:eR**************@TK2MSFTNGP10.phx.gbl...
Where is the cm object being instantiated? That is what's important.

"Teo" <no********@homsany.net> wrote in message
news:e2**************@tk2msftngp13.phx.gbl...
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

"Marina" <zl*******@nospam.hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.

"Teo" <no********@homsany.net> wrote in message
news:u$**************@TK2MSFTNGP12.phx.gbl...
> 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
>
>



Nov 19 '05 #5
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
"Marina" <zl*******@nospam.hotmail.com> wrote in message
news:Oc**************@tk2msftngp13.phx.gbl...
What is a 'System' error? That really doesn't tell us much.

"Teo" <no********@homsany.net> wrote in message
news:en**************@TK2MSFTNGP11.phx.gbl...
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
"Marina" <zl*******@nospam.hotmail.com> wrote in message
news:eR**************@TK2MSFTNGP10.phx.gbl...
Where is the cm object being instantiated? That is what's important.

"Teo" <no********@homsany.net> wrote in message
news:e2**************@tk2msftngp13.phx.gbl...
> 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
>
> "Marina" <zl*******@nospam.hotmail.com> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
> > 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.
> >
> > "Teo" <no********@homsany.net> wrote in message
> > news:u$**************@TK2MSFTNGP12.phx.gbl...
> > > 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
> > >
> > >
> >
> >
>
>



Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Oasis | last post: by
1 post views Thread by Craig Stadler | last post: by
7 posts views Thread by Steven D.Arnold | last post: by
reply views Thread by Steve - DND | last post: by
3 posts views Thread by zelnaga | last post: by
9 posts views Thread by lenygold via DBMonster.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.