how to do i change the value of data from server side? - c#

i am trying to set a value from server side in this code. i have a textbox to enter ticket number and when the ticket is validated and activated, i want the used property of this particulare ticket to be changed to true.
i have this code :
TicketBLL Tickets = new TicketBLL();
ClientDeviceBLL Devices = new ClientDeviceBLL();
if (String.IsNullOrEmpty(txtTicket.Text))
{
CVUsed.Visible = false;
CVUsed.Enabled = false;
CVMember.Enabled = false;
CVMember.Visible = false;
CVInValid.Enabled = false;
CVInValid.Visible = false;
lblMessages.Text = MessageFormatter.GetFormattedErrorMessage("You can login using a Ticket Number.");
txtTicket.Focus();
}
else
{
Ticket = Tickets.GetTicketByTicketNumber(txtTicket.Text);
////// we must enter the value of the correct SN and the Client ID
Device = Devices.GetClientDeviceBySN(txtSN.Text , Convert.ToInt32(txtClientID.Text));
if (Ticket != null)
{
//Correct Ticket number
CVInValid.Visible = false;
CVInValid.Enabled = false;
if (Ticket.Used == true)
{
//ticket was used, internet forbidden
CVUsed.Visible = true;
CVUsed.Enabled = true;
CVMember.Enabled = false;
CVMember.Visible = false;
CVUsed.IsValid = false;
}
else
{
//if exists but not used, Ticket accepted
//check if device is a member if client divices
if (Device != null)
{
//internet access garanteed
CVUsed.Visible = false;
CVUsed.Enabled = false;
CVMember.Enabled = false;
CVMember.Visible = false;
CVUsed.IsValid = true;
CVMember.IsValid = true;
//here is my error.
//ticket.used is not changing in the database so the next
//time he enters the same ticket number it would go through
//again.
Ticket.Used = true;
Response.Redirect("http://www.google.com");
}
else
{
//device not member, internet access forbidden
CVMember.Enabled = true;
CVMember.Visible = true;
CVUsed.Visible = false;
CVUsed.Enabled = false;
CVUsed.IsValid = true;
CVMember.IsValid = false;
}
}
}
else
{
//Ticket Number is not valid
CVUsed.Visible = false;
CVUsed.Enabled = false;
CVMember.Enabled = false;
CVMember.Visible = false;
CVInValid.Enabled = true;
CVInValid.Visible = true;
CVInValid.IsValid = false;
}
}
how can i automatically update the ticket.used value in the database?!

Without making a connection to database you cannot update the value of ticket.used. If you want to know how to connect to database and update, use stored procedures of direct queries to do your work. Take a look at this.

Related

BLToolKIT to Entity Framework help needed

I am creating a method for checking the product key. everything working fine in bltoolkit the code is
private void CheckKey()
{
try
{
using (DbManager db = new DbManager())
{
DataTable dt = db
.SetCommand("SELECT TOP 1 * FROM TblReg WHERE ProductKey=#ProductKey",
db.Parameter("#ProductKey", CommanClass.strRegkey))
.ExecuteDataTable();
if (dt.Rows.Count == 0)
{
GetSoftKey = false;
strSoftKey = null;
}
else
{
strSoftKey = dt.Rows[0].Field<string>("ProductKey");
GetSoftKey = true;
}
}
if ((GetSoftKey == true) && (strSoftKey != null))
{
if (strSoftKey == CommanClass.strRegkey)
{
SoftwareKey = true;
}
else
{
SoftwareKey = false;
}
}
}
catch (Exception)
{
SoftwareKey = false;
}
}
Now in When I try to write a method using entity framework for checking product key, it's confusing me at how to pass DataTable variable DataTable dt = login into entity context and set entity query parameter login.Parameter("#ProductKey", CommanClass.strRegkey), the code is
private void CheckKey()
{
try
{
using (loginEntities login = new loginEntities())
{
var pKey= from pk in login.tblSoftRegs
where pk.ProductKey == pk.ProductKey
select pk.ProductKey.FirstOrDefault();
if (pKey.Count() == 0)
{
GetSoftKey = false;
strSoftKey = null;
}
else
{
strSoftKey = ("ProductKey");
GetSoftKey = true;
}
}
if ((GetSoftKey == true) && (strSoftKey != null))
{
if (strSoftKey == CommanClass.busRegkey)
{
SoftwareKey = true;
}
else
{
SoftwareKey = false;
}
}
}
catch (Exception)
{
SoftwareKey = false;
}
}
Waiting for community contribution...
You've almost got it. FirstOrDefault will return null if there are none found, and you bind local variables directly into your LINQ query. Like this:
var pKey= from pk in login.tblSoftRegs
where pk.ProductKey == CommanClass.strRegkey
select pk.ProductKey.FirstOrDefault();
if (pKey == null)
{
GetSoftKey = false;
strSoftKey = null;
}
else
{
strSoftKey = ("ProductKey");
GetSoftKey = true;
}

How can I search Notes by sequence number

I use Evernote C# API. I understand how filters working.
ENNoteStoreClient store = ENSessionAdvanced.SharedSession.PrimaryNoteStore;
SyncState currentState = store.GetSyncState();
int currentUpdateCount = currentState.UpdateCount;
if (currentUpdateCount > latestUpdateCount)
{
latestUpdateCount = currentUpdateCount;
// Here synchronization code
}
I have latestUpdateCount and how I can get notes with sequence number >= this number?
You'll want to use GetFilteredSyncChunk using code something like the following:
List<SyncChunk> syncBlocks = new List<SyncChunk>();
int maxEntries = 250;
// These are sample filter settings; you should use what's appropriate for you based on
// what data you want returned in your note objects.
SyncChunkFilter filter = new SyncChunkFilter();
filter.IncludeNotes = true;
filter.IncludeNoteAttributes = true;
filter.IncludeNotebooks = true;
filter.IncludeTags = false;
filter.IncludeSearches = false;
filter.IncludeResources = false;
filter.IncludeLinkedNotebooks = false;
filter.IncludeExpunged = false;
filter.IncludeNoteApplicationDataFullMap = false;
filter.IncludeResourceApplicationDataFullMap = false;
filter.IncludeNoteResourceApplicationDataFullMap = false;
do
{
SyncChunk chunk = store.getFilteredSyncChunk(currentUpdateCount, maxEntries, filter);
if (chunk == null)
{
return null; // This can happen if there is a "503 - Service unavailable" error accessing this person's Evernote info
}
syncBlocks.Add(chunk);
currentUpdateCount = chunk.ChunkHighUSN;
} while (currentUpdateCount < serverSyncState.UpdateCount);
foreach (SyncChunk chunk in syncBlocks)
{
if (chunk != null && chunk.Notes != null)
{
foreach (Note chunkNote in chunk.Notes)
{
// do something with the retrieved chunkNote object
}
}
}

Setting Excel worksheet protection with EPPlus

I'm trying to set worksheet permissions for an XLSM file using EPPlus but it seems I can only set the default protection level, individual protections are not being set. For the record, I'm trying to accomplish programmatically method 1 in this article. Here's the code I'm using:
using (var p = new ExcelPackage("output.xlsm"))
{
var ws = p.Workbook.Worksheets["MySheet"];
// Set some cell values here
// Filtering, sorting, protection
ws.Cells[7, 1, 10, 5].AutoFilter = true;
ws.View.FreezePanes(7, 1);
ws.ProtectedRanges.Add("FilteredCells", new ExcelAddress(7, 1, 10, 5));
// Worksheet protection
ws.Protection.AllowAutoFilter = true;
ws.Protection.AllowDeleteColumns = false;
ws.Protection.AllowDeleteRows = false;
ws.Protection.AllowEditObject = false;
ws.Protection.AllowEditScenarios = false;
ws.Protection.AllowFormatCells = false;
ws.Protection.AllowFormatColumns = false;
ws.Protection.AllowFormatRows = false;
ws.Protection.AllowInsertColumns = false;
ws.Protection.AllowInsertHyperlinks = false;
ws.Protection.AllowInsertRows = false;
ws.Protection.AllowPivotTables = false;
ws.Protection.AllowSelectLockedCells = false;
ws.Protection.AllowSelectUnlockedCells = true;
ws.Protection.AllowSort = true;
ws.Protection.IsProtected = true;
ws.Protection.SetPassword("hunter2");
p.SaveAs(new FileInfo("output.xlsm"));
}
This runs without errors, but when I open the file in Excel, or load it back into EPPlus, I find that different protection options have been applied:
AllowAutoFilter = false
AllowDeleteColumns = false
AllowDeleteRows = false
AllowEditObject = true
AllowEditScenarios = true
AllowFormatCells = false
AllowFormatColumns = false
AllowFormatRows = false
AllowInsertColumns = false
AllowInsertHyperlinks = false
AllowInsertRows = false
AllowPivotTables = false
AllowSelectLockedCells = true
AllowSelectUnlockedCells = true
AllowSort = false
IsProtected = true
These obviously aren't the permissions I set before, so how can I make sure they are set correctly? Everything else is saved correctly.
Here is the source:
https://github.com/pruiz/EPPlus/blob/master/EPPlus/ExcelSheetProtection.cs
Setting the IsProtected property is overwriting your choices:
public bool IsProtected
{
get
{
return GetXmlNodeBool(_isProtectedPath, false);
}
set
{
SetXmlNodeBool(_isProtectedPath, value, false);
if (value)
{
AllowEditObject = true;
AllowEditScenarios = true;
}
else
{
DeleteAllNode(_isProtectedPath); //delete the whole sheetprotection node
}
}
}
Move your IsProtected = true call to the start of the code or handle however you want, but you are accidently overriding your previous choice. I would look at the properties at that link to see which ones are going to override your existing selections.

Asp.net visible button when 2 parameters match

Hello I want to make a button visibile when UserId stored in database match with current UserId.
string clientId = Context.User.Identity.GetUserId();
JobDescriptions job = new JobDescriptions();
if (job.PostedBy == clientId)
{
Button2.Visible = true;
else
{
Button2.Visible = false;
}
PostedBy is the Id of the user who posted on website saved on jobs table. Problem is that button is not visibile when my statement should work.
The solution
if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
{
int id = Convert.ToInt32(Request.QueryString["id"]);
JobReqModel model = new JobReqModel();
JobDescriptions job = model.GetJob(id);
string clientId = Context.User.Identity.GetUserId();
if (job.PostedBy == clientId)
{
Button2.Visible = true;
}
else
{
Button2.Visible = false;
}
}

How to recheck field A from field B by using TextChanged?

I am having a little problem here. Here is my situation:
I am typing user name: tester (valid user name; avoiding all checks) and then type password: testerr (valid password; avoiding all checks). Problem is that I am checking for the same inputs. And in my code when both inputs are the same I will see a notification. Now, when I type tester as user name and password I get the error, but when I add additional character to my password 'testerr' I am making password valid, but user name is checked as invalid saying that both are still the same and making my validation impossible.
How can avoid this? I was thinking of rechecking user name field from field 2, but I'm not sure how.
bool passIsValid, userIsValid;
public AuthenticationWindow()
{
InitializeComponent();
// Creating TextChanged events which till validate input fields.
txtUserName.TextChanged += new EventHandler(txtUserName_TextChanged);
txtPassword.TextChanged += new EventHandler(txtPassword_TextChanged);
}
private void txtUserName_TextChanged(object sender, EventArgs e)
{
// Checking for empty user name field.
if (string.IsNullOrEmpty(txtUserName.Text))
{
lblMessageUser.Text = "User Name field cannot be empty!";
lblMessageUser.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
}
// Making sure that user name is at least 6 characters long.
else if (txtUserName.Text.Length < 6)
{
lblMessageUser.Text = "User Name field must be at least 6 characters long!";
lblMessageUser.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
}
// Checking for user name made of same repeating character.
// Invalid example: 'aaaaaa'
else if (!txtUserName.Text.Distinct().Skip(1).Any())
{
lblMessageUser.Text = "User Name cannot be made of repeating the same characters!";
lblMessageUser.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
}
// Making sure that password and user name aren't the same.
else if (txtUserName.Text == txtPassword.Text)
{
lblMessageUser.Text = "User Name and Password can not be the same!";
lblMessagePass.Text = "User Name and Password can not be the same!";
lblMessageUser.ForeColor = Color.IndianRed;
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
passIsValid = false;
}
// If all other checks aren't trigered; enable authentication.
else
{
lblMessageUser.Text = "User Name is valid.";
lblMessageUser.ForeColor = Color.Green;
userIsValid = true;
if (passIsValid && userIsValid)
{
btnAuthenticate.Enabled = true;
}
}
}
private void txtPassword_TextChanged(object sender, EventArgs e)
{
// Checking for Null or Empty string in password field.
if (string.IsNullOrEmpty(txtPassword.Text))
{
lblMessagePass.Text = "Password field cannot be empty!";
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
passIsValid = false;
}
// Making sure that password is at least 6 characters long.
else if (txtPassword.Text.Length < 6)
{
lblMessagePass.Text = "Password field must be at least 6 characters long!";
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
passIsValid = false;
}
// Checking for password made of same repeating character.
// Invalid example: 'aaaaaa'
else if (!txtPassword.Text.Distinct().Skip(1).Any())
{
lblMessagePass.Text = "Password cannot be made of repeating the same characters!";
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
passIsValid = false;
}
// Making sure that user name and password are not the same.
// Security measure.
else if (txtUserName.Text == txtPassword.Text)
{
lblMessageUser.Text = "User Name and Password can not be the same!";
lblMessagePass.Text = "User Name and Password can not be the same!";
lblMessageUser.ForeColor = Color.IndianRed;
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
passIsValid = false;
}
// If all other checks aren't trigered; enable authentication.
else
{
lblMessagePass.Text = "Password is valid.";
lblMessagePass.ForeColor = Color.Green;
passIsValid = true;
if (passIsValid && userIsValid)
{
btnAuthenticate.Enabled = true;
}
}
}
You can merge your both events into one, then the user sees all errors and your check could be successful.
bool passIsValid, userIsValid;
public AuthenticationWindow()
{
InitializeComponent();
// Creating TextChanged events which till validate input fields.
txtUserName.TextChanged += new EventHandler(txtCheck_TextChanged);
txtPassword.TextChanged += new EventHandler(txtCheck_TextChanged);
}
private void txtCheck_TextChanged(object sender, EventArgs e)
{
// Checking for empty user name field.
if (string.IsNullOrEmpty(txtUserName.Text))
{
lblMessageUser.Text = "User Name field cannot be empty!";
lblMessageUser.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
}
// Making sure that user name is at least 6 characters long.
else if (txtUserName.Text.Length < 6)
{
lblMessageUser.Text = "User Name field must be at least 6 characters long!";
lblMessageUser.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
}
// Checking for user name made of same repeating character.
// Invalid example: 'aaaaaa'
else if (!txtUserName.Text.Distinct().Skip(1).Any())
{
lblMessageUser.Text = "User Name cannot be made of repeating the same characters!";
lblMessageUser.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
}
else
{
userIsValid = true;
lblMessageUser.Text = "Password is valid.";
lblMessageUser.ForeColor = Color.Green;
}
// Checking for Null or Empty string in password field.
if (string.IsNullOrEmpty(txtPassword.Text))
{
lblMessagePass.Text = "Password field cannot be empty!";
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
passIsValid = false;
}
// Making sure that password is at least 6 characters long.
else if (txtPassword.Text.Length < 6)
{
lblMessagePass.Text = "Password field must be at least 6 characters long!";
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
passIsValid = false;
}
// Checking for password made of same repeating character.
// Invalid example: 'aaaaaa'
else if (!txtPassword.Text.Distinct().Skip(1).Any())
{
lblMessagePass.Text = "Password cannot be made of repeating the same characters!";
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
passIsValid = false;
}
else
{
passIsValid = true;
lblMessagePass.Text = "Password is valid.";
lblMessagePass.ForeColor = Color.Green;
}
// Making sure that user name and password are not the same.
// Security measure.
if (txtUserName.Text == txtPassword.Text)
{
lblMessageUser.Text = "User Name and Password can not be the same!";
lblMessagePass.Text = "User Name and Password can not be the same!";
lblMessageUser.ForeColor = Color.IndianRed;
lblMessagePass.ForeColor = Color.IndianRed;
btnAuthenticate.Enabled = false;
userIsValid = false;
passIsValid = false;
}
// If all other checks aren't trigered; enable authentication.
if (passIsValid && userIsValid)
{
btnAuthenticate.Enabled = true;
}
}

Categories

Resources