Thanks Igor. As you suggested, I ditched the attempt to store SqlConnection and DataAdapter classes, and created a new insance when I needed them. It works fine now. I am still caching the dataset, because it is relatively small and I think I'd take a bigger hit from loading it every time I need it (which is often). But I'll watch out for this as I scale my app.
Ed 54
Posts
-
how to update dataset after session cache? -
how to update dataset after session cache?I'm teaching myself ADO .NET, and am struggling with how to programmatically update a dataset after I have cached it and recovered it in an ASP .NET web app. I want to use a DataAdapter to populate a DataSet, cache the DataSet in Session, then recover the DataSet in a different page, work on it, and update the database. I can populate the DataSet, cache it, recover it, and make changes. However, I cannot get the adapter.update(dataset, "table") command to work properly. Have tried multiple solutions, with multiple errors revolving around lack of INSERT commands, uninitialized connection strings, etc. What programmatic steps must I take in a new page in order to reconnect that dataset with the origin database? Do I have to instantiate a new DataAdapter, build new commands, open a new connection, etc? Can I cache and recover any of the relevant objects (DataAdapter, SqlCommand, commandBuilder) and use them? Here's the code to populate and cache the DataSet. It works fine (but may be excessive):
using (SqlConnection connection = new SqlConnection(connStr)) { try { SqlDataAdapter pointAdapter = new SqlDataAdapter(); pointAdapter.TableMappings.Add("Table", "UserPoints"); SqlCommand pointCommand = new SqlCommand(); pointCommand.CommandText = "SELECT \* FROM Points WHERE userID = @userID"; pointCommand.Parameters.AddWithValue("@userID", userIDstr); pointCommand.Connection = connection; pointCommand.CommandType = CommandType.Text; pointAdapter.SelectCommand = pointCommand; SqlCommandBuilder pointCmdBuilder = new SqlCommandBuilder(pointAdapter); pointAdapter.Fill(ds); Session\["UserDS"\] = ds;
And here's what I'm trying to do in a different page:
dataset = (DataSet)Session\["UserDS"\]; // ... do some stuff to rows using (SqlConnection connection = new SqlConnection(connStr)) { try { SqlDataAdapter pointAdapter = (SqlDataAdapter)Session\["pointAdapter"\]; pointAdapter.Update(dataset, "UserPoints");
With this code I get a "connection string uninitialized" error. But I've gotten several different errors, enough to convince me I'm going about this the hard way. Is there a standard pattern for this sort of thing, or a tutorial someone can point me to?
-
strategy for when to update DB from dataset [modified]Hi, What event should I use to trigger an update of a dataset to the originating DB, in order to take max advantage of dataset scalability without losing data? I'm building an ASP .NET website driven by an SSE database. Hopefully there will be several thousand users. When a user logs on, the codebehind queries the DB and retrieves up to several dozen records belonging to that user. The user can add, delete, and modify records on several webforms. I'm using a dataset and SqlDataAdapter to get the records at login and caching them in the Session object. Right now I'm calling
SqlDataAdapter.Update(dataset)
every time the user submits a webform with a new record. That works, but it doesn't seem very efficient. But I can't figure out how else to do it without potentially losing records. What if the user enters a record, and then closes the browser window? What if he times out or goes to another website before my Update method runs? How do I take better advantage of datasets and data adapters? Is there a way to detect the end of a Session and set up an event handler to run an update then? Or maybe cache to the Application and run an update on time intervals? I apologize if this question is elementary. I'm early in the learning process for C# / ASP .NET / ADO. TIA -- modified at 0:15 Wednesday 5th July, 2006 -
Deserialize XML by reverse-engineering class defThat's a good idea, thanks. I already built a class that I think matches the XML. Should be simple to serialize an object and compare it to the XML I'm trying to deserialize.
-
Deserialize XML by reverse-engineering class defNo, I'm not sure, and it probably wasn't. In fact, most likely it was generated by another language, like php or javascript. I didn't realize that was an issue. I figured that since an XML file was just text in XML format, with no special notation to indicate that it was serialized, it wouldn't matter whether it was serialized or generated by another method. As long as the class definition matched the XML format, I couldn't see how the deserializer would know any better. But I'm new to all this. In any event, I wound up parsing the XML with XmlTextReader, and it wasn't too hard since I'm only extracting one node right now. But if I wanted to extract multiple nodes, or a whole data record, deserialization would be a better solution if it were possible. Is it?
-
Deserialize XML by reverse-engineering class defCan I deserialize XML into an object by reverse-engineering the class definition from a sample XML document, if I don't have the actual class definition from which the XML was serialized? I'm making function calls to the Google Maps API geocoder using an HTTP request. The function call returns an XML file, which I want to extract data from. Here are the first few lines of the XML file:
<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://earth.google.com/kml/2.0"> <Response> <name>1600 Amphitheatre Parkway, Mountain View,CA</name> ....
And here are the first few lines of the class definition that I reverse-engineered from the XML:
\[XmlRootAttribute(ElementName = "kml", IsNullable = false)\] public class kmlJSON { public kmlJSON() { } public class Response { public String name; ....
Here is my C# routine to deserialize the data into an object of kml class:
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream resStream = response.GetResponseStream(); XmlSerializer xs = new XmlSerializer(typeof(kmlJSON)); kmlJSON jsonObject = new kmlJSON(); jsonObject = (kmlJSON)xs.Deserialize(resStream); //try-catch block omitted for brevity
When I run it, I get the following error at
jsonObject = (kmlJSON)xs.Deserialize(resStream);
:\[InvalidOperationException: <kml xmlns='http://earth.google.com/kml/2.0'> was not expected.\] Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderkmlJSON.Read3\_kml() +91 \[InvalidOperationException: There is an error in XML document (1, 40).\]
Any ideas why XmlDeserializer is getting stuck on the root declaration of the XML file? Am I missing something simple, or must I have the exact class definition that the XML was serialized from?
-
object vs. text streamI think I may have answered my own question with a little more web searching. Looks like I should use the Deserialize() method of the XmlSerializer class to convert the XML string into an object type. I'm still a little shaky on OOP, but I can then cast the object into a custom defined type so I can extract the fields I need, right?
-
object vs. text streamHello, sorry in advance if this question is too simple for this crowd. I'm new to much of this. I'm building an ASP .NET 2.0 site with C# code behind. It includes calls to the Google Maps API. One of the functions I need is a geocode function. It takes an address and returns lat/long coordinates. I can call it from the client or server with javascript (not preferred), or from the C# code-beside using an HTTP request like this:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("URIcodeString");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();The address and response type are coded into the URIcodeString. I can opt to recieve the response either as an object or as XML. The object looks like this. I can get as far as reading resStream into a String. From there, I'm planning on converting it to XML and parsing it to extract the data elements I need. That will entail some learning on my part, as I don't know XML. That's ok, but it does seem like a brute-force solution. Question: is there a way to receive the JSON object directly and extract the fields I need, to avoid having to parse strings and/or XML? If I can, should I, or is parsing a stream a safer solution when dealing with data sent over the web?