Hii guys, i've made simple windows form application to create new account records.
Here is the snippet code that i use to create account:
private void btnUpload_Click(object sender, EventArgs e) { Stopwatch watch = new Stopwatch(); watch.Start(); if (textBox1.Text != string.Empty || textBox1.Text != "") { string FileName = Path.GetFileName(textBox1.Text); string Extension = Path.GetExtension(textBox1.Text); DataTable dt = ImportData(textBox1.Text, Extension); if (dt.Rows.Count > 0) { progressBar1.Minimum = 0; progressBar1.Maximum = dt.Rows.Count - 1; for (int i = 1; i < dt.Rows.Count; i++) { try { Entity entity = new Entity("account"); string guid = dt.Rows[i][0].ToString(); entity["accountid"] = new Guid(guid); entity["name"] = dt.Rows[i][1].ToString(); entity["telephone1"] = dt.Rows[i][2].ToString(); entity["websiteurl"] = dt.Rows[i][3].ToString(); entity["fax"] = dt.Rows[i][4].ToString(); var multiplerequest = new ExecuteMultipleRequest() { Settings = new ExecuteMultipleSettings() { ContinueOnError = false, ReturnResponses = true }, Requests = new OrganizationRequestCollection() }; UpdateRequest update = new UpdateRequest { Target = entity }; multiplerequest.Requests.Add(update); ExecuteMultipleResponse exec = (ExecuteMultipleResponse)service.Execute(multiplerequest); progressBar1.Value = i; } catch (Exception ex) { MessageBox.Show(ex.Message); } } watch.Stop(); TimeSpan ts = watch.Elapsed; MessageBox.Show(dt.Rows.Count + " Records Has Benn Created/Updated, total time consumed is " + ts); }
Those code run well. Now i try to create windows form for update, but i can't get the logic here. I read on some article, that for update we need to match accountid that is the primary key of the account entity with the guid on the excel sheet. (I'm sure that i have guid in my excel sheet)
and change the : Createrequest to Updaterequest. But it throw an error which says: "unrecognized guid format".
Could you show me the correct ways to update entity ..
Thanks :)
EDITED
I was publish unclear question, sorry for that. This is my Windowsform layout:
I import excel sheet through the solution and create new account in ms crm. But i got error when trying to edit this windows form so it can run for update account. Thanks
EDITED
I full fill the upload button code :
And this is my excel sheet:
And this is the "ImportData" full code (i mean this : DataTable dt = ImportData(textBox1.Text, Extension);)
string connString = ""; DataTable dt = new DataTable(); switch (Extension) { case ".xls": connString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString; break; case ".xlsx": connString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString; break; } connString = string.Format(connString, Filepath, 1); try { OleDbConnection connect = new OleDbConnection(connString); OleDbCommand command = new OleDbCommand(); OleDbDataAdapter oda = new OleDbDataAdapter(); command.Connection = connect; //Get Firstsheet Name connect.Open(); DataTable dtExcelSchema; dtExcelSchema = connect.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); connect.Close(); //Read data from first connect.Open(); command.CommandText = "Select * from [" + SheetName + "]"; oda.SelectCommand = command; oda.Fill(dt); connect.Close(); } catch (Exception ex) { MessageBox.Show("Error while trying upload data !! "+ex, "ERROR" ,MessageBoxButtons.OK, MessageBoxIcon.Error); } return dt;