By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,995 Members | 1,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,995 IT Pros & Developers. It's quick & easy.

C#-WEB: ManagementObject Delete DNS TXT records not working

P: 9
I can Add and Delete (A, CName, MX and TXT) Dns records on the Dns Server using C# code. The problem I am having is if someone manaully creates a TXT record on the Dns Server, I cannot delete that particular record using the same C# code. Here is what I am using to delete the records:
Expand|Select|Wrap|Line Numbers
  1. public void DeleteRecord(string domain, string recordName, string recordType, string recordData)
  2.         {
  3.             try
  4.             {
  5.                 this.ConnectToDNS(dnsServerList[x].Server, dnsServerList [x].UserName, dnsServerList[x].Pass);
  6.  
  7.                 // Find Record to Delete
  8.                 string wql = "";
  9.                 wql = "SELECT *";
  10.                 wql += " FROM MicrosoftDNS_TXTType";
  11.                 wql += " WHERE ContainerName = '" + domain + "'";
  12.                 wql += " AND recordData = '" + recordData + "'";
  13.                 wql += " AND OwnerName = '" + ownerName + "'";
  14.                 ObjectQuery q = new ObjectQuery(wql);
  15.                 ManagementObjectSearcher s = new ManagementObjectSearcher(this.Session, q);
  16.                 ManagementObjectCollection col = s.Get();
  17.                 int total = col.Count;
  18.                 foreach (ManagementObject o in col)
  19.                 {
  20.                     // Record to Delete was found
  21.                     o.Delete();
  22.                     o.Dispose();
  23.                 }
  24.             }
  25.             catch (Exception)
  26.             {
  27.                 throw;
  28.             }
  29.         }
  30.  
When the C# code tries to Delete the manually created TXT record 'o.Delete()' does not produce any errors it just doesn't do anything.

Here is a TXT record created manually on the Dns Server:

" TXT ( "v=spf1 ip4:0.0.0.0 ip4:0.0.0.0 ?all" )"

Here is a TXT record created by C# code:

" TXT ( "v=spf1 ip4:0.0.0.0 ip4:0.0.0.0 ?all"
"" )"

The interesting thing is, if I edit the TXT record manually on the Dns Server and move the cursor to the end of the text in the TextBox and hit the 'Enter' key on my keyboard then save the change, my C# code can then delete that TXT record. When you type 'Enter' after the text in the textbox on a Dns Server it formats the record to match my C# code.

Anyway I just need to find out why I can't delete a TXT record that was manually created on a Dns Server when the person entering the text does not actually hit the 'Enter' key. I would appreciate any help you could provide. Thanks in adavance.
Nov 9 '07 #1
Share this Question
Share on Google+
5 Replies


Plater
Expert 5K+
P: 7,872
Does that record work as a valid record without that extra cariage return?
does it ONLY apply to TXT type records? (Which BTW, I've yet to figure out how to use)
Nov 9 '07 #2

P: 9
Does that record work as a valid record without that extra cariage return?
does it ONLY apply to TXT type records? (Which BTW, I've yet to figure out how to use)
Yes both records are valid formats and yes it only applies to TXT type records. It is almost like the method used to create a TXT manually on a Dns Server does not add the cariage return where the method in C# adds it for you. Then the Delete method in C# requires it to be there before it can do anything with it.
Nov 9 '07 #3

P: 9
Still trying to figure this out... If anyone has an idea let me know thanks.
Nov 13 '07 #4

Plater
Expert 5K+
P: 7,872
Is there a non-code way to delete these records? That you could like invoke instead of the .net method? I don't like suggesting an API call over a framework call, but you might be able to find something?
Nov 13 '07 #5

P: 9
Is there a non-code way to delete these records? That you could like invoke instead of the .net method? I don't like suggesting an API call over a framework call, but you might be able to find something?
I think I could find a way to delete them manually via API, etc. but since the .net code is not returning any errors I would not have a way to trigger the API.
Nov 13 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.