How to exit from a method manually? - c#

I have a query which returns a DataSet. After that I check whether records are available or not. If no records available I want to display error message, exit from the process and redirect to another page. Methods available in below the exiting method should not execute.
Here is the code I have so far.
ds = dba.loadEmpInfo(number, searchType, department);
string appNumber = "";
if (ds.Tables[0].Rows.Count > 0)
{
appNumber = ds.Tables[0].Rows[0]["Ref_no"].ToString();
workDS = dba.workExp(appNumber, searchType);
}
else
{
WebMsgBox.Show("No Record relevant to this app number are available");
}

If you want to show Alert message from code behind use below syntax,
ScriptManager.RegisterStartupScript(Control control, Type type,string key, string script,bool addScriptTags);
i.e, in the else part
{
ScriptManager.RegisterStartupScript(this, GetType(), "Alert",
"alert('No Record relevant to this app number are available!');", true);
// Now Re-direct to the next page as you wish
Response.Redirect("yourPage.aspx");
}

Related

C# Simple login interface with SQL data model

So i'm doing a course on C#, where i´ve been tasked to build an app for a fictive company.
In the app needs to be a login screen, i´ve been struggling with getting it to accept my correct login parameters (See code). Instead it says every entry is incorrect.
Can any of you spot the immediate issue with my code?
private void button1_Click(object sender, EventArgs e)
{
logindbEntities context = new logindbEntities();
if (textBox1.Text!=string.Empty || textBox2.Text!=string.Empty)
{
var user = context.AdminLogin.Where(a =>
a.Full_name.Equals(textBox1.Text)).First();
if (user != null)
{
if (user.Password.Equals(textBox2.Text))
{
success s1 = new success();
s1.Show();
}
else
{
MessageBox.Show("password is not correct");
}
}
else
{
MessageBox.Show("username is not registered");
}
}
else
{
MessageBox.Show("username & password are required");
}
EDIT: Have me excused please, i´ve tried to edit the post to suit the guidelines better.
Firstly, you may get the username and password into two variables from your form.
string username="";
string password="";
After you get the username and password from your form into the two variables, it's time to check it with the database.
var user = context.AdminLogin.Where(a => a.Full_Name.Equals(username)).First();
Then, check if the sequence does not contain any element, the user does not exist. You may use message box to display that it does not exists.
if(!user.Any()) // Show message here --user does not exist--
Then, if the user exist. You may check the password.
if(user.Password.Equals(password)) // Handle password here.

Contains Method doesnt work

Using the code below, I want to compare the UserType, if it is "Student" I want to redirect to student profile, etc.
But it always gets to the last else statement and Writes an Error.
The returnQuery method works well because it returns the value "Student".
String emailID = Session["New"].ToString();
string usertype = returnQuery(
"select userType from Registration where email = '" + lblEmail.Text + "'");
if (usertype.Contains("Student"))
{
Response.Redirect("Profile.aspx?email=" + emailID.ToString());
}
else if (usertype.Contains("Company"))
{
Response.Redirect("CompanyProfile.aspx?email=" + emailID.ToString());
}
else if(usertype.Contains("Admin"))
{
Response.Redirect("AdminProfile.aspx?email=" + emailID.ToString());
}
else
Response.Write("Error");
If usertype="Student" you can use usertype.Equals("Student"); to determine the variable.
Here is the method.
I suspect that you are dealing with a Page Lifecycle issue. lblEmail.Text is likely not what you expect it to be during Postback Event Handling time of the button click.
Verify that you are not modifying the contents of lblEmail.Text at any point before the button click event is processed, and verify the value of lblEmail.Text at the time the click event is processed.
For more information about Page Lifecycle, see this MSDN article:
https://msdn.microsoft.com/en-us/library/ms178472%28v=vs.100%29.aspx

Unity - Reading text off an echo'd php script into a string is not matching if statements

So my project as of now is almost working:
It connects to facebook
It takes the Facebook ID and sends it to a script/iterator to handle connection
That script then sends off a concatenated string to a php url, which correctly checks my dataTable for an entry, and if not creates an entry
After that the php code echo's a text reply: OldUser/NewUserCreated/NewUserFailed
And here's where my problem starts:
the aim is to get the text from the echo, then check that string, if it works then unity moves on to the next scene and goes on, however when checking the string it fails for some reason, before I explain further here's my iterator that handles the connection:
IEnumerator Connect()
{
//call databasequeries and get sDBConnect
string sConnectPhp = GameObject.FindGameObjectWithTag("DBConnector").GetComponent<DatabaseQueries>().sDBConnect;
//now ready the url with the necessary code for php's get, and the FacebookID
string url = sConnectPhp + "?UserID=" + sFacebookID;
WWW wwwGet = new WWW(url);
yield return wwwGet;
string sTemp = wwwGet.text.ToString();
if(wwwGet.error == null)
{
print (sTemp);
if(sTemp == "OldUser")
{
//if successfully connected set to true
print ("connectToDatabase: successful");
bDatabaseConnected = true;
}
else if(sTemp == "NewUserCreated")
{
//if successfully connected set to true
print ("connectToDatabase: successful");
bDatabaseConnected = true;
}
else if(sTemp == "NewUserFailed")
{
//game connection has failed
print ("connectToDatabase: failed");
bDatabaseConnected = false;
bConnectionFailed = true;
}
}
else
{
//game connection has failed
print ("connectToDatabase: failed");
bDatabaseConnected = false;
bConnectionFailed = true;
}
}
Now my problem is that sTemp is printing in the unity console: OldUser, and it's not matching my if else condition? I have tried copying wwwGet.text without ToString but that doesn't work either - printing shows OldUser, but it doesn't match the condition
My only guess is that I haven't used ToString correctly, but my searches have shown it being used for Decimal and currency conversion, and as said without toString sTemp is not matching, any ideas as to why it's not matching?

how to get cmos date/time asp.net [duplicate]

This question already has an answer here:
Get BIOS date in asp.net
(1 answer)
Closed 9 years ago.
i am developing an application in asp.net of which i want to create a DEMO version for 7 days.. i tried to make the demo stop in asp.net by checking for the current date and if its past 7 days then the application. wont start.
but the problem is the user changes the date from windows control panel then application. wont detect it and it will run.. i want take cmos date/time Without Online
int num=7;
int smonth=convert.toint32(Datetime.Now.Date.Tostring());
if ((num < smonth) || (num == smonth))
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Window", "alert('Please Update your Licence Key');", true);
}
else
{
if (crt_val == "0")
{
Session["UserId"] = txtusername.Text.Trim();
objuser.UserCode = txtusername.Text.Trim();
objuser.Password = s_hex_md5(txtpassword.Text.Trim());
string pwd = s_hex_md5(txtpassword.Text);
objuser.Ccode = ddlCode.SelectedValue;
objuser.Lcode = ddlLocation.SelectedValue;
DataTable dt = new DataTable();
dt = objdata.UserLogin(objuser);
if (dt.Rows.Count > 0)
{
if ((dt.Rows[0]["UserCode"].ToString().Trim() == txtusername.Text.Trim()) && (dt.Rows[0]["Password"].ToString().Trim() == pwd) && (dt.Rows[0]["CompCode"].ToString().Trim() == ddlCode.SelectedValue) && (dt.Rows[0]["LocationCode"].ToString().Trim() == ddlLocation.SelectedValue))
{
Session["Isadmin"] = dt.Rows[0]["IsAdmin"].ToString().Trim();
Session["Usernmdisplay"] = txtusername.Text.Trim();
Session["Ccode"] = dt.Rows[0]["CompCode"].ToString().Trim();
Session["Lcode"] = dt.Rows[0]["LocationCode"].ToString().Trim();
if (Session["Isadmin"].ToString() == "1")
{
Session["RoleCode"] = "1";
}
else
{
Session["RoleCode"] = "2";
}
Response.Redirect("Dashboard.aspx");
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Window", "alert('User Name and Password Wrong');", true);
}
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Window", "alert('Please Update your Licence Key');", true);
}
}
else
{ }
}
Anyone help me...
When user changes the time in control panel, the user IS changing the CMOS date/time.
You might want to have your application contact a time server online to get the correct date/time.
Excellent answer on how to do it here
https://stackoverflow.com/a/12150289/263003
DateTime structure in C# gives you the date and time details from the machine where the code is running. so if you want to give a demo application to the client you can deploy the application on the Server which is under your control. so that user does not have the control to change the date and time, even if the client changes date and time it wont effect the Application as the Application code is running on different machine.
DateTime.Now gives you the Current date and time of the Server
so i would suggest to maintain a dedicated server for deploying/maintaining the website for client.

Alert show when extension of FileUpLoads are not allowed

I have a asp FileUpload control in my aspx page.
And the code behind to check file's extension and then insert them into database:
private string Write(HttpPostedFile file, string table)
{
string fileNameMain = Path.GetFileName(file.FileName);
// check for the valid file extension
string fileExtension = Path.GetExtension(fileNameMain).ToLower();
if (fileExtension.Equals(".pdf") || fileExtension.Equals(".doc"))
{
...insert fileupload into database
}
else
{
LabelError.Text = "Extension of files are not allowed.";
return null;
}
}
With the code above, it checks the extension and then show the message "Extension of files are not allowed." in a LabelError if the fileupload is not allowed.
Now, I'm required to do the "check-extension" in the other way: at the moment the client click and choose FileUpload, an alert show "Extension of files are not allowed.".
I need a way to make an alert show at the momment the FileUpload choosed in browser. Help!!!!
Hi but if you want an Alert (JavaScript) but first you need an postback action to execute code vb and the alert for example you could use a button to execute this method. You can show alert with this code
string script = "alert('Extension of files are not allowed');";
ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", script , true);

Categories

Resources