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

No Inserta Datos en Base de Datos

Estas en el tema de No Inserta Datos en Base de Datos en el foro de .NET en Foros del Web. Hola chicos ojala me puedan dar una mano!! soy super nuevo en esto, en realidad años que no programaba y no me acuerdo de nada. ...
  #1 (permalink)  
Antiguo 05/10/2009, 09:43
 
Fecha de Ingreso: mayo-2008
Mensajes: 5
Antigüedad: 16 años
Puntos: 0
No Inserta Datos en Base de Datos

Hola chicos ojala me puedan dar una mano!! soy super nuevo en esto, en realidad años que no programaba y no me acuerdo de nada. tengo el siguiente codigo y cree una clase para insertar información extraida de un formulario y todo bien pero al tirar el mensaje sale que no a registrado al usuario.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mantenedor_Usuario : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
User oUser = new User();
DataSet oDS = new DataSet();
oDS = oUser.ListarComunas(Server.MapPath("./"));
if (oDS != null)
{
ddlComuna.DataSource = oDS.Tables[0].DefaultView;
ddlComuna.DataTextField = "comuna";
ddlComuna.DataValueField = "id_localidad";
ddlComuna.DataBind();
}

oDS = oUser.ListarSexo(Server.MapPath("./"));
if (oDS != null)
{
ddlSexo.DataSource = oDS.Tables[0].DefaultView;
ddlSexo.DataTextField = "sexo";
ddlSexo.DataValueField = "id_sexo";
ddlSexo.DataBind();
}
oDS = oUser.ListarEdad(Server.MapPath("./"));
if (oDS != null)
{
ddlEdad.DataSource = oDS.Tables[0].DefaultView;
ddlEdad.DataTextField = "id_edad";
ddlEdad.DataValueField = "id_edad";
ddlEdad.DataBind();
}

}
protected void bEnviar_Click(object sender, EventArgs e)
{
User oUser = new User();
oUser.Cod_persona = Convert.ToInt32(tRut.Text);
oUser.Dv_persona = tDigitoVerificador.Text;
oUser.Id_localidad = Convert.ToInt32(ddlComuna.SelectedValue);
oUser.Nombre = tNombres.Text;
oUser.Apellido = tApellidos.Text;
oUser.Id_sexo = Convert.ToInt32(ddlSexo.SelectedValue);
oUser.Telefono = tTelefono.Text;
oUser.Id_edad = Convert.ToInt32(ddlEdad.Text);
oUser.Direccion = tDireccion.Text;
oUser.Email = tEmail.Text;
oUser.PathBD = Server.MapPath("./");
if (oUser.InsertarUsuario() >= 1)
{
Response.Write("Ingresado");
}
else
Response.Write("No fue Ingresado");
}

}


y el codigo del USER.CS

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;

public class User
{
private int _cod_persona, _id_localidad, _id_sexo, _id_edad;
private string _dv_persona, _nombre, _apellido, _telefono, _direccion, _email;
private string _pathBD = "";

public string PathBD
{
get { return _pathBD; }
set { _pathBD = value; }
}

public string Email
{
get { return _email; }
set { _email = value; }
}

public string Direccion
{
get { return _direccion; }
set { _direccion = value; }
}

public string Telefono
{
get { return _telefono; }
set { _telefono = value; }
}

public string Apellido
{
get { return _apellido; }
set { _apellido = value; }
}

public string Nombre
{
get { return _nombre; }
set { _nombre = value; }
}

public string Dv_persona
{
get { return _dv_persona; }
set { _dv_persona = value; }
}


public int Id_edad
{
get { return _id_edad; }
set { _id_edad = value; }
}

public int Id_sexo
{
get { return _id_sexo; }
set { _id_sexo = value; }
}

public int Id_localidad
{
get { return _id_localidad; }
set { _id_localidad = value; }
}

public int Cod_persona
{
get { return _cod_persona; }
set { _cod_persona = value; }
}


public User()
{

}

public DataSet ListarComunas(string pathBD)
{

string cadena = "Data Source=DIEGOOFI\\SQLEXPRESS;Initial Catalog=User_PT;Integrated Security=True; Password=;";
SqlConnection oConexion = new SqlConnection(cadena);
SqlCommand oCommand = new SqlCommand();
SqlDataAdapter oAdapter = new SqlDataAdapter();
DataSet oDS = new DataSet();
try
{
oConexion.Open();
if (oConexion.State == ConnectionState.Open)
{
oCommand.Connection = oConexion;
oCommand.CommandType = CommandType.Text;
oCommand.CommandText = "SELECT id_localidad, comuna FROM pt_localidad ORDER BY comuna";
oAdapter.SelectCommand = oCommand;
oAdapter.Fill(oDS);


}

}

catch
{
oDS = null;
}
finally
{
if (oConexion.State == ConnectionState.Open)
{
oConexion.Close();
}
if (oConexion != null)
{
oConexion = null;
}
}
return oDS;
}

public DataSet ListarSexo(string pathBD)
{
string cadena = "Data Source=DIEGOOFI\\SQLEXPRESS;Initial Catalog=User_PT;Integrated Security=True; Password=;";
SqlConnection oConexion = new SqlConnection(cadena);
SqlCommand oCommand = new SqlCommand();
SqlDataAdapter oAdapter = new SqlDataAdapter();

DataSet oDS = new DataSet();

try
{
oConexion.Open();
if (oConexion.State == ConnectionState.Open)
{

oCommand.Connection = oConexion;
oCommand.CommandType = CommandType.Text;
oCommand.CommandText = "SELECT id_sexo, sexo FROM pt_sexo ORDER BY sexo";
oAdapter.SelectCommand = oCommand;
oAdapter.Fill(oDS);


}

}

catch
{
oDS = null;
}
finally
{
if (oConexion.State == ConnectionState.Open)
{
oConexion.Close();
}
if (oConexion != null)
{
oConexion = null;
}
}

return oDS;
}

public DataSet ListarEdad(string pathBD)
{
string cadena = "Data Source=DIEGOOFI\\SQLEXPRESS;Initial Catalog=User_PT;Integrated Security=True; Password=;";
SqlConnection oConexion = new SqlConnection(cadena);
SqlCommand oCommand = new SqlCommand();

SqlDataAdapter oAdapter = new SqlDataAdapter();

DataSet oDS = new DataSet();

try
{

oConexion.Open();
if (oConexion.State == ConnectionState.Open)
{
oCommand.Connection = oConexion;
oCommand.CommandType = CommandType.Text;
oCommand.CommandText = "SELECT id_edad FROM pt_edad ORDER BY id_edad";
oAdapter.SelectCommand = oCommand;
oAdapter.Fill(oDS);


}

}

catch
{
oDS = null;
}
finally
{
if (oConexion.State == ConnectionState.Open)
{
oConexion.Close();
}
if (oConexion != null)
{
oConexion = null;
}
}
return oDS;
}


public int InsertarUsuario()
{
int iStatus = 0;
SqlConnection oConexion = new SqlConnection();
SqlCommand oCommand = new SqlCommand();


try
{
oConexion.ConnectionString = "Provider=.NET Framework Data Provider for SQL Server;Data Source=" + _pathBD + "User_PT.dbo;User Id=;Password=;";
oConexion.Open();
if (oConexion.State == ConnectionState.Open)
{
string sSQLInsert = "INSERT INTO pt_user (cod_persona,dv_persona,id_localidad,nombre,apelli do,id_sexo,id_edad,telefono,direccion,email) VALUES";
sSQLInsert += "('" + _cod_persona + "','" + _dv_persona + "','" + _id_localidad + "','" + _nombre + "','" + _apellido + "','" + _id_sexo + "','" + _id_edad + "','" + _telefono + "','" + _direccion + "','" + _email + "')";
oCommand.Connection = oConexion;
oCommand.CommandType = CommandType.Text;
oCommand.CommandText = sSQLInsert;
iStatus = oCommand.ExecuteNonQuery();

}
}
catch
{
iStatus = 0;
}
finally
{
if (oConexion.State == ConnectionState.Open)
oConexion.Close();
if (oConexion != null)
oConexion = null;
}
return iStatus;
}
}
  #2 (permalink)  
Antiguo 05/10/2009, 16:19
Avatar de eledgarr  
Fecha de Ingreso: octubre-2008
Ubicación: La ciudad de la esperanza
Mensajes: 133
Antigüedad: 15 años, 7 meses
Puntos: 3
Respuesta: No Inserta Datos en Base de Datos

Pues asi como para decirte exactamente por que no puedo ya que implica conocer el diseño de tu BD etc. pero mire tu codigo y lo unico que te sugiero es checar en la función InsertarUsuario() que valor te regresa oCommand.ExecuteNonQuery(); para la linea


iStatus = oCommand.ExecuteNonQuery();

por que igual y da cero o nulo y por eso parece que no guarda nada.

Repito asi como para darte una respuesta seguro no puedo estar, supongo por eso nadie habia respondido, suerte
  #3 (permalink)  
Antiguo 06/10/2009, 05:29
Avatar de Valery-Net  
Fecha de Ingreso: agosto-2008
Mensajes: 694
Antigüedad: 15 años, 9 meses
Puntos: 12
Respuesta: No Inserta Datos en Base de Datos

Te muestra algún error ?
  #4 (permalink)  
Antiguo 06/10/2009, 10:10
Avatar de Porlachucha  
Fecha de Ingreso: noviembre-2008
Ubicación: Santiago
Mensajes: 172
Antigüedad: 15 años, 7 meses
Puntos: 5
Respuesta: No Inserta Datos en Base de Datos

te recomiendo que si estas usando sqlserver (eso creo) te crees un store procedure en la BD, a fin de encapsular tus llamadas a la BD
para poder ver si el problema es de tu BD o de tu codigo, ejecuta una aplicacion que se llama sqlserver profiles, ahi filtras los mensajes de acuerdo al usuario que estas usando psra conectarte a la bd.
ahi podras dreterminar si el problema esta en tu codigo C# o en la bd

salu2
plch
  #5 (permalink)  
Antiguo 06/10/2009, 11:21
 
Fecha de Ingreso: mayo-2008
Mensajes: 5
Antigüedad: 16 años
Puntos: 0
Respuesta: No Inserta Datos en Base de Datos

No no me tira ningún problema, solo no guarda, voy a checar lo de las bases de datos y les comento!

muchas gracias!
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 23:34.