NeoVe
NeoVe

Reputation: 3897

DataBind into DB from Class in asp.net

I'm filling up a database from an aspx web application.

Everything works fine, except for the fact that I use multiple pages for users to fill in their data into DB.

So, i got this method in one class.cs file:

public class Botones
{
public void SaveCVInfo2(string varOne,string varTwo, string  varThree)
{
using (ConexionGeneralDataContext db = new ConexionGeneralDataContext())
{
    Usuario_Web columna = new Usuario_Web();
    //Add new values to each fields
    columna.Nombre = varOne;
    columna.Apellido = varTwo;
    columna.Em_solicitado = varThree;
    //and the rest where the textboxes would have been


    //Insert the new Customer object
    db.Usuario_Web.InsertOnSubmit(columna);
    //Sumbit changes to the database
    db.SubmitChanges();
 }

 }
 }

This is just a part of the file, it has more columns, but it doesn't change the example.

However, I added another class.cs file in order to reference it's method from a button in the second page. Just like the one I posted before in this post:

public class Botones2
{
    public void SaveCVInfo3(string varOne, string varTwo, string varThree, string varFour, string varFive, string varSix, string varSeven,
    string varEight, string varNine, string varTen, string varEleven, string varTwelve, string varThirteen, string varFourteen, string varFifteen)
    {
        using (ConexionGeneralDataContext db = new ConexionGeneralDataContext())
        {
            Usuario_Web columna = new Usuario_Web();
            //Insert the new Customer object
            columna.Estatus = 1;
            columna.nombre_esposo = varOne;
            columna.profe_compa = varTwo;
            columna.emp_compa = varThree;
            columna.cargo_actual_compa = varFour;
            columna.Hijos = varFive;
            columna.Edades_hijos = varSix;
            columna.persona_depende_compa = varSeven;
            columna.afinidades = varEight;
            columna.Edades_depende = varNine;
            columna.nom_padre = varTen;
            columna.profesion_padre = varEleven;
            columna.tel_padre = varTwelve;
            columna.nom_madre = varThirteen;
            columna.profesion_madre = varFourteen;
            columna.tel_madre = varFifteen;

            db.Usuario_Web.InsertOnSubmit(columna);
            //Sumbit changes to the database
            db.SubmitChanges();
        }
    }
}

AS you can see there are more columns I'm filling into the db, in the same table, problem is, when i submit data to the sql server, from second page, it just creates a new Usuario_web without the columns i referenced in first class.

What i need is to bind somehow the data already sent from first page. So first class is associated with all other classes, and columns are filled in the same row.

If anybody knows how to handle this situation, please let me know.

EDIT

This is how i call methods from asp buttons:

protected void Button1_Click(object sender, EventArgs e)
{
    Botones botones = new Botones();
    botones.SaveCVInfo2(nombre.Text, Apellidos.Text, EmpleoS.Text);
}

And SaveCVInfo3:

protected void Button1_Click(object sender, EventArgs e)
{
    Botones2 botones2 = new Botones2();
    botones2.SaveCVInfo3(nombre_compa.Text, Profesion_compa.Text, Emp_trabaja.Text, Cargo_compa.Text, RadioButtonList8.SelectedItem.Text, edades_sons.Text, num_depende.Text, Afinidad.Text, Edad_compa.Text, nom_padre.Text, profesion_compa_padre.Text, Tel_compa.Text, nom_madre.Text, profesion_compa_madre.Text, Tel_compa_madre.Text);
Response.Redirect("Envia3.aspx");
}

Upvotes: 0

Views: 106

Answers (1)

afzalulh
afzalulh

Reputation: 7943

CAUTION: Untested code below.

I would return the columna.ID from SaveCVInfo2:

public int  SaveCVInfo2(string varOne,string varTwo, string  varThree)
{
    int columnaId = 0;
    using (ConexionGeneralDataContext db = new ConexionGeneralDataContext())
    {
        Usuario_Web columna = new Usuario_Web();
        //Add new values to each fields
        columna.Nombre = varOne;
        columna.Apellido = varTwo;
        columna.Em_solicitado = varThree;
        //and the rest where the textboxes would have been    

        //Insert the new Customer object
        db.Usuario_Web.InsertOnSubmit(columna);
        //Sumbit changes to the database
        db.SubmitChanges();
        columnaId = columna.ID;
    }
    return columnaId;
}

And call the method, get the ID and save in the Session like this:

protected void Button1_Click(object sender, EventArgs e)
{
    int columnaId = 0;
    Botones botones = new Botones();
    columnaId = botones.SaveCVInfo2(nombre.Text, Apellidos.Text, EmpleoS.Text);
    Session["columnaId"] = columnaId.ToString();
}

Now when calling the SaveCVInfo3 method, I can pass the columnaId:

protected void Button1_Click(object sender, EventArgs e)
{
    int columnaId = 0;
    if(Session["columnaId"] != null && int.TryParse(Session["columnaId"].ToString(), out columnaId)
    {
        Botones2 botones2 = new Botones2();
        botones2.SaveCVInfo3(columnaId, nombre_compa.Text, Profesion_compa.Text, Emp_trabaja.Text, Cargo_compa.Text, RadioButtonList8.SelectedItem.Text, edades_sons.Text, num_depende.Text, Afinidad.Text, Edad_compa.Text, nom_padre.Text, profesion_compa_padre.Text, Tel_compa.Text, nom_madre.Text, profesion_compa_madre.Text, Tel_compa_madre.Text);
        Response.Redirect("Envia3.aspx");
    }
}

And in SaveCVInfo3 method I would get the object by Id, which is already saved in previous page and edit it, save it:

public void SaveCVInfo3(int columnaId, string varOne, string varTwo, string varThree, string varFour, string varFive, string varSix, string varSeven,
    string varEight, string varNine, string varTen, string varEleven, string varTwelve, string varThirteen, string varFourteen, string varFifteen)
{
    using (ConexionGeneralDataContext db = new ConexionGeneralDataContext())
    {
        //You will need to add reference to Linq if not added already
        //Usuario_Web columna = new Usuario_Web();
        //Insert the new Customer object

        Usuario_Web columna = (Usuario_Web)db.Usuario_Webs.Find(columnaId);
        columna.Estatus = 1;
        columna.nombre_esposo = varOne;
        columna.profe_compa = varTwo;
        columna.emp_compa = varThree;
        columna.cargo_actual_compa = varFour;
        columna.Hijos = varFive;
        columna.Edades_hijos = varSix;
        columna.persona_depende_compa = varSeven;
        columna.afinidades = varEight;
        columna.Edades_depende = varNine;
        columna.nom_padre = varTen;
        columna.profesion_padre = varEleven;
        columna.tel_padre = varTwelve;
        columna.nom_madre = varThirteen;
        columna.profesion_madre = varFourteen;
        columna.tel_madre = varFifteen;

        //db.Usuario_Web.InsertOnSubmit(columna);
        //Sumbit changes to the database
        db.SubmitChanges();
    }
}

EDIT If Id is not a primary key, you can change this part-

Usuario_Web columna = (Usuario_Web)db.Usuario_Webs.Find(columnaId);
to :

Usuario_Web columna =(Usuario_Web)db.Usuario_Web.Where(x=>x.ID == columnaId).FirstOrDefault()

Upvotes: 1

Related Questions