Jump to content
Xtreme .Net Talk

PlausiblyDamp

Administrators
  • Posts

    7016
  • Joined

  • Last visited

Everything posted by PlausiblyDamp

  1. Request.UserHostAddress should do the trick.
  2. Good catch, just declare the object outside of the routine and it should work fine.
  3. The way you are currently doing it isn't guaranteed to prevent 2 threads running the routine at the same time - it is perfectly possible for 1 thread to query RoutineIsRunning and see it is false, lose it's timeslice and another thread to query the value and also see it as false. The easiest way (although not necessarily the most efficient) is to do something like Dim o As New Object Public Sub RoutineOfInterest() System.Threading.Monitor.Enter(o) ' ' The Code for this routine ' System.Threading.Monitor.Exit(o) 'or SyncLock o ' ' The Code for this routine ' End SyncLock End Sub
  4. Probably the best thing to do is decide if you wish to continue doing all the XML by hand, this will give you more control but create more work, or use something like Serialisation. If you are moving away from the raw xml you could create your own collection class (or use generics -what version of VB are you using btw?) and simply store / calculate a unique numeric ID for each record.
  5. My bad - didn't read all your post properly... http://robz.homedns.org:8080/blog/archive/2005/02/25/387.aspx looks like it might do what you want (and possibly a bit more).
  6. Dim xr As New Xml.XmlTextReader(targetUrl) would create an XmlReader object from the specified url and allow you walk over the nodes etc. Dim xr As New Xml.XmlTextReader(targetUrl) Dim doc As New Xml.XmlDocument() doc.Load(xr) would load the returned xml into a XmlDocument - the .Net equivalent of a DOMDocument. If the xml is being returned from a web service then .Net provides an entire set of classes and framework objects for dealing with the remote server which may be worth looking at.
  7. Basic sample class... Public Class Member Private _Name As String Private _Phone As String Private _Address As String Public Sub New() End Sub Public Sub New(ByVal name As String, ByVal phone As String, ByVal address As String) _Name = name _Phone = phone _Address = address End Sub Public Property Name() As String Get Return _Name End Get Set(ByVal value As String) _Name = value End Set End Property Public Property Phone() As String Get Return _Phone End Get Set(ByVal value As String) _Phone = value End Set End Property Public Property Address() As String Get Return _Address End Get Set(ByVal value As String) _Address = value End Set End Property End Class Sample code to populate / save list... Dim members As New List(Of Member) 'for .net 2 use an array or arraylist for .net 1 members.Add(New Member("Jim Bob", "(123) 456-7890", "Not a cowboy")) members.Add(New Member("John Hancock", "(098) 765-4321", "Sometown")) members.Add(New Member("etc.", "etc.", "etc.")) Dim xs As New Xml.Serialization.XmlSerializer(GetType(List(Of Member))) Dim fs As New System.IO.FileStream("c:\test.xml", IO.FileMode.Create, IO.FileAccess.ReadWrite) xs.Serialize(fs, members) fs.Close() Reading it back in... Dim xs2 As New Xml.Serialization.XmlSerializer(GetType(List(Of Member))) Dim fs2 As New System.IO.FileStream("c:\test.xml", IO.FileMode.Open, IO.FileAccess.Read) Dim m As List(Of Member) m = DirectCast(xs2.Deserialize(fs2), List(Of Member)) fs2.Close() hope that gets you started.
  8. There is no reason at all to feel you need to move to http://www.xtremevbtalk.com/ for future questions - we have nothing against VB.Net here (in fact several of the regular posters are VB developers primarily). Often the issues are more to do with the .Net framework rather than the language syntax. You are probably going to find answers over here will have less 'legacy' VB functionaility and more '.Net' functionality though.
  9. Try dim curString as string Each curString In s If curString.StartsWith("var1=") Then var1 = curString.Remove(0, curString.IndexOf("=") + 1) ElseIf curString.StartsWith("var2=") Then var2 = curString.Remove(0, curString.IndexOf("=") + 1) End If Next the syntax posted needs .Net 1.1 or higher.
  10. As a minimum you are probably better switching to the classes under System.IO for your file handling. A rough translation of your code would be something like Public Sub LoadSettingsFromFile(ByVal FileName As String) On Error GoTo ErrorHandler If File.Exists(FileName) Then Dim tmpString As String Dim QuitReadingFile As Boolean = False Dim fs As FileStream = File.Open(FileName, FileMode.Open, FileAccess.Read) Dim br As New BinaryReader(fs) While QuitReadingFile = False tmpString = br.ReadString() If tmpString.IndexOf("DONE") > 0 Then QuitReadingFile = True ElseIf tmpString.IndexOf("Variable1=") > 0 Then var1 = tmpString.Remove(0, tmpString.IndexOf("=") + 1) ElseIf tmpString.IndexOf("Variable2=") > 0 Then var2 = tmpString.Remove(0, tmpString.IndexOf("=") + 1) End If End While fs.Close() Else MsgBox("Can not find the file containing the variable." + vbCrLf + _ "The Specified File = " + FileLocation_GameWindowTitle1) End If Exit Sub ErrorHandler: MsgBox("An Error Occured in LoadSettingsFile SubRoutine." _ + vbCrLf + "Error Number = " + Err.Number.ToString + vbCrLf + _ "Error Message = " + Err.Description + vbCrLf + "Error Source = " _ + Err.Source, MsgBoxStyle.Critical, "Error Occured") End Sub although you might also want to use messagebox.Show() rather than msgbox and a Try ... Catch rather than On Error Goto. As this is using native classes it should remove the need for interop with the FSO library. If the file has one entry per line then you could make this easer by using a streamreader On Error GoTo ErrorHandler If File.Exists(FileName) Then Dim tmpString As String Dim QuitReadingFile As Boolean = False Dim sr As New StreamReader(FileName) While QuitReadingFile = False tmpString = sr.ReadLine() If tmpString.IndexOf("DONE") > 0 Then QuitReadingFile = True ElseIf tmpString.IndexOf("Variable1=") > 0 Then var1 = tmpString.Remove(0, tmpString.IndexOf("=") + 1) ElseIf tmpString.IndexOf("Variable2=") > 0 Then var2 = tmpString.Remove(0, tmpString.IndexOf("=") + 1) End If End While sr.Close() Else MsgBox("Can not find the file containing the variable." + vbCrLf + _ "The Specified File = " + FileLocation_GameWindowTitle1) End If Exit Sub ErrorHandler: MsgBox("An Error Occured in LoadSettingsFile SubRoutine." _ + vbCrLf + "Error Number = " + Err.Number.ToString + vbCrLf + _ "Error Message = " + Err.Description + vbCrLf + "Error Source = " _ + Err.Source, MsgBoxStyle.Critical, "Error Occured") End Sub or even Public Sub LoadSettingsFromFile(ByVal FileName As String) On Error GoTo ErrorHandler If File.Exists(FileName) Then Dim tmpString As String Dim sr As New StreamReader(FileName) tmpString = sr.ReadToEnd() Dim s() As String = tmpString.Split(Environment.NewLine) For Each line As String In s If line.StartsWith("Variable1=") Then var1 = tmpString.Remove(0, tmpString.IndexOf("=") + 1) ElseIf line.StartsWith("Variable2=") Then var2 = tmpString.Remove(0, tmpString.IndexOf("=") + 1) End If Next sr.Close() Else MsgBox("Can not find the file containing the variable." + vbCrLf + _ "The Specified File = " + FileLocation_GameWindowTitle1) End If Exit Sub ErrorHandler: MsgBox("An Error Occured in LoadSettingsFile SubRoutine." _ + vbCrLf + "Error Number = " + Err.Number.ToString + vbCrLf + _ "Error Message = " + Err.Description + vbCrLf + "Error Source = " _ + Err.Source, MsgBoxStyle.Critical, "Error Occured") End Sub would probably work. Even easier though would be to use the built in XML serialisation support, or if you are using 2005 then this is done for you via the Settings tab of the project's property page.
  11. I have very little knowledge of working with FoxPro - however if it supports the concept of a SELECT ... WHERE statement then it will probably be far more efficient to use that rather than manually looping through the records yourself.
  12. As a not too wild a guess - are you running the application on IIS6? If so is it in an application pool which also is used by .Net 1.1 applications? There is a problem with different versions of .Net being hosted in the same application pool with IIS6 - easiest fix is to have a separate pool for each version of the runtime.
  13. How is it formatted in the database?
  14. If you are doing a release build you don't need to set it to null for objects within a method, the GC will free resources that are no longer used even if it triggers mid function. For class level variables then if you no longer need the object setting it to null will allow the GC to free it.
  15. Dispose is only required when an object holds onto non-managed resources (think file handles, db connections etc.) as these need an immediate release when no longer required. Even though the XmlDocument class can be a memory hog it's just memory and therefore the GC knows best ;)
  16. Firstly, from a personal point of view, there is a lot of legacy VB6 style of coding in there (RTrim, Replace etc.) so you might want to convert them over to a more .Net way of doing things. If you prefer the VB syntax though that's fine. Secondly, again from a personal point of view, the naming convention(s) in use make for a fairly hard to read piece of code. You might want to pick a particular convention in regards to prefix or not, casing etc. and apply it as standard. Also your public methods are not following the MS guidelines andas such could also do with a revision. Some parts of the code appear to be very cut and paste i.e. If strDBFile = "TMDENTS" Then MainForm.lvTodaysEvents.Items.Clear() 'clear the lv of events CurrentDayStr = Format(System.DateTime.FromOADate(Today.ToOADate + intAdvance), "dddd") 'format the date to the day of the week plus the advance number For I = 0 To objDS1.Tables(strDBFile).Rows.Count - 1 With objDS1.Tables(strDBFile).Rows(I) strDay = Replace(!Day, " ", "") If strDay = CurrentDayStr Then 'was the day found blnDayNotFound = True LVR = 1 ' As I am going through this can't remeber what this is for St = Replace(!StartTime, " ", "") Et = Replace(!EndTime, " ", "") D = Replace(!Day, " ", "") DS = RTrim(!Descript) Itm = MainForm.lvTodaysEvents.Items.Add(St) Itm.ImageIndex = 6 Itm.SubItems.Add(Et) Itm.SubItems.Add(D) Itm.SubItems.Add(DS) End If End With Next I ElseIf strDBFile = "PADEVENT" Then MainForm.lvTodayIboc.Items.Clear() CurrentDayStr = Format(System.DateTime.FromOADate(Today.ToOADate + intAdvance), "dddd") For I = 0 To objDS1.Tables(strDBFile).Rows.Count - 1 With objDS1.Tables(strDBFile).Rows(I) strDay = Replace(!Day, " ", "") If strDay = CurrentDayStr Then blnDayNotFound = True LVR = 1 St = Replace(!StartTime, " ", "") Et = Replace(!Day, " ", "") D = Replace(!Title, " ", "") DS = RTrim(!Artist) AL = RTrim(!Album) Dis = RTrim(!Discript) Itm = MainForm.lvTodayIboc.Items.Add(St) Itm.ImageIndex = 6 Itm.SubItems.Add(Et) Itm.SubItems.Add(Dis) Itm.SubItems.Add(D) Itm.SubItems.Add(DS) Itm.SubItems.Add(AL) End If End With Next I End If could probably be replace with a method... Public Sub Test(ByVal lv As ListView, ByVal padEvent As Boolean) Dim Itm As ListViewItem Dim strConnect, strSql As String Dim St, Et, D, DS, AL, Dis As String Dim I As Integer Dim objDT1 As DataTable = objDS1.Tables(0) Dim blnDayNotFound As Boolean 'was the date found Dim strDay As String Dim objDA1 As New OdbcDataAdapter Dim objDS1 As New System.Data.DataSet Dim dbCommand As New Data.OleDb.OleDbCommand Dim intCounter As Integer Dim mvarSysProjPath As String = Application.StartupPath & "" strConnect = "Provider=MSDASQL/SQLServer ODBC;Driver={Microsoft Visual FoxPro Driver};" _ & "SourceType=DBF;SourceDB=" & mvarSysProjPath _ & ";InternetTimeout=300000;Transact Updates=True" strSql = "SELECT * FROM " & strDBFile & ".dbf" objDA1 = New OdbcDataAdapter(strSql, strConnect) objDA1.Fill(objDS1, strDBFile) lv.Items.Clear() 'clear the lv of events CurrentDayStr = Format(System.DateTime.FromOADate(Today.ToOADate + intAdvance), "dddd") 'format the date to the day of the week plus the advance number For I = 0 To objDS1.Tables(strDBFile).Rows.Count - 1 With objDS1.Tables(strDBFile).Rows(I) strDay = Replace(!Day, " ", "") If strDay = CurrentDayStr Then 'was the day found blnDayNotFound = True LVR = 1 ' As I am going through this can't remeber what this is for St = Replace(!StartTime, " ", "") Et = Replace(!EndTime, " ", "") D = Replace(!Day, " ", "") DS = RTrim(!Descript) Itm = lv.Items.Add(St) Itm.ImageIndex = 6 If padEvent Then Itm.SubItems.Add(Et) Itm.SubItems.Add(Dis) Itm.SubItems.Add(D) Itm.SubItems.Add(DS) Itm.SubItems.Add(AL) Else Itm.SubItems.Add(Et) Itm.SubItems.Add(D) Itm.SubItems.Add(DS) End If End If End With Next I End Sub Public Sub TodaysList(ByVal intAdvance As Integer, ByVal strDBFile As String) Do If strDBFile = "TMDENTS" Then Test(MainForm.lvTodaysEvents, False) ElseIf strDBFile = "PADEVENT" Then Test(MainForm.lvTodayIboc, True) End If If blnDayNotFound = False Then intAdvance = intAdvance + 1 'add one to the date Else If LVR = 1 Then 'can't remember If intAdvance = 7 Then NextWeek = True End If NextInList(strDBFile) End If Exit Do End If Loop Until intAdvance > 7 End Sub although a more appropriate method name than 'Test' is in order ;) Once you make a start you are bound to find out other things that can be improved etc. Ideally if you have a set of test cases (unit tests are ideal) then as you tidy up the code you can always regression test it to prevent new bugs being introduced.
  17. As Cags mentioned - you could loop through the elements far easier... public void WriteXML(String TheParent, String TheChild, String TheValue) { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(@"SP.xml"); System.Xml.XmlNode node = doc.DocumentElement; foreach (XmlElement curNode in node) { if (curNode.LocalName == TheChild) { SetElement(curNode, TheChild, TheValue); break; } } doc.Save(@"SP.xml"); } ...should do the same thing. However the entire method could probably be simplified to public void WriteXML(String TheParent, String TheChild, String TheValue) { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(@"SP.xml"); System.Xml.XmlNode node = doc.DocumentElement; SetElement(node[TheChild], TheChild, TheValue); doc.Save(@"SP.xml"); } In the SetElement method the first part seems redundant if (settingElement[path].InnerText == value) { settingElement[path].InnerText = value; } simply checks to see if the inner text is equal to value, and if it is then makes it equal to the same value :confused: if (settingElement[path].InnerText != value) { System.Xml.XmlDocument doc = settingElement.OwnerDocument; System.Xml.XmlElement newElement = doc.CreateElement(path); newElement[path].InnerText = value; } should do the same thing. It looks as though you have a couple of minor typos though public void WriteXML(String TheParent, String TheChild, String TheValue) { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(@"SP.xml"); System.Xml.XmlNode node = doc.DocumentElement; SetElement(node[TheParent], TheChild, TheValue); doc.Save(@"SP.xml"); } is probably what you mean to do for the WriteXML. The main problem I have is I'm unsure if you wish to change the value of the existing Subjects element or add a new element. For the first case you could just do private void SetElement(System.Xml.XmlElement settingElement, string path, string value) { if (settingElement[path].InnerText != value) settingElement[path].InnerText = value; } If you need to append a new element try something along the lines of private void SetElement(System.Xml.XmlElement settingElement, string path, string value) { if (settingElement[path].InnerText != value) { System.Xml.XmlDocument doc = settingElement.OwnerDocument; System.Xml.XmlElement newElement = doc.CreateElement(path); newElement.InnerText = value; settingElement.ParentNode.AppendChild(newElement); } }
  18. If you are using VS 2005 then you are using .Net 2.0, .net 1.1 used VS 2002 / 2003. The readonly keyword doesn't make the class behave like a singleton on it's own, however it can be useful if you are using the singleton pattern. The main use for it would be in preventing other code doing things like LoadStaticImages.ImageSpace = null; //or similar In terms of fixing the memory error it might not remove the underlying problem for the particular individual - however having a more efficient memory model may resolve the problem; the best thing to do is try it and see - even if the problem doesn't go away you have at least got a better / more stable code base for the future.
  19. You might want to change the definitions to be public readonly static as this will prevent the objects being re-assigned. The idea of a static class is new in .Net 2.0, if you are on an older version then you will not be able to declare a static class. Although the overall memory may not change you are probably putting less strain on the garbage collector as it will not need to be releasing memory as often.
  20. If ctype works, DirectCast should as well. You need to make sure that when you refer to a control the text is the same including the casing.
  21. http://www.icsharpcode.net edit: beaten to it...
  22. The update command should be a parameterised string that the DataAdapter will call with the relevant parameters, you don't need to build it yoursefl (and if you do using string concatenation is a bad way to do it). If you are happy to build the update command yourself then you can just execute it directly and not bother with the DataAdapter.Update.
  23. It will only work if the anonymous template is showing and if you have the control ID exactly right.
  24. The Database (or Initial Catalog) entries specify the name of the database on the server to connect to; in your example you are conecting to a database called database1. The 'user instance = true' isn't really a hack at all, normally SQL Server runs as a background service, SQLExpress being a stripped down version also allows this behaviour. One of the problems is that in a shared hosting environment (like an ISP who hosts your site) you may not have access to a SQL server running as a service or if you do various permissions need to be configured to make things work - hence the error you were getting originally. SQLExpress allows you to treat the database (.MDF file) as a stand alone file (similar to how MS Access works) and therefore avoid the configuration problems with SQL server - that is what user instance is for.
  25. DirectCast(LoginView1.FindControl("TextBox1"), TextBox).Text = "Test"
×
×
  • Create New...