
05/10/2009, 09:43
|
| | Fecha de Ingreso: mayo-2008
Mensajes: 5
Antigüedad: 16 años, 8 meses 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;
}
} |