Answered by:
which condition applied in pageload where page load without query string?

Question
-
User-1026236167 posted
hello
i have two webpages in 1st page when click on button then data fill to 2nd page
in second page (pageload) with query string page is run but without query string independent page not run
which condition apply on pageload where independent page run or with query string?
1st page css
button coding
protected void BT_submit11_Click(object sender, EventArgs e)
{
Button btn1 = (Button)sender;
GridViewRow row = (GridViewRow)btn1.Parent.Parent;
Label bn2 = (Label)row.FindControl("bn2");
Label bn1 = (Label)row.FindControl("bn1");
Response.Redirect("webform49.aspx?Benefited_leaves1=" + bn2.Text + "&Basic_allowance1=" + bn1.Text);
}2nd page css
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;namespace WebApplication14
{
public partial class WebForm49 : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
string connection = System.Configuration.ConfigurationManager.AppSettings["con"].ToString();
public void EstablishConnection(string storeprocedure)
{
con.ConnectionString = connection;
cmd.Connection = con;
cmd.Connection.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = storeprocedure;
}public void CloseConnection()
{
cmd.Connection.Close();
cmd.Connection.Dispose();
con.Close();
}protected void Page_Load(object sender, EventArgs e)
{if (!IsPostBack)
{
txt3.Text = Request.QueryString["Benefited_leaves1"].ToString();
txt2.Text = Request.QueryString["Basic_allowance1"].ToString();
}
}protected void BT_submit_Click(object sender, EventArgs e)
{
//string str = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
////SqlConnection cn = new SqlConnection(str);
//SqlConnection con = new SqlConnection(str);
//SqlCommand cmd = new SqlCommand("Insert into mann(Benefited_leaves1,Basic_allowance1)values(@benefited_leaves1,@basic_allowance1) ", con);
//con.Open();EstablishConnection("Users_InsertUser");
cmd.Parameters.Add("@benefited_leaves1", SqlDbType.Decimal, 30).Value = txt3.Text;
cmd.Parameters.Add("@basic_allowance1", SqlDbType.Decimal, 30).Value = txt2.Text;
// cmd.Parameters.Add("@tbl_id", SqlDbType.Decimal, 30).Value = TB_zipcode.Text;
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Record Add Sucessfully');", true);
try { cmd.ExecuteNonQuery(); }
catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); }//con.Close();
CloseConnection();}
}
}Wednesday, April 15, 2020 9:09 AM
Answers
-
User1535942433 posted
Hi prabhjot1313,
Accroding to your description,as far as I think,you could check whether query string is null.You could use try,catch.
More details,you could refer to below code:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { try { if (Request.QueryString["Benefited_leaves1"].ToString() != null && Request.QueryString["Basic_allowance1"].ToString() != null) { txt3.Text = Request.QueryString["Benefited_leaves1"].ToString(); txt2.Text = Request.QueryString["Basic_allowance1"].ToString(); } } catch { ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('textnull');", true); } } } protected void BT_submit_Click(object sender, EventArgs e) { string str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString; SqlConnection cn = new SqlConnection(str); SqlConnection con = new SqlConnection(str); SqlCommand cmd = new SqlCommand("Insert into tbl_gen_salary(Benefited_leaves1,Basic_allowance1)values(@benefited_leaves1,@basic_allowance1) ", con); con.Open(); //cmd.Parameters.AddWithValue("@studname", "%" + studname + "%"); cmd.Parameters.Add("@benefited_leaves1", SqlDbType.Decimal, 30).Value = txt3.Text; cmd.Parameters.Add("@basic_allowance1", SqlDbType.Decimal, 30).Value = txt2.Text; ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Record Add Sucessfully');", true); try { cmd.ExecuteNonQuery(); } catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); } con.Close(); }
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 16, 2020 5:31 AM -
User475983607 posted
sir my question is how webpage contains query string run webpage without query string and with query string which condition applied
error occur object reference not set to an instance when i run webpage without Query string
Take advantage of the null-coalesce operator if you are using C# 8.0 and later.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator
if (!IsPostBack) { txt3.Text = Request.QueryString["Benefited_leaves1"] ?? string.Empty ; txt2.Text = Request.QueryString["Basic_allowance1"] ?? string.Empty; }
Otherwise write an 'if" statement to check of the querystring is empty.
if (!IsPostBack) { if(Request.QueryString["Benefited_leaves1"] != null) { txt3.Text = Request.QueryString["Benefited_leaves1"]; } if (Request.QueryString["Basic_allowance1"] != null) { txt2.Text = Request.QueryString["Basic_allowance1"]; } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, April 17, 2020 11:12 AM
All replies
-
User1535942433 posted
Hi prabhjot1313,
Accroding to your description,as far as I think,you could check whether query string is null.You could use try,catch.
More details,you could refer to below code:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { try { if (Request.QueryString["Benefited_leaves1"].ToString() != null && Request.QueryString["Basic_allowance1"].ToString() != null) { txt3.Text = Request.QueryString["Benefited_leaves1"].ToString(); txt2.Text = Request.QueryString["Basic_allowance1"].ToString(); } } catch { ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('textnull');", true); } } } protected void BT_submit_Click(object sender, EventArgs e) { string str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString; SqlConnection cn = new SqlConnection(str); SqlConnection con = new SqlConnection(str); SqlCommand cmd = new SqlCommand("Insert into tbl_gen_salary(Benefited_leaves1,Basic_allowance1)values(@benefited_leaves1,@basic_allowance1) ", con); con.Open(); //cmd.Parameters.AddWithValue("@studname", "%" + studname + "%"); cmd.Parameters.Add("@benefited_leaves1", SqlDbType.Decimal, 30).Value = txt3.Text; cmd.Parameters.Add("@basic_allowance1", SqlDbType.Decimal, 30).Value = txt2.Text; ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Record Add Sucessfully');", true); try { cmd.ExecuteNonQuery(); } catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); } con.Close(); }
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 16, 2020 5:31 AM -
User-1026236167 posted
sir my question is how webpage contains query string run webpage without query string and with query string which condition applied
error occur object reference not set to an instance when i run webpage without Query string
Friday, April 17, 2020 10:50 AM -
User475983607 posted
sir my question is how webpage contains query string run webpage without query string and with query string which condition applied
error occur object reference not set to an instance when i run webpage without Query string
Take advantage of the null-coalesce operator if you are using C# 8.0 and later.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator
if (!IsPostBack) { txt3.Text = Request.QueryString["Benefited_leaves1"] ?? string.Empty ; txt2.Text = Request.QueryString["Basic_allowance1"] ?? string.Empty; }
Otherwise write an 'if" statement to check of the querystring is empty.
if (!IsPostBack) { if(Request.QueryString["Benefited_leaves1"] != null) { txt3.Text = Request.QueryString["Benefited_leaves1"]; } if (Request.QueryString["Basic_allowance1"] != null) { txt2.Text = Request.QueryString["Basic_allowance1"]; } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, April 17, 2020 11:12 AM