0

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");
}
1
  • Can you show how you are calling SaveCVInfo2 and SaveCVInfo3 from pages? Commented Oct 16, 2013 at 20:09

1 Answer 1

1

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()

Sign up to request clarification or add additional context in comments.

16 Comments

Thank you!! I'll test as soon as I get into the pc, btw, i can extend this to some other pages right? I mean i can add 'SaveCVInfo' 4, 5, 6, etc... with "Session["columnaId"] = columnaId.ToString();" and last one (9) with 'SaveCVInfo3' example call from button you gave me, right? Only difference it will be called 'SaveCVInfo9'.
Yes. You have to get the session value in Button's event method and pass to the class methods.
It throws me these 2 errors: "Error 1 The best overloaded method match for 'int.TryParse (string, out int)' has some invalid arguments C: \ Users \ Kristian \ Documents \ zulcon \ Group Zulcon \ Group Zulcon \ Envia2.aspx.cs 29 38 Group Zulcon Error 2 Argument 1: can not convert from 'method group' to 'string'. C: \ Users \ Kristian \ Documents \ zulcon \ Group Zulcon \ Group Zulcon \ Envia2.aspx.cs 29 51 Group Zulcon"
They both on ' if(Session["columnaId"] != null && int.TryParse(Session["columnaId"].ToString, out columnaId) '
I have edited my answer. 1. in FInd change db.Usuario_Web to db.Usuario_Webs. If you still have error, try the other code with Where.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.