how to remove NullExceptionError from Session Variable - sql

I have a varible sem,which takes inputfrom the textbox but if the textbox is kept empty i want it to return "Please Enter a Semester"
int sem;
int parsevalue;
if (int.TryParse(TextBox2.Text, out parsevalue))
{
sem = parsevalue;
Session["Sem"] = sem;
}
else
{
Literal2.Text = "Please Enter a Semester";
}
But If the Textbox is empty the Session["Sem"] returns NullExceptionError in the .aspx file where i have used it.
I searched for the proper conversion using tryparse but could not understand it clearly as to how to print the above mentioned error message.
please help
Thank you in advance

The problem here is that you're assigning the Session variable only when there is a correct value, but you're always trying to access it. This will fail if the value is incorrect (and the Session variable is not set).
Here's your corrected code:
int sem;
int parsevalue;
if (int.TryParse(TextBox2.Text, out parsevalue))
{
sem = parsevalue;
}
else
{
Literal2.Text = "Please Enter a Semester";
}
//Always set the Session variable when it's used somewhere else
Session["Sem"] = sem;

Try:
HttpContext.Current.Session["Sem"] = sem;
Actually:
Session.Add("Sem", sem);
and
Session["Sem"] = sem;
is a same

Q1 Hi , I have a varible sem,which takes inputfrom the textbox but if the textbox is kept empty i want it to return "Please Enter a Semester"
int sem;
int parsevalue;
var txt = TextBox2.Text
if (!String.IsNullOrEmpty(text) && int.TryParse(text, out parsevalue))
{
sem = parsevalue;
}
else
{
Literal2.Text = "Please Enter a Semester";
}
Session["Sem"] = sem;

Related

How can i close window and relaod in asp.net

My code look like below.
protected void btnApprove_Click(object sender, EventArgs e)
{
if (id == "Button2")
{
string sql = #"update AdjTranSeqDetails set Approver1Status = 'Rejected',finalStatus='Rejected' ,updatedtime = GETDATE(),Approver1Comment ='{1}',modifieduser = '{2}' where TranSeq ={0}";
sql = String.Format(sql, tbxTranSeq.Text, TextBox2.Text, Master.CurrentUser);
string sql2 = #"INSERT INTO AdjTranSeqDetailsHistory SELECT * FROM AdjTranSeqDetails WHERE tranSeq={0}";
sql2 = String.Format(sql2, tbxTranSeq.Text);
string sql3 = #"delete from AdjTranSeqDetails where tranSeq={0}";
sql3 = String.Format(sql3, tbxTranSeq.Text);
if (dba.SqlUpdate(sql, ref ex, ref rowCt) || dba.SqlInsert(sql2, ref ex, ref rowCt) ||dba.SqlDelete(sql3, ref ex, ref rowCt))
{
string sscript = "<script language=\"Jscript\" > ";
if (Master.PageMode == "View")
{
sscript += "if (window.opener && !window.opener.closed) ";
sscript += "{window.opener.DoSearch();}";
}
sscript += "window.opener='x';self.close();";
sscript += "</Script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "SelfClose", sscript);
}
}
updateapp();
GetBillTranInfo();
}
Like Button2, like have 10 button. If one if condition function done I need to close the window and need to reload the page. Can I do like this?
I have tried like above but that is not working for me.
In my user controls, after updating data I do:
Response.Redirect(Request.RawUrl);
That ensures that the page is reloaded, and it works fine from a user control. You use RawURL and not Request.Url.AbsoluteUri to preserve any GET parameters that may be included in the request.
You probably don't want to use: __doPostBack, since many aspx pages behave differently when doing a postback.

How to perform click on a string value?

I want to click a variable value which is stored in a string and below is my code.
OrderConfirmationData orderConfirmationData= (OrderConfirmationData)Serenity.sessionVariableCalled("OrderConfirmationData");
int maxtries=0;
System.out.println("Order is available in NTI" + EfloristConstants.lastname.get());
List<WebElement> recordcount = getDriver().findElements(By.xpath("//table[contains(#id,'ctrlDWController1_DWDetails1_gvNTI')]/*/tr[not(contains(#style,'bold'))]"));
int j=16;
for(int i=1;i<=recordcount.size();i++) {
j=j*i;
String record = "(//table[contains(#id,'ctrlDWController1_DWDetails1_gvNTI')]/*/tr[not(contains(#style,'bold'))]/td)["+j+"]";
String recipientname= element(By.xpath(record)).getTextValue();
if(recipientname.equals(orderConfirmationData.getShipFirstName() != null)) {
System.out.println("Order is available ");
}
j=16;
}
want to click on the recipientname when the, if the condition matches i.e want to perform click inside the If condition.
Add the below line in if condition:
element(By.xpath(record)).click()

Java: How edit button positions in JOptionPane.showInputDialog?

How do you edit the button positions in the JOptionPane.showInputDialog pop up?
Are there arguments that you can put into the parameters?
String inputValue = JOptionPane.showInputDialog(frame.getFrame(), "Please enter a value from 1 to 9");
if (inputValue == null) { // Cancel button
return 0;
}
Apparently I just had to add extra arguments to make it a plain message, but how do you reposition the buttons?
String inputValue = JOptionPane.showInputDialog(frame.getFrame(), "Please enter a value from 1 to 9","", JOptionPane.PLAIN_MESSAGE);

DialogResult Infinite Loop

So I have this project at school to create a game "Guess the number". I am trying to loop a DialogResult using a switch statement and a while loop. I tried many possibilities but I go into a infinite loop. Just to know I am a beginner.I would really need some help here if possible. This is my code. Thanks in advance.
private void btnStartTheGame_Click(object sender, EventArgs e)
{
int guessTheNumber = Convert.ToInt32(txtNumberGuess.Text);
DialogResult dialogResult = MessageBox.Show("Is number" + number.ToString() + " you are thinking about?", "Answer the question!", MessageBoxButtons.YesNo);
switch(dialogResult)
{
case DialogResult.No:
while (dialogResult == DialogResult.No)
{
Random newNumberGenerator = new Random();
number = newNumberGenerator.Next(0, 101);
MessageBox.Show("Is number" + number.ToString() + " you are thinking about?", "Answer the question!", MessageBoxButtons.YesNo);
}
break;
case DialogResult.Yes:
if (dialogResult == DialogResult.Yes)
{
MessageBox.Show("Congratulation! You guessed the number!!");
break;
}
break;
}
You need to load the new result of the dialog.
int guessTheNumber = Convert.ToInt32(txtNumberGuess.Text);
DialogResult dialogResult;
do
{
Random newNumberGenerator = new Random();
number = newNumberGenerator.Next(0, 101);
dialogResult = MessageBox.Show("Is number" + number.ToString() + " you are thinking about?", "Answer the question!", MessageBoxButtons.YesNo);
} while (dialogResult == DialogResult.No);
MessageBox.Show("Congratulation! You guessed the number!!");
The do loop will execute the code first, and then check the condition. THis will also prevent having the same piece of code at different places, meaning the same.

JOptionPane input problems

Ok, so I understand that if you want to test what the user clicks in a JOptionPane, you would do something like this:
final int option = JOptionPane.showConfirmDialog(null, "That file name does not exist.", "File Name", JOptionPane.OK_CANCEL_OPTION);
if(option == JOptionPane.CANCEL_OPTION)
{
g++;
}
However, what if I wanted to set a String = to a JOptionPane like this?
String fileName = JOptionPane.showInputDialog("File Name") + ".txt";
How am I supposed to compare what the user clicked then?
you can use as you mentioned
String fileName = JOptionPane.showInputDialog("File Name");
Default input dialog has OK button and cancel buttons. Therefore if you press cancel button
your string value will be null
Otherwise it is the value you entered on textfield
so you can have a if(fileName == null)
to check whether user clicked cancel or OK button
String fileName = JOptionPane.showInputDialog("Enter file name");
if (fileName == null) {
System.out.println("User pressed cancel");
//your logic
}else{
System.out.println(fileName);
//your logic
}
When you're using showInputDialog() you want to get user input from a textfield, not depend on what the user clicked.
You can use either a JOptionPane with an input field
String s = JOptionPane.showInputDialog(null, "Enter a message:);
Or you can use a combobox style JOptionPane
Object[] possibilities = {"ham", "spam", "yam"};
String s = (String)JOptionPane.showInputDialog(
null,
"Complete the sentence:\n"
+ "\"Green eggs and...\"",
"Customized Dialog",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
"ham");
Edit:
String s = JOptionPane.showInputDialog(null, "Enter a message:);
if (s != null){ // OK is pressed with a value in the field
// do something with s
}