ImageryGrl
asked on
Avoid creating duplicates in a table without killing the process
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.Strin g")).Uniqu e = true;
table.Columns.Add("Locatio nID", Type.GetType("System.Int32 "));
table.Columns.Add("Process ID", Type.GetType("System.Int32 ")).AutoIn crement = 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"].New Row();
row["Name"] = process;
row["LocationID"] = locationID;
row["GUID"] = System.Guid.NewGuid();
data.Tables["Process"].Row s.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);
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.Strin
table.Columns.Add("Locatio
table.Columns.Add("Process
table.Columns.Add("GUID", Type.GetType("System.Guid"
//define method to addProcess
private int addProcess(string process, int locationID) {
DataRow row = data.Tables["Process"].New
row["Name"] = process;
row["LocationID"] = locationID;
row["GUID"] = System.Guid.NewGuid();
data.Tables["Process"].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);
ASKER
Thanks Kahhoe,
What do I put in the Catch to handle it silently? in this case, do I need to declare the "e"?
What do I put in the Catch to handle it silently? in this case, do I need to declare the "e"?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
works like a charm!
private int addProcess(string process, int locationID)
{
DataRow row = data.Tables["Process"].New
row["Name"] = process;
row["LocationID"] = locationID;
row["GUID"] = System.Guid.NewGuid();
try
{
data.Tables["Process"].Row
}
catch(Exception e)
{
MessageBox.Show(e.Message)
}
return (int) row["ProcessID"];
}