0
votes

I'm new in ASP.NET and I have to make a project in ASP.NET Web Application in Visual Studio and database in Microsoft SQL Server for finishing my studies at university. I tried to transfer data from a TextBox to another TextBox, but in another page and this error appears everytime. I tried using method with Application[] and Session[], but it doesn't work.

Here is my error:

"System.NullReferenceException: 'Object reference not set to an instance of an object.'"

The code in the first page is :

SqlConnection conn = new SqlConnection("Data Source=LARI-LUCAS;Initial
    Catalog = Licenta_BD; Integrated Security = True");
SqlCommand cmd;           

try
{
    cmd = new SqlCommand("insert into Date_studenti_UPT (universitate,facultate,sectie,nume,prenume,gen,an,media,marca,numar) values(@universitate,@facultate,@sectie,@nume,@prenume,@gen,@an,@media,@marca,@numar) ", conn);
    cmd.Parameters.AddWithValue("@universitate", txtUniv.Text);
    cmd.Parameters.AddWithValue("@facultate", txtFac.Text.Trim());
    cmd.Parameters.AddWithValue("@sectie", txtSectie.Text.Trim());
    cmd.Parameters.AddWithValue("@nume", txtNume.Text.Trim());
    cmd.Parameters.AddWithValue("@prenume", txtPrenume.Text.Trim());
    cmd.Parameters.AddWithValue("@gen", txtGen.Text.Trim());
    cmd.Parameters.AddWithValue("@an", txtAn.Text.Trim());
    cmd.Parameters.AddWithValue("@media", txtMedie.Text.Trim());
    cmd.Parameters.AddWithValue("@marca", txtMarca.Text.Trim());
    cmd.Parameters.AddWithValue("@numar", txtNr.Text.Trim());

    int rowsAffected = cmd.ExecuteNonQuery();
    Session["Nume"] = txtNume.Text;
    Session["Prenume"] = txtPrenume.Text;
    Session["Gen"] = txtGen.Text;
    Session["An"] = txtAn.Text;
    Session["Media"] = txtMedie.Text;
    Session["Universitate"] = txtUniv.Text;

    if (rowsAffected == 1)
    {
        conn.Open();
        string url = "Profil.aspx";
        Response.Redirect(url);
    }
    else
        Label12.Text = "Eroare actualizare date utilizator!";
    }
    catch (Exception ex)
    {
        //log error
        Label12.Text = "Eroare la deschidere baza date " + ex.Message;
    }
    finally
    {
        conn.Close();
    }

And for the page 2 is:

protected void Page_Load(object sender, EventArgs e)
{
    txtA.Text = Session["An"].ToString();
    txtG.Text = Session["Gen"].ToString();
    txtM.Text = Session["Media"].ToString();
    txtN.Text = Session["Nume"].ToString();
    txtP.Text = Session["Prenume"].ToString();
    txtUn.Text = Session["Universitate"].ToString();
}
1
You really shouldn't be starting a new project of any kind in ASP .NET Webforms. It's been obsolete for years.Daniel Mann
It's a project in ASP.NET Web Application. :)Lari Ana-Maria
Does this help?Nikki9696
No, it doesn't, but that's it. I cannot do the app in something else..Lari Ana-Maria
So your config allows cookies and stuff? Response.Redirect sends a 302 to the browser, and generally uses cookies. But the cookies might not have been set so they get lost. That's why cookiesless set to true for that one.Nikki9696

1 Answers

0
votes

I'd use Server.Transfer to redirect to the second page, that way you can call your textbox from page 1 easily.