|
|
Hi,
I wanted to know how can I encrypt any number to a 6 unique alphanumeric digits using C#?
Thanks
Jassim[^]
Technology News @ www.JassimRahma.com
|
|
|
|
|
ANY number? Down to a UNIQUE 6 digit code?
Think about that. How are you going to represent any number UNIQUELY in a space that, by definition, can only hold 1,000,000 unique values?
By the way, that's not encryption. That's a hash.
You might want to go into what you're really trying to do with this.
|
|
|
|
|
Though Dave overlooked that you were talking about an alphanumeric representation, this doesn't change the fact that you can't represent any number in 6 digits UNIQUE alphanumeric.
1.) 2'175'005'370 would be the highest number you can represent that way ("ZYXWVU").
2.) Because the digits should be unique, not all numbers in that range can be represented. The lowest number that can be represented is 1'776'965 (= "012345") and there will be several "gaps" in between.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
modified 22-May-15 21:25pm.
|
|
|
|
|
Just to add to what the others say: Encryption implies that Decryption is possible - but what you are talking about is Basically Hashing (as Dave mentioned) which cannot be reversed, cannot be "de-hashed". Once you have generated your unique code, you can't get the original input back. Encryption does not have a fixed length output, only hashing does.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
modified 23-May-15 6:23am.
|
|
|
|
|
Getting an error: input string was not in a correct format
asp:
onrowdatabound="highlightrow"
protected void highlightrow(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int ActualFTE=Convert.ToInt32(e.Row.Cells[5].Text);
if (ActualFTE > 1.25)
{
e.Row.BackColor = Color.Red;
}
}
}
modified 22-May-15 14:31pm.
|
|
|
|
|
The error message says it clearly.
You are trying to covert a decimal to int.
Change your stament like below and try.
decimal ActualFTE=Convert.ToDecimal(e.Row.Cells[5].Text);
if (ActualFTE > 1.25)
|
|
|
|
|
|
Still getting the same error.
|
|
|
|
|
Can you post what value is coming in
e.Row.Cells[5].Text It will give a better idea on what we are dealing with.
|
|
|
|
|
|
decimal ActualFTE=Convert.ToDecimal(e.Row.Cells[5].Text);
if (ActualFTE > 1.25)
In the above code check whether e.Row.Cells[5].Text returns data that can be converted to decimal. Or add ToString() at the end and see if it helps.
decimal ActualFTE=Convert.ToDecimal(e.Row.Cells[5].Text.ToString());
|
|
|
|
|
Same error: Input string was not in a correct format.
|
|
|
|
|
Just to make sure everything is fine, try hardcoding the value and see if you still get the error.
decimal ActualFTE=Convert.ToDecimal("0.9500");
If this works without exception, then you can be sure, the problem is with the expression e.Row.Cells[5].Text . This expression you use should return some string (like your value 0.9500) that can be converted to decimal for the conversion to work without exception.
|
|
|
|
|
okay it didn't error this time but nothing changed.
|
|
|
|
|
May be the value ActualFTE is not greater than 1.25, as per your condition.
if (ActualFTE > 1.25)
{
e.Row.BackColor = Color.Red;
}
So the color will not be changed.
|
|
|
|
|
That is true for most of the 251 rows. I only see a couple that are over 1.25. So I should take out that code?
|
|
|
|
|
Do you think I should post all of my code to see if I have something missing?
|
|
|
|
|
When you want the row to be colored? Change the if condition based on that. It should work.
|
|
|
|
|
Not the whole row only that field. ActualFTE.
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace StaffingWebParts.CatwComment
{
public partial class CatwCommentUserControl : UserControl
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLStaffingConn"].ConnectionString);
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
lblMsg.Text = "";
if (!Page.IsPostBack)
{
BindSubjectData();
}
}
protected void highlightrow(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal ActualFTE = Convert.ToDecimal(e.Row.Cells[5].Text.ToString());
if (ActualFTE > 1.2500M)
{
e.Row.BackColor = Color.Red;
}
}
}
protected void BindSubjectData()
{
using (SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLStaffingConn"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = ("select a.id, b.StaffTrackingID, a.ResourceName, b. EstimateHours, EstimateFTE, b.ActualHours, b.ActualFTE,b.Comment, b.CommentBy, b.Period from StaffTracking a, StaffTrackingFTEData b where a.id = b.StaffTrackingid order by ResourceName");
cmd.Connection = sqlCon;
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
gvCATW.DataSource = dt;
gvCATW.DataBind();
}
else
{
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
gvCATW.DataSource = dt;
gvCATW.DataBind();
gvCATW.Rows[0].Visible = false;
}
sqlCon.Close();
}
}
}
protected void gvCATW_RowEditing(object sender, GridViewEditEventArgs e)
{
gvCATW.EditIndex = e.NewEditIndex;
BindSubjectData();
}
protected void gvCATW_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvCATW.EditIndex = -1;
BindSubjectData();
}
protected void gvCATW_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
bool IsUpdated = false;
int StaffTrackingID =
Convert.ToInt32(gvCATW.DataKeys[e.RowIndex].Value.ToString());
TextBox ResourceName =
(TextBox)gvCATW.Rows[e.RowIndex].FindControl("txtResourceName");
TextBox EstimateHours = (TextBox)gvCATW.Rows[e.RowIndex].FindControl("txtEstimateHours");
TextBox EstimateFTE = (TextBox)gvCATW.Rows[e.RowIndex].FindControl("txtEstimateFTE");
TextBox ActualHours = (TextBox)gvCATW.Rows[e.RowIndex].FindControl("txtActualHours");
TextBox ActualFTE = (TextBox)gvCATW.Rows[e.RowIndex].FindControl("txtActualFTE");
TextBox Comment = (TextBox)gvCATW.Rows[e.RowIndex].FindControl("txtComment");
TextBox CommentBy = (TextBox)gvCATW.Rows[e.RowIndex].FindControl("txtCommentBy");
TextBox Period = (TextBox)gvCATW.Rows[e.RowIndex].FindControl("txtPeriod");
using (SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLStaffingConn"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = @"UPDATE StaffTrackingFTEData SET StaffTrackingID=@StaffTrackingID, EstimateHours=@EstimateHours, EstimateFTE=@EstimateFTE, ActualHours=@ActualHours, ActualFTE=@ActualFTE,Comment=@Comment, CommentBy=@CommentBy, period=@Period WHERE StaffTrackingID=@StaffTrackingID";
cmd.Parameters.AddWithValue("@StaffTrackingId", StaffTrackingID);
cmd.Parameters.AddWithValue("@EstimateHours", EstimateHours.Text);
cmd.Parameters.AddWithValue("@EstimateFTE", EstimateFTE.Text);
cmd.Parameters.AddWithValue("@ActualHours", ActualHours.Text);
cmd.Parameters.AddWithValue("@ActualFTE", ActualFTE.Text);
cmd.Parameters.AddWithValue("@Comment", Comment.Text);
cmd.Parameters.AddWithValue("@CommentBy", CommentBy.Text);
cmd.Parameters.AddWithValue("@Period", Period.Text);
cmd.Connection = sqlCon;
sqlCon.Open();
IsUpdated = cmd.ExecuteNonQuery() > 0;
sqlCon.Close();
}
}
if (IsUpdated)
{
lblMsg.Text = "'" + ResourceName.Text + "' CATW Comment updated successfully!";
lblMsg.ForeColor = System.Drawing.Color.Green;
}
else
{
lblMsg.Text = "Error while updating '" + ResourceName.Text + "' subject details";
lblMsg.ForeColor = System.Drawing.Color.Red;
}
gvCATW.EditIndex = -1;
BindSubjectData();
}
}
}
|
|
|
|
|
|
Mathi,
I was able to get it to work.
Thank you for your help and patience.
You and others have help me to complete this project.
protected void gvCATW_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ActualFTE")) > 1.25M)
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
|
|
|
|
|
I. Consider this scenario in WinForms:
1. you decide you want to have a custom UserControl/Component implement an Interface for the usual reasons you use Interfaces.
2. since you cannot use Fields in an Interface, you implement Properties ... that's the way Interfaces work in C# / .NET.
3. in the custom UserControl/Component that inherits the Interface, you must, of course, implement those Properties the Interface specifies as 'Public ... that's the way Interfaces work in C# / .NET.
4. when you then place/drag-drop an instance of the custom UserControl/Component on a Form:
a. if you examine the Designer.cs file for the Form: you will see that Public Properties of whatever Type declared in the UserControl/Component are initialized to null ... that's the way a WinForm works.
II. How is this an issue ?
1. in the case that you wish to set the values of the Interface-specified Properties in the UserControl/Component when they are ... internally ... initialized:
a. any initialization you've done will be erased by the execution of the code in the Designer.cs file as described in 4.a.
b. you might think you can use some Event of the UserControl/Component ... like Load, or even Layout ... to initialize the Properties: you would be wrong; the code in the Designer.cs file is executed last. And, forget using 'Layout ... design-time side-effects that are "freaky."
You might think you could use the Form's ControlAdded event: sorry, that won't work.
III. why not just call the code that initializes the Properties in the UserControl/Component in the Form after the Form is initialized ... for example, in the Form's Load event ?
a. you'll have to trust me here when I say that there is a compelling reason not to do that.
IV. What is the current work-around
Fairly simple ... if you know how to use a custom ApplicationContext, and understand what that requires.
So I switched to that. Since in that application-model I create all the Forms inside a static controller-class, then it's straightforward to invoke initialization in the right sequence.
V. What I have not considered yet:
a. in the Form's Load event doing a recursive search of all Controls, and calling initialization code for each instance of the custom UserControl/Component.
VI. other thoughts
1. while I think I understand why Interfaces cannot contain Fields, why they can't have specifiers like 'ref and 'out (thanks to Eric Lippert's essays), I sometimes wonder why a property in an Interface could not have an access modifier, and why the implementation of a Property in a Class that inherits from an Interface cannot implement that Property with a 'private modifier.
I hope that when I grow up I will understand these things
So, here's a little code illustration of the above:
public interface IHasSomething
{
Dictionary<int, string> IntToString { set; get; }
}
namespace SomeNameSpace
{
public partial class SomeComponent : TextBox, IHasSomething
{
public SomeComponent() { InitializeComponent(); }
public SomeComponent(IContainer container)
{
container.Add(this);
InitializeComponent();
this.AutoSize = true;
IntToString = new Dictionary<int, string>();
}
[Browsable(false)]
public Dictionary<int, string> IntToString { set; get; }
}
} Now assume you've compiled, and drag-dropped an instance of the custom Component onto a Form; that Form has a Button which when clicked tries to access the Dictionary implemented in the custom Component:
namespace SomeNameSpace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
someComponent1.IntToString.Add(1,"hello");
}
}
} And, here you can see where the Dictionary was nulled out in the Designer.cs code:
this.someComponent1.IntToString = null;
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
modified 23-May-15 4:02am.
|
|
|
|
|
Hi Bill,
idea 1: Omit the setter from the interface.
idea 2: Expose only the required actions on the dictionary as methods in the interface, not the dictionary (-property).
cheers, Sascha
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Thanks, Sascha !
Omitting the 'setter is a good idea, but it also means that no external "entity" with a valid reference ... to an instance of the Class that inherits the Interface ... can set the Property ... that's a strategy I can't use in this case.Sascha Lefèvre wrote: Expose only the required actions on the dictionary as methods in the interface, not the dictionary (-property). That's a very intriguing suggestion, and one I don't quite understand: do you mean have the Interface declare method signatures for such actions as Dictionary ... 'Add, 'Remove, etc. ?
Or, could you mean ... in the Interface ... declare the signature of a method whose "only purpose" is to set the initial Value of the Property ? That would work.
In this case, I am using the Interface more as "contract" than as all-the-other-uses-possible.
I am going to think about your response, and the responses by Pete O', Richard D., and Alan N., today and so some more experimentation.
My goal here is that these Properties are initialized by a Static Class, not by any code in the Forms in which the custom Component(s) are placed/instantiated. Since, in a way, control of the instances of these custom Components, no matter what "container" they are in, is a kind of "cross-cutting concern" (to mis-use a term from the AOP religion), I do think using an ApplicationContext here may be the best way to go, and it's an alternative WinForm architecture (better to say it's the "real" WinForm architecture whether or not the programmer ever realizes that) I am very familiar with.
Have a great day (well, it's GMT +07:00 here) !
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut, Jr.
|
|
|
|
|