Foros del Web » Programación para mayores de 30 ;) » .NET »

Problema al insertar datos BD

Estas en el tema de Problema al insertar datos BD en el foro de .NET en Foros del Web. Hola estoy haciendo el codigo para que atraves de una plantilla se puedan insertar datos, en mi platilla tengo texbox y dropdownlist cuando defino el ...
  #1 (permalink)  
Antiguo 15/03/2005, 08:58
Avatar de Sayra  
Fecha de Ingreso: diciembre-2004
Mensajes: 365
Antigüedad: 19 años, 5 meses
Puntos: 1
Problema al insertar datos BD

Hola estoy haciendo el codigo para que atraves de una plantilla se puedan insertar datos, en mi platilla tengo texbox y dropdownlist

cuando defino el parametro a incertar en la proveniente del texbos le digo asi

MyCommand.Parameters.Add(New SqlParameter("@Horario", SqlDbType.NChar, 40))
MyCommand.Parameters("@Horario").Value = textBoxHorario.Text

por ende pense que si cuando vinieran de lo drop podia definirse asi

BC30456: 'DropDownList' no es un miembro de 'System.Web.UI.WebControls.DropDownList'.
y no se como definirlo entonces

estos drop estan enlazados a una tabla de una base de datos sql
y son tres que estan concatendaos entre si drop1=Estado, Drop2= Ciudad, Drop3= Ubicacion.

Gracias de antemano
  #2 (permalink)  
Antiguo 15/03/2005, 09:29
Avatar de Sayra  
Fecha de Ingreso: diciembre-2004
Mensajes: 365
Antigüedad: 19 años, 5 meses
Puntos: 1
Bueno ya resolvi ese datallito le pongo

MyCommand.Parameters.Add(New SqlParameter("@Estado", SqlDbType.NChar, 40))
MyCommand.Parameters("@Estado").Value = DropDownList1.SelectedItem.Value

y listo

pe aun me genera error en la insercion y no se porque

abajo les pongo mi godigo de insercion a ver si alguno le le ve el error y muchas gracias

Sub buttonAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Message.InnerHtml = ""

If (Page.IsValid)

Dim DS As DataSet
Dim MyCommand As SqlCommand

Dim InsertCmd As String = "insert into Agencia (CodigoID, Agencia, Direccion, Codigo_Area, Telefono,Fax, Horario, Estado, Ciudad, Ubicacion, Estacionamiento, Cajero_Automatico, Autobanco, Taquilla_Externa) values (@CodigoID, @Agencia, @Direccion, @Codigo_Area, @Telefono, @Fax, @Horario, @Estado, @Ciudad, @Ubicacion, @Estacionamiento, @Cajero_Automatico, @Autobanco, @Taquilla_Externa)"

MyCommand = New SqlCommand(InsertCmd, MyConnection)

MyCommand.Parameters.Add(New SqlParameter("@CodigoID", SqlDbType.NVarChar, 11))
MyCommand.Parameters("@CodigoID").Value = textBoxCodigoID.Text

MyCommand.Parameters.Add(New SqlParameter("@Agencia", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@Agencia").Value = textBoxAgencia.Text

MyCommand.Parameters.Add(New SqlParameter("@Direccion", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@Direccion").Value = textBoxDireccion.Text

MyCommand.Parameters.Add(New SqlParameter("@Codigo_Area", SqlDbType.NChar, 12))
MyCommand.Parameters("@Codigo_Area").Value = textBoxCodigoArea.Text

MyCommand.Parameters.Add(New SqlParameter("@Telefono", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@Telefono").Value = textBoxTelefono.Text

MyCommand.Parameters.Add(New SqlParameter("@Fax", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@Fax").Value = textBoxFax.Text

MyCommand.Parameters.Add(New SqlParameter("@Horario", SqlDbType.NChar, 40))
MyCommand.Parameters("@Horario").Value = textBoxHorario.Text

MyCommand.Parameters.Add(New SqlParameter("@Estado", SqlDbType.NChar, 40))
MyCommand.Parameters("@Estado").Value = DropDownList1.SelectedItem.Value

MyCommand.Parameters.Add(New SqlParameter("@Ciudad", SqlDbType.NChar,40))
MyCommand.Parameters("@Ciudad").Value = DropDownList2.SelectedItem.Value

MyCommand.Parameters.Add(New SqlParameter("@Ubicacion", SqlDbType.NChar,40))
MyCommand.Parameters("@Ubicacion").Value = DropDownList3.SelectedItem.Value

MyCommand.Parameters.Add(New SqlParameter("@Estacionamiento", SqlDbType.NChar,40))
MyCommand.Parameters("@Estacionamiento").Value = textBoxEstacio.Text

MyCommand.Parameters.Add(New SqlParameter("@Cajero_Automatico", SqlDbType.NChar,40))
MyCommand.Parameters("@Cajero_Automatico").Value = textBoxCajero.Text

MyCommand.Parameters.Add(New SqlParameter("@Autobanco", SqlDbType.NChar,40))
MyCommand.Parameters("@Autobanco").Value = textBoxAutobanco.Text

MyCommand.Parameters.Add(New SqlParameter("@Taquilla_Externa", SqlDbType.NChar,40))
MyCommand.Parameters("@Taquilla_Externa").Value = textBoxTaquilla.Text

MyCommand.Connection.Open()

Try
MyCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>Record Added</b><br>" & InsertCmd.ToString()
Catch Exp As SQLException
If Exp.Number = 2627
Message.InnerHtml = "ERROR: A record already exists with the same primary key"
Else
Message.InnerHtml = "ERROR: Could not add record, please ensure the fields are correctly filled out"
End If
Message.Style("color") = "red"
End Try

MyCommand.Connection.Close()

End If
BindGrid()

End Sub
  #3 (permalink)  
Antiguo 15/03/2005, 09:37
 
Fecha de Ingreso: octubre-2004
Mensajes: 94
Antigüedad: 19 años, 7 meses
Puntos: 0
Que error te da????
  #4 (permalink)  
Antiguo 15/03/2005, 09:47
Avatar de Sayra  
Fecha de Ingreso: diciembre-2004
Mensajes: 365
Antigüedad: 19 años, 5 meses
Puntos: 1
Pues el error me da este error

ERROR: Could not add record, please ensure the fields are correctly filled out


es cuando lo estoy ejecutando una vez que presiono el boton agregar
puedo insertar

me sale ese mensaje en rojo

ese mensaje esta definido en el codigo
Else
Message.InnerHtml = "ERROR: Could not add record, please ensure the fields are correctly filled out"
End If
Message.Style("color") = "red"
  #5 (permalink)  
Antiguo 15/03/2005, 10:02
Avatar de neivan  
Fecha de Ingreso: febrero-2005
Mensajes: 539
Antigüedad: 19 años, 3 meses
Puntos: 1
añadele exp.message para haber si te especifica algo mas el error
añadeselo al final con un &
ya q el nombre del erro ya esta definido en el codigo y podria ser cualquier cosa
  #6 (permalink)  
Antiguo 15/03/2005, 10:21
Avatar de Sayra  
Fecha de Ingreso: diciembre-2004
Mensajes: 365
Antigüedad: 19 años, 5 meses
Puntos: 1
Oye estuve buenisisma tu idea. El error es este ERROR: Could not add record, please ensure the fields are correctly filled outSintaxis incorrecta cerca de la palabra clave 'INNER'.
Yo cambie un poquito el codijo por que pense que el error se debia a lo mejor a que no ha bia espcificado que EstadoID, CiudadID, UbicacionID son claves en la tabla agencia y en form se seleccionan de un drop , entonces lo puse asi

Sub buttonAdd_Click(Sender As Object, E As EventArgs)
Message.InnerHtml = ""

If (Page.IsValid)

Dim DS As DataSet
Dim MyCommand As SqlCommand

Dim InsertCmd As String = "insert into Agencia (CodigoID, Agencia, Direccion, Codigo_Area, Telefono, Fax, Horario, EstadoID, CiudadID, UbicacionID, Estacionamiento, Cajero_Automatico, Autobanco, Taquilla_Externa) values (@CodigoID, @Agencia, @Direccion, @Codigo_Area, @Telefono, @Fax, @Horario, @Estado, @Ciudad, @Ubicacion, @Estacionamiento, @Cajero_Automatico, @Autobanco, @Taquilla_Externa)"
InsertCmd += " INNER JOIN Estado, Ciudad, Ubicacion"
InsertCmd += " WHERE Agencia.EstadoID=Estado.EstadoID"
InsertCmd += " AND Agencia.CiudadID=Ciudad.CiudadID"
InsertCmd += " AND Agencia.UbicacionID=Ubicacion.UbicacionID"
InsertCmd += " AND Agencia.EstadoID=" &DropDownList1.SelectedItem.Value
InsertCmd += " AND Agencia.CiudadID=" &DropDownList2.SelectedItem.Value
InsertCmd += " AND Agencia.UbicacionID=" &DropDownList3.SelectedItem.Value


MyCommand = New SqlCommand(InsertCmd, MyConnection)

MyCommand.Parameters.Add(New SqlParameter("@CodigoID", SqlDbType.NVarChar, 11))
MyCommand.Parameters("@CodigoID").Value = textBoxCodigoID.Text

MyCommand.Parameters.Add(New SqlParameter("@Agencia", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@Agencia").Value = textBoxAgencia.Text

MyCommand.Parameters.Add(New SqlParameter("@Direccion", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@Direccion").Value = textBoxDireccion.Text

MyCommand.Parameters.Add(New SqlParameter("@Codigo_Area", SqlDbType.NChar, 12))
MyCommand.Parameters("@Codigo_Area").Value = textBoxCodigoArea.Text

MyCommand.Parameters.Add(New SqlParameter("@Telefono", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@Telefono").Value = textBoxTelefono.Text

MyCommand.Parameters.Add(New SqlParameter("@Fax", SqlDbType.NVarChar, 40))
MyCommand.Parameters("@Fax").Value = textBoxFax.Text

MyCommand.Parameters.Add(New SqlParameter("@Horario", SqlDbType.NChar, 40))
MyCommand.Parameters("@Horario").Value = textBoxHorario.Text

MyCommand.Parameters.Add(New SqlParameter("@Estado", SqlDbType.NChar, 40))
MyCommand.Parameters("@Estado").Value = DropDownList1.SelectedItem.Value

MyCommand.Parameters.Add(New SqlParameter("@Ciudad", SqlDbType.NChar,40))
MyCommand.Parameters("@Ciudad").Value = DropDownList2.SelectedItem.Value

MyCommand.Parameters.Add(New SqlParameter("@Ubicacion", SqlDbType.NChar,40))
MyCommand.Parameters("@Ubicacion").Value = DropDownList3.SelectedItem.Value

MyCommand.Parameters.Add(New SqlParameter("@Estacionamiento", SqlDbType.NChar,40))
MyCommand.Parameters("@Estacionamiento").Value = textBoxEstacio.Text

MyCommand.Parameters.Add(New SqlParameter("@Cajero_Automatico", SqlDbType.NChar,40))
MyCommand.Parameters("@Cajero_Automatico").Value = textBoxCajero.Text

MyCommand.Parameters.Add(New SqlParameter("@Autobanco", SqlDbType.NChar,40))
MyCommand.Parameters("@Autobanco").Value = textBoxAutobanco.Text

MyCommand.Parameters.Add(New SqlParameter("@Taquilla_Externa", SqlDbType.NChar,40))
MyCommand.Parameters("@Taquilla_Externa").Value = textBoxTaquilla.Text

MyCommand.Connection.Open()

Try
MyCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>Record Added</b><br>" & InsertCmd.ToString()
Catch Exp As SQLException
If Exp.Number = 2627
Message.InnerHtml = "ERROR: A record already exists with the same primary key"
Else
Message.InnerHtml = "ERROR: Could not add record, please ensure the fields are correctly filled out" &Exp.Message
End If
Message.Style("color") = "red"
End Try

MyCommand.Connection.Close()

End If
BindGrid()

End Sub
  #7 (permalink)  
Antiguo 15/03/2005, 12:12
Avatar de Sayra  
Fecha de Ingreso: diciembre-2004
Mensajes: 365
Antigüedad: 19 años, 5 meses
Puntos: 1
Pero me continuo mostrnado el mismo error como hago para indicarle en el insert que los campos EstadoiD, ciudadID y UbicacionID les igual a lo que estan seleccionando los drop

Dim InsertCmd As String = "insert into Agencia (CodigoID, Agencia, Direccion, Codigo_Area, Telefono, Fax, Horario, EstadoID, CiudadID, UbicacionID, Estacionamiento, Cajero_Automatico, Autobanco, Taquilla_Externa) values (@CodigoID, @Agencia, @Direccion, @Codigo_Area, @Telefono, @Fax, @Horario, @Estado, @Ciudad, @Ubicacion, @Estacionamiento, @Cajero_Automatico, @Autobanco, @Taquilla_Externa)"
InsertCmd += " WHERE Agencia.EstadoID=Estado.EstadoID"
InsertCmd += " AND Agencia.CiudadID=Ciudad.CiudadID"
InsertCmd += " AND Agencia.UbicacionID=Ubicacion.UbicacionID"
InsertCmd += " AND Agencia.EstadoID=" &DropDownList1.SelectedItem.Value
InsertCmd += " AND Agencia.CiudadID=" &DropDownList2.SelectedItem.Value
InsertCmd += " AND Agencia.UbicacionID=" &DropDownList3.SelectedItem.Value
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 21:44.