Click here to Skip to main content
15,886,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
Thank you all to read question and help is highly appreciated.

I want to update the all password on the basis of username in the list and when it will done convert generic list into XML below format because all the password should be update in the database also.
Please check below code.

XML
<Root>
    <A>
    <UserName>12164<UserName>
    <Password>40be4e59b9a2a2b5dffb918c0e86b3d7</Password>
    </A>
</Root>


XML
public List<UserNamePassword> DecryptAllPasswordwithOldKey()
    {

        string strcon = conn.checkServer();
        SqlConnection con = new SqlConnection(strcon);
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT DISTINCT EmpCode,UserName,[Password] FROM Users", con);
        SqlDataAdapter Adap = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        Adap.Fill(dt);

        List<UserNamePassword> objectList = new List<UserNamePassword>();

        foreach (DataRow dr in dt.Rows)
        {
            UserNamePassword Objnew = new UserNamePassword();

            Objnew.UserName = Convert.ToString(dr["UserName"]);  // Beware of the possible conversion errors due to type mismatches
            //Objnew.Password = Convert.ToString(dr["Password"]);
            Objnew.Password = Decrypt_AES(Convert.ToString(dr["Password"]));

            objectList.Add(Objnew);
        }

        return objectList;

    }

  public void GenerateKey()
    {

        List<UserNamePassword> ListLogin = new List<UserNamePassword>();
        //List<UserNamePassword> ListLogin1 = new List<UserNamePassword>();

        ListLogin = DecryptAllPasswordwithOldKey();

        //ListLogin1 = ListLogin;
        for (int i = 0; i < ListLogin.Count; i++)
        {
            string UserName = ListLogin[i].UserName.ToString();
            string Newpassword = Encrypt_AES(ListLogin[i].Password.ToString());

            ListLogin.Where(d => d.UserName == UserName).First().Password = Newpassword;


        }
    }
Posted
Comments
johannesnestler 22-Sep-14 9:31am    
so any problems or questions? You didn't show the code to produce the XML (could mean you think ist trivial, or this is your Problem?)...

Just one thing I spoted on first read: ListLogin.Where(d => d.UserName == UserName).First().Password = Newpassword; why you do that when you allready iterate over all Logins? Should this line be ListLogin[i].Password = Newpassword. But this is no mistake just inefficient...

1 solution

There are many ways you could achive this, but the simplest way I found is just give a proper name of the DataSet and DataTabel and use GetXML() function of DataSet

Following code snippet could help;

C#
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("UserName",typeof(string));
dt.Columns.Add("Password", typeof(string));

DataRow dr=dt.NewRow();
dr["UserName"]="ram";
dr["Password"]="pass001";

dt.Rows.Add(dr);

dr=dt.NewRow();
dr["UserName"]="Shaym";
dr["Password"]="pass002";

dt.Rows.Add(dr);

ds.Tables.Add(dt);
ds.DataSetName="Root";
dt.TableName="A";


Output of the code is as below;

XML
<Root>
  <A>
    <UserName>ram</UserName>
    <Password>pass001</Password>
  </A>
  <A>
    <UserName>Shaym</UserName>
    <Password>pass002</Password>
  </A>
</Root>



Hope it solves your purpose.
 
Share this answer
 
v2
Comments
johannesnestler 22-Sep-14 10:19am    
good answer - IF that was the question...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900