Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I save this serialized data?


C#
String connStr = ConfigurationManager.ConnectionStrings["MDFserializerdb"].ConnectionString;
            String cmdStr = "SELECT * FROM [Table1];";
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            try
            {
                using (SqlConnection conn = new SqlConnection(connStr))
                {
                    using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
                    {
                        conn.Open();
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        da.Fill(ds);
                        dt = ds.Tables[0];
                        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                        List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                        Dictionary<string, object> row;
                        foreach (DataRow dr in dt.Rows)
                        {
                            row = new Dictionary<string, object>();
                            foreach (DataColumn col in dt.Columns)
                            {
                                row.Add(col.ColumnName, dr[col]);
                            }
                            rows.Add(row);
                        } 
                        String obj = serializer.Serialize(rows);
                        conn.Close();
                        cmd.Dispose();
                        conn.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                Label2.Text = ex.ToString();
            }
            FileWriter file = new FileWriter("c:\\path\\test.json");
            file.write(obj.ToJSONString());
            file.flush();
            file.close();


ASP.net does not accept "FileWriter".
Posted
Updated 1-Oct-14 1:44am
v4
Comments
ZurdoDev 1-Oct-14 7:35am    
What is it you are trying to actually do?
Ashish_Agrawal 1-Oct-14 7:43am    
Assuming you are going to use this data at client side (javascript), you can put this data into a hidden field and then access it on client side.
Sinisa Hajnal 1-Oct-14 7:49am    
What do you mean "Does not accept FileWriter"? You get some error? Also, you should dispose of the fileWriter - and wrap it in try...catch...disposing in finally

Same with conn and cmd...disposing in Finally block ensures you dispose of them even if there is some exception.
Thomas Nielsen - getCore 1-Oct-14 8:28am    
Would you prefer JSON or XML format for serialization format?
Can you examplify the columns in your data??
The best serializers work on contracts or classes and are controlled by adding attributes to a data class so it can influence the implementation and make it much more clear to use and maintain, to be specifik.

1 solution

I think FileWriter is a class in Java not in Asp.net. You can use StreamWriter instead.
Something similar to this..
C#
using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/data.txt"), true))
 {
  _testData.WriteLine(obj); // Write the file.
 }


Edited: Removed ToJSONString().
 
Share this answer
 
v2
Comments
Teledextri 1-Oct-14 8:04am    
Is there an assembly reference that I am missing for "ToJSONString"?
Ashish_Agrawal 1-Oct-14 8:21am    
ToJSONString() is also a java function. Just removed it and save the obj only.

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