Solved

Avoid creating duplicates in a table without killing the process

Posted on 2004-10-14
4
208 Views
Last Modified: 2010-04-15
I am EXTREMELY new to C#, and but have been sucessfully updating a program which reads an excel file and converts the data to XML.  
My problem is as I cycle thru the data, I don't want to add a duplicate "process" to my table.  The way it is currently setup, the table accepts duplicates and the records are the same except for the GUID value.  This is causing problems with the XML.

This is an excerpt of the code as it currently sits.  I tried adding a .Unique=true to the "Name" column in my table, but it throws an error that I don't know how to catch - and kills the program.  This is probably basic, but I am a real newbie. ;-)  

I am supposed to demo this process next week, and it would make me look really good if this issue was fixed - hence the high points!

//Setup table structure
table = data.Tables.Add("Process");
table.Columns.Add("Name", Type.GetType("System.String")).Unique = true;
table.Columns.Add("LocationID", Type.GetType("System.Int32"));
table.Columns.Add("ProcessID", Type.GetType("System.Int32")).AutoIncrement = true;
table.Columns.Add("GUID", Type.GetType("System.Guid")).Unique = true;



//define method to addProcess
private int addProcess(string process, int locationID)      {
DataRow row = data.Tables["Process"].NewRow();
row["Name"] = process;
row["LocationID"] = locationID;
row["GUID"] = System.Guid.NewGuid();
data.Tables["Process"].Rows.Add(row);

return (int) row["ProcessID"];
}

//Add processes to table
Excel.Range pcell = (Excel.Range) sheet.Cells[2, 3];
string process = getCellValue(pcell);
int processID = addProcess(process, locationID);


0
Comment
Question by:ImageryGrl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:Kahhoe
ID: 12316165
Probably cause by duplicate name when you add a new row, modified you method as below to catch the error and promt the user. Of course you can choose to ignore it silently.

private int addProcess(string process, int locationID)    
{
     DataRow row = data.Tables["Process"].NewRow();
     row["Name"] = process;
     row["LocationID"] = locationID;
     row["GUID"] = System.Guid.NewGuid();

     try
    {
          data.Tables["Process"].Rows.Add(row);
     }
     catch(Exception e)
     {
          MessageBox.Show(e.Message);
     }

     return (int) row["ProcessID"];
}
0
 

Author Comment

by:ImageryGrl
ID: 12324217
Thanks Kahhoe,

What do I put in the Catch to handle it silently?  in this case, do I need to declare the "e"?
0
 
LVL 1

Accepted Solution

by:
Kahhoe earned 500 total points
ID: 12326118
Depend on what you do on the duplicate data. When an exception is caught, the codes inside the try bracket are not actually executed. So if you leave the catch bracket blank then you just skip the duplicate data silently. The Exception e is optional. Just if you need to know what kind of exception have you caught. Or, if you want to do something on the data, you can just do it inside the catch bracket. As long as you do not pop up any message to the user, it is silent.. :-)
0
 

Author Comment

by:ImageryGrl
ID: 12340535
works like a charm!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question