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