I cannot pass more than one value in QueryString - asp.net

My ASP.NET application has stopped working after I migrated it to another server.
The problem is that I cannot send more than one value via the query string.
The URL I'm trying looks like this:
ThisIsSecondPage.aspx?Mode=Edit&ID=0001
I can capture the value of Mode in ThisIsSecondPage.aspx, but ID is blank.
I also tried to change ID to something line A0001, but it did not work.
I also tried:
ThisIsSecondPage.aspx?Mode=Edit<and>ID=0001
Can anyone help me please?

send querystring like this
ThisIsSecondPage.aspx?Mode=Edit&ID=0001
and recive in page_load event of ThisIsSecondPage page:
protected void Page_Load(object sender, EventArgs e)
{
string ModeParam = "";
string IDparam = "";
if(Request.Params["Mode"] !=null)
ModeParam = Request.Params["Mode"].ToString();
if (Request.Params["ID"] != null)
IDparam = Request.Params["ID"].ToString();
}

You are passing query string correctly.
ThisIsSecondPage.aspx?Mode=Edit&ID=0001
In second page, Use as below
For Mode
string ModeValue = Request.QueryString["Mode"];
or in short form,
string ModeValue = Request["Mode"];
For ID
string IDValue = Request.QueryString["ID"];
or in short form,
string IDValue = Request["ID"];

Related

Check at run time if primary key exists

I am working in asp.net. I have a textbox named formidtxt and another textbox is colortxt. Now what I want is that when a user enters an Form ID in formidtxt then at the same time it should start checking whether there already exists a form id with same ID that has been entered and if Form ID already exists in database then the color of colortxt textbox should change to red else it should be green.
I have an idea that it can be done by using events in text boxes but can't understand the working. My database is in SQL Server 2008.
Try this C# code;
private void Page_Load(object sender, EventArgs e)
{
// formidtxt is the name of the textbox
this.formidtxt.TextChanged += FormIDTextBox_TextChanged;
formidtxt.AutoPostBack = true;
}
Note that this method was written inside the Page_Load method.
TextChanged is an event and it occurs when the text is modified in a TextBox.
In this case, when the formidtxt (textbox) text changes, it will call the FormIDTextBox_TextChanged method.
private void FormIDTextBox_TextChanged(object sender, EventArgs e)
{
int x = 0;
// convert textbox text (string) to int
Int32.TryParse(formidtxt.Text, out x);
// call IsIDAvailableDAO method
// x is the converted int value
if (IsIDAvailableDAO(x))
{
colortxt.BackColor = System.Drawing.Color.Red;
}
else
{
colortxt.BackColor = System.Drawing.Color.Green;
}
}
This method will get the text from the textbox (formidtxt) and send it to the IsIDAvailableDAO method as a parameter.
Using the IsIDAvailableDAO method, we can check whether the ID is available in the database or not. If it is available, then the method will return a TRUE boolean value. If not, it will return a False boolean value.
According to that boolean value, you can change the color of the colortxt textbox as you want or do something else.
private Boolean IsIDAvailableDAO(int id)
{
Boolean output;
using (SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=Testing;Integrated Security=True"))
{
string query = #"SELECT CASE WHEN COUNT(ID) >= 1 THEN CAST( 1 as BIT ) ELSE CAST( 0 as BIT )
END As IsAvailable
FROM TableName
WHERE ID = #ID";
SqlCommand cmd = new SqlCommand(query, myConnection);
cmd.Parameters.AddWithValue("#ID", id);
myConnection.Open();
output = (Boolean)cmd.ExecuteScalar();
myConnection.Close();
}
return output;
}
In this method (IsIDAvailableDAO), Please change the query (TableName, ID, etc.) and connectionstring as appropriate.
You also has to add this namespace: using System.Data.SqlClient;
https://www.connectionstrings.com/sql-server-2008/
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/namespaces/using-namespaces

How to transfer data from one page to another page in asp.net, I dont want to use sessions

**How to transfer data from one page to another page in asp.net, I don't
want to use sessions **
You can use Query String & Cookies
Example for Query String :
Passing value..
private void Button1_Click(object sender, System.EventArgs e)
{
// Value sent using HttpResponse
Response.Redirect("Form1.aspx?Name="+txtName.Text);
}
Getting value using Query String..
if (Request.QueryString["Name"]!= null) // null checking
lbl_Name.Text = Request.QueryString["Name"];

failing to update private fields in Asp.net

I have a private field in the code behind file of Asp.net page that I'm trying to reset but for some reason it doesn't update. this is the field
private double interest = 0;
I then update this field in this method:
protected void DropDownListBanks_SelectedIndexChanged(object sender, EventArgs e)
{
MajesticEntities majesticentities = new MajesticEntities();
var query2 = from bank in
majesticentities.Banks
where bank.Name == DropDownListBanks.SelectedValue
select bank.Interest;
interest = query2.Single().Value;
// Label5.Text = interest.ToString();
// Label5.Text = fees.ToString() + "welcome";
}
When I try to use the field again in a code initiated by a button click I get the original value (0)
Can anyone please explain why this is happening ?
Thanks
You need to save them in an hidden field , textbox , label or in a view state.
ViewState["VSinterest"] = interest;
Then the value will be saved after postback
"after postback " private double interest = 0; " = 0"
Then if you need te value you take it from where you saved it and convert it to what it needs to be

Textbox using textmode password not showing text asp.net c#

I have a few buttons on a web form, and when the user clicks them they will update the the textbox. This worked till I added the textmode = password. Now the textbox doesn't show the text anymore. I debugged the app, and the text property is getting the value, but once again it is not showing.
Here is what I have tried:
protected void btn_punch_7_Click(object sender, EventArgs e)
{
const string string_punch_Number_7 = "7";
var text = txt_punch.Text;
text += string_punch_Number_7;
txt_punch.Text = text;
}
protected void btn_punch_8_Click(object sender, EventArgs e)
{
const string string_punch_Number_8 = "8";
var text = txt_punch.Text;
text += string_punch_Number_8;
txt_punch.Text = text;
}
I have also tired this:
public partial class WebForm3 : System.Web.UI.Page
{
public string string_punch;
protected void Page_Load(object sender, EventArgs e)
{
MultiView1.SetActiveView(View1);
txt_punch.Width = 300;
txt_punch.Height = 50;
txt_punch.MaxLength = 4;
txt_punch.Attributes.Add("OnChange", string_punch);
}
protected void btn_punch_7_Click(object sender, EventArgs e)
{
const string string_punch_Number_7 = "7";
string_punch = txt_punch.Text;
string_punch += string_punch_Number_7;
txt_punch.Text = string_punch;
}
protected void btn_punch_8_Click(object sender, EventArgs e)
{
const string string_punch_Number_8 = "8";
string_punch = txt_punch.Text;
string_punch += string_punch_Number_8;
txt_punch.Text = string_punch;
}
How desperate are you?
If you're not desperate enough to try anything, anything to get it to work, don't read on. This will not be nice. OK? OK.
The trick is to make the web app think that it's not a password box. In other words, don't use TextMode="password".
Then in Page_Load, put txt_punch.Attributes["type"] = "password"
That's it. The browser will know it's a password field (and show asterisks or dots), but the server side won't know, so it will send the content to the client as it it were plain text. Of course, this will also put the password in the page source...
When you set the TextMode property of a TextBox to Password the default behaviour is for it not to display the value of the Text property.
This is to prevent unmasked passwords being shown in the HTML source of the page.
One solution is to use an attribute to hold the password value:
TextBox.Attributes.Add("value", "yourPassword");
Source
Based off option one of Kola's answer you can try this:
TextBox.Attributes["value"] = "Whatever value goes here";
When you set property of textbox to TextMode="Password" then in edit mode textbox display blank. If you fill value like
TextBox.Text = "Password";
then not working and you need to enter password again to save form.
You can use Attributes to fill value in textbox like below code.
TextBox.Attributes["value"] = "your password value";
This is an asp.net BUG!! The email validation algorithm is very strict, resulting in the exclusion of some valid emails. If you switch the TextMode from "Password" tp "SingleLine", your code will work.
we cant directly assign values to password text box.
for assign values to text box password we should use textbox attributes
Textbox1.Attributes.Add("value",variable/"string");
Password generally are not meant to SHOW, but if you are intended to do so this page may help you Solution

Passing Value from textboxes in one webform to texboxes in another webform

am trying to get users to enter some details into a textbox in form1 and get the entry validated against the database. if the entry is correct, form2 loads with other texboxes including the one they made entries into. however i dont want them to make any changes to the textboxes they entered values into previously neither should they have to re-enter the values again.
how do i get the values in the textboxes to move from form1 to form2?
the code below shows what ive done with both forms but the second form dosent display the items in the textboxes when the form is loaded.
first form
protected void Button1_Click(object sender, EventArgs e)
{
string strConn;
strConn = "Provider=MIcrosoft.Jet.OLEDB.4.0;data Source=" +
Server.MapPath("App_Data/test.mdb");
OleDbConnection mDB = new OleDbConnection(strConn);
mDB.Open();
prodSnStr = pSnTextBox.Text;
purDate = Convert.ToDateTime(purDateTextBox.Text);
string dateStr = purDateTextBox.Text;
productClass aProduct = new productClass();
if (aProduct.Prods(mDB, prodSnStr, purDate))
{
Session["ProdSn"] = pSnTextBox.Text;
Session["PurDate"] = purDateTextBox.Text.ToString();
Response.Redirect("Warranty.aspx");
}
else
{
//error message
}
}
form two
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString["ProdSn"] != "")
{
pSNoTextBox.Text = Request.QueryString["ProdSn"];
if (Request.QueryString["PurDate"] != "")
{
dateTextBox.Text = Request.QueryString["PurDate"];
}
else
{
//error message to display
}
}
else
{
//error message to display
}
}
eagaerly waiting for your responses..thanks..
In your code you are putting the values on one page into the session:
Session["ProdSn"] = pSnTextBox.Text;
Session["PurDate"] = purDateTextBox.Text.ToString();
However you are trying to read them out on the 2nd page from the Request collection:
if (Request.QueryString["ProdSn"] != "")
{
pSNoTextBox.Text = Request.QueryString["ProdSn"];
if (Request.QueryString["PurDate"] != "")
{
dateTextBox.Text = Request.QueryString["PurDate"];
}
This makes no sense. If you want to use the session, you must also get the values back out from the session object.
Personally I would look into Cross Page postbacks and Server.Transfer combined with Page.PreviousPage. Just make sure you don't set preserveForm parameter to false if using Server.Transfer.
You aren't passing your values as a query string. If you were your Response.Redirect would look like this:
Response.Redirect("Warranty.aspx?ProdSn=something&PurDate=something");
Instead since you are saving these values in a Session variable try this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["ProdSn"] != "")
{
pSNoTextBox.Text = Session["ProdSn"];
if (Session["PurDate"] != "")
{
dateTextBox.Text = Session["PurDate"];
}
else
{
//error message to display
}
}
else
{
//error message to display
}
}
In the button_click of the first form i entered this code
Session["ProdSn"] = pSnTextBox.Text;
Session["PurDate"] = purDateTextBox.Text.ToString();
Response.Redirect("Warranty.aspx?ProdSn=" + Server.UrlEncode(pSnTextBox.Text) +
"&PurDate=" + Server.UrlEncode(purDateTextBox.Text));
and then in the Page_load event of the second form i did this..
string value = Request["ProdSn"];
string value1 = Request["PurDate"];
pSnTextBox.Text = value;
purDateTextBox.Text = value1;
no hassle sustained....easy and perfectly working....
thank for ya'11 helping....
am very grateful
your asp.net page must post your data to second page.
just set your buttons PostBackUrl attribute.
<asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="target.aspx" />
I do not understand while you are making things complex.
When users clicks the button all data will be send to your target page.

Resources