Try replacing 'while (newId == null)' with 'while (newId == string.Empty)'.
Conversion broblem from vb net to C#
John Bairlis
21
Reputation points
Hi Gays
I have made an application with vb net.
I want to pass in C# and i convert it.
All is good except that code who add invoice data with details to database.
With vb net invoice details adds ID normally but in C# adds ID only in the first row not in the others.
example:
ID PRODUCT QUANTITY
001 TABLE 2
DOOR 2
Here is th vb net code
Public Function Execute(inumber As String, supplier As String, createDate As Date,
details As IEnumerable(Of CreateInvoiceDetailModel)) As String _
Implements ICreateInvoiceFactory.Execute
Try
Dim invoices = _invoiceRepository.GetAll().ToList()
Dim defaultId = 0
Dim newId = String.Empty
If invoices.Count = 0 Then
newId = "IN" + 1.ToString("000000")
Else
Do While newId = Nothing
defaultId += 1
Dim newIdCheck = "IN" + defaultId.ToString("000000")
Dim q = True
For Each unit As Invoice In invoices
If (unit.Id = newIdCheck) Then
q = False
Exit For
End If
Next
If q Then newId = newIdCheck
Loop
End If
Dim newInvoice = New Invoice() With {.Id = newId, .Inumber = inumber, .Supplier = supplier, .CreateDate = createDate}
newInvoice.InvoiceDetails = New List(Of InvoiceDetail)
Dim defaultDetailId = 1
Dim qu = True
Dim invoiceDetailsList = _invoiceRepository.GetInvoiceDetails()
Dim idList = New List(Of String)
For i = 0 To details.Count - 1
Dim newDetailId = String.Empty
If invoiceDetailsList.Count = 0 And qu Then
idList.Add(1.ToString("0000000000"))
qu = False
Else
Do While newDetailId = Nothing
defaultDetailId += 1
Dim newIdCheck = defaultDetailId.ToString("0000000000")
Dim q = True
For Each unit As InvoiceDetail In invoiceDetailsList
If unit.Id = newIdCheck Then
q = False
Exit For
End If
Next
If q Then newDetailId = newIdCheck
Loop
idList.Add(newDetailId)
End If
Next
Dim z = -1
For Each detailModel As CreateInvoiceDetailModel In details
z += 1
Dim portion = _portionRepository.GetOne(detailModel.PortionId)
newInvoice.InvoiceDetails.Add(New InvoiceDetail() With {.Id = idList(z), .Portion = portion,
.Measure = detailModel.Measure, .Weight = detailModel.Weight,
.Quantity = detailModel.Quantity, .Total = detailModel.Total})
Next
_invoiceRepository.Add(newInvoice)
_unitOfWork.Commit()
Return newId
Catch
Return String.Empty
End Try
End Function
Here is C# code
public string Execute(string inumber, string supplier, DateTime createDate,
IEnumerable<CreateInvoiceDetailModel> details)
{
try
{
// auto-create new invoice id
var invoices = _invoiceRepository.GetAll().ToList();
var defaultId = 0;
var newId = string.Empty;
if (invoices.Count == 0)
{
newId = "IN" + 1.ToString("000000");
}
else
while (newId == null)
{
defaultId += 1;
var newIdCheck = "IN" + defaultId.ToString("000000");
var q = true;
foreach (Invoice unit in invoices)
{
if (unit.Id == newIdCheck)
{
q = false;
break;
}
}
if (q)
{
newId = newIdCheck;
}
}
var newInvoice = new Invoice() { Id = newId, Inumber = inumber, Supplier = supplier, CreateDate = createDate };
// add invoice details of invoice
newInvoice.InvoiceDetails = new List<InvoiceDetail>();
// get new invoice detail list id
var defaultDetailId = 1;
var qu = true;
var invoiceDetailsList = _invoiceRepository.GetInvoiceDetails();
var idList = new List<string>();
for (var i = 0; i <= details.Count() - 1; i++)
{
var newDetailId = string.Empty;
if (invoiceDetailsList.Count == 0 & qu)
{
idList.Add(1.ToString("0000000000"));
qu = false;
}
else
{
while (newDetailId == null)
{
defaultDetailId += 1;
var newIdCheck = defaultDetailId.ToString("0000000000");
var q = true;
foreach (InvoiceDetail unit in invoiceDetailsList)
{
if (unit.Id == newIdCheck)
{
q = false;
break;
}
}
if (q)
{
newDetailId = newIdCheck;
}
}
idList.Add(newDetailId);
}
}
// create new invoice
var z = -1;
foreach (CreateInvoiceDetailModel detailModel in details)
{
z += 1;
// get the product
var portion = _portionRepository.GetOne(detailModel.PortionId);
// add invoice detail to invoice
newInvoice.InvoiceDetails.Add(new InvoiceDetail() { Id = idList[z], Portion = portion,
Measure = detailModel.Measure,
Weight = detailModel.Weight,
Quantity = detailModel.Quantity,
TotalQuantity = detailModel.TotalQuantity });
}
// add new invoice to database
_invoiceRepository.Add(newInvoice);
_unitOfWork.Commit();
return newId;
}
catch
{
return string.Empty;
}
}
Thanks in advance
SQL Server | Other
14,499 questions
Developer technologies | C#
11,578 questions