asp.net c# array

Webboy2008
Webboy2008 used Ask the Experts™
on
string encode_everything = Alldata.ToString();
It try to response.writet encode_everything. but all i see is system.collections.arraylist. I want to see the value. how can i do that?
Thank you
protected void Page_Load(object sender, EventArgs e)
    {
       
        string aemail     = Request.QueryString["aemail"];
        string typeofbond = Request.QueryString["typeofbond"];
        string bondamount = Request.QueryString["bondamount"];
        string exedate    = Request.QueryString["exedate"];
        string term       = Request.QueryString["term"];
        string priname    = Request.QueryString["priname"];
        string phone      = Request.QueryString["phone"];
        string pfax       = Request.QueryString["pfax"];
        string pstreet    = Request.QueryString["pstreet"];
        string pcity      = Request.QueryString["pcity"];
        string pstate     = Request.QueryString["pstate"];
        string pzip       = Request.QueryString["pzip"];
        int quote_statusID = 1;
        string assigned_quoteID;
        string date_created         = "01/01/2009";
        string created_by           = "xxx";
        
        ArrayList Alldata = new ArrayList();
        Alldata.Add(aemail);
        Alldata.Add(typeofbond);
        Alldata.Add(bondamount);
        Alldata.Add(exedate);
        Alldata.Add(term);
        Alldata.Add(priname);
        Alldata.Add(phone);
        Alldata.Add(pfax);
        Alldata.Add(pstreet);
        Alldata.Add(pcity);
        Alldata.Add(pstate);
        Alldata.Add(pzip);
        string encode_everything = Alldata.ToString();
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
 for (int i = 0; i < Alldata.Length; i++)
  {
   encode_everything = encode_everything +  Alldata[i].ToString() + "<br />";
  }


Top Expert 2013

Commented:
foreach (string str in Alldata)
        {
            Response.Write(str + "<br \\>");
        }
Raghu MutalikdesaiPassionate for code

Commented:
Another way to address this would be to use StringBuilder (System.Text namespace) instead. Also, you can keep all your query string fields in one array so that you write less number of lines whenever you have additional fields.

string[] fieldList = {
   "aemail", "typeofbond", "bondamount", "exedate", 
   "term", "priname", "phone", "pfax", "pstreet", 
   "pcity", "pstate", "pzip"
};
 
int quote_statusID       = 1;
string assigned_quoteID;
string date_created      = "01/01/2009";
string created_by        = "xxx";
 
string newLine = @"<br/>";
StringBuilder allData = new StringBuilder();
foreach (string field in fieldList)
{
   string value = Request.QueryString.Get(field);
   allData.Append(value + newLine);
}
 
string encode_everything = allData.ToString();

Open in new window

Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Author

Commented:
medhavi: working fine thk. but now the question is if I saved into the database and try to retrieve back from there. I get a long string with fix data. How can i get each one back individually.
like I want to get aemail and state only.

Thank you
Top Expert 2013

Commented:
So do you store all those values as one string in one column in the DB?
Can you show what is your DB table look like or how you are storing those values?
What Data store is it?
If its is stored as one concatenated string ...what is the purpose?

One way is storing as comma seperated String i.e.
replace --- string newLine = @"<br/>"; by
string newLine = @",";

Then on retrieval from DB
string[] values = encode_everything.Split(,);
string aemail = values[0]; //considering the above example
string state = value[10];

Author

Commented:
Actually, the purpose of this is we do not want to create too many columns in the database because this database is just like a container. It will store everything.
Let me try your codes now.
Top Expert 2013

Commented:
oh i see an error in the code above...
string[] values = encode_everything.Split(',');

Author

Commented:
Don't worry i fixed it.
I actually added the codes for security, see attached. Everything is working fine.
here is  the code
string encode_everything = base64Encode(allData.ToString());
Now I want to put function base64Encode and base64Decode into App_Code folder.
After I put, I want to know how to call the function back in aspx page. can you show me?
I appreicate your helps.
private string base64Encode(string sData) 
    { 
        try 
        { 
            byte[] encData_byte = new byte[sData.Length]; 
            encData_byte = System.Text.Encoding.UTF8.GetBytes(sData); 
            string encodedData = Convert.ToBase64String(encData_byte); 
            return encodedData; 
        } 
        catch (Exception ex) 
        { 
            throw new Exception("Error in base64Encode" + ex.Message); 
        } 
    }
    public string base64Decode(string sData) 
    { 
        System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); 
        System.Text.Decoder utf8Decode = encoder.GetDecoder(); 
        byte[] todecode_byte = Convert.FromBase64String(sData); 
        int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length); 
        char[] decoded_char = new char[charCount]; 
        utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0); 
        string result = new String(decoded_char); 
        return result; 
    }

Open in new window

Top Expert 2013

Commented:
1: Add a class to App_Code folder ..here e.g. i named it UtilityClass:
The code looks like below:
Note the public static ---class and methods:

public static class UtilityClass
{
    public static string base64Encode(string sData)
    {
        try
        {
            byte[] encData_byte = new byte[sData.Length];
            encData_byte = System.Text.Encoding.UTF8.GetBytes(sData);
            string encodedData = Convert.ToBase64String(encData_byte);
            return encodedData;
        }
        catch (Exception ex)
        {
            throw new Exception("Error in base64Encode" + ex.Message);
        }
    }
    public static string base64Decode(string sData)
    {
        System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
        System.Text.Decoder utf8Decode = encoder.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(sData);
        int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decoded_char = new char[charCount];
        utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        string result = new String(decoded_char);
        return result;
    }
}

In .aspx code-behind...
string encode_everything = UtilityClass.base64Encode(allData.ToString());

and same for decode

Author

Commented:
error.
Error      1      'UtilityClass.UtilityClass()' must declare a body because it is not marked abstract or extern

Author

Commented:
Do not worry about the error i fixed it. but now I hope to use Gridview on the page en on from DB
I know I can databind it, but I have to first decode the filecontent column, separate the data, and
then use gridview to show the information.

is this possible? Sorry i may ask u too much questions.
Top Expert 2013
Commented:
That might become tricky...try something like this:

        DataTable dt = new DataTable("test");
        dt.Columns.Add("aemail");
        dt.Columns.Add("state");
        //similarly create other columns
        DataRow dr = dt.NewRow();
        dr["aemail"] = "test@email.com"; //get this from DB/decode
        dr["state"] = "MA"; //get this from DB/decode
        //set other column values
        dt.Rows.Add(dr);
        GridView1.DataSource = dt;
        GridView1.DataBind();

Author

Commented:
It is tricky and I do not really feel comfortable to use this code even it is working, but i highly appreicate your helps, and I may want to research and see we have better ways to handle this task.
If you know any, let me know. thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial