Hi, I have a project in ASP .NET with C#. I have the following code in my webform1 to pass the value that the user writes to another webform and compare it in an SQL query and it throws me the error:
'Object reference not set to an instance of a get object(refers to webform1's get) returned null.
WebForm1 code:
public string semana
{
get
{
return sem.Text; //textbox donde el usuario va a escribir el valor
}
}
protected void button_Click(object sender, EventArgs e)
{
if (semana!= null)
{
Response.Redirect("webform2.aspx?semana=" + semana);
}
else {
Response.Redirect("webform1.aspx");
}
}
}
Webform2 code (here it throws me the error)
int valor = Int32.Parse(PreviousPage.semana);
//aqui esta el valor del webform1 y lo convierto a int
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * from tabla where semana="+valor+",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
gridview1.Visible = true;
gridviwe1.DataSource = dt;
gridview1.DataBind();
con.Close();
}
}
I have relied on these pages to pass the value of the webform to the other:
https://msdn.microsoft.com/es-mx/library/6c3yckfw(v=vs.100).aspx https://social.msdn.microsoft.com/Forums/es-ES/af012424-6a31-4273- b9e7-f8ed6b8c233d/passing-data-from-one-web-form-to-another-web-fom-aspnet-with-c?forum=netfxwebes
I hope you can help me.
Thanks. Cheers
you are sending the variable as a parameter in your url to get it in webform2 you must use
Request.QueryString
the following wayAssuming that the week variable is of integer type, it will be assigned the value of said variable, otherwise it will remain 0.