Insert Data to Database - LINQ - c#

There is some data in my datagridview. I want to insert all that data at once into database from datagridview using LINQ. But, it couldn't be inserted.
Here is my code :
DetailTransaction dt = new DetailTransaction();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dt.TransactionID = labelID.Text;
dt.ProductID = dataGridView1.Rows[i].Cells[0].Value.ToString();
dt.Quantity = int.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString());
}
dc.DetailTransactions.InsertOnSubmit(dt);
dc.SubmitChanges();
Can anyone tell me what is the correct code?

You need to populate a collection of DetailTransaction and then use InsertAllOnSubmit like:
List<DetailTransaction > list = new List<DetailTransaction>();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DetailTransaction dt = new DetailTransaction();
dt.TransactionID = labelID.Text;
dt.ProductID = dataGridView1.Rows[i].Cells[0].Value.ToString();
dt.Quantity = int.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString());
list.Add(dt);
}
dc.DetailTransactions.InsertAllOnSubmit(list);
dc.SubmitChanges();
With your current code, you will end up with a single row inserted in the database and that row will be holding the records of the last row in dataGridView1

Related

Ms word automation table row problem using C#

What is the main reason of this error
No individual rows in this collection can be addressed because the
table contains vertically linked cells. '
I have table in which I want to add data (stored in array) but second row of the table is merged and I got this error message.
What can be appropriate solution to remove this error.
Code :
if (pCell.Range.Text.Contains("List of components robots"))
{
iDT16 = 0; rowcount = 0;
foreach (int Row in wordDocument.Tables[j].ToString()) // Add row in the table according to data available inside Result array
{
while (sDesignation_Componentsrobots[iDT16] != null)
{
wordDocument.Tables[j].Rows.Add();
wordDocument.Tables[j].Rows.SetHeight(28, Word.WdRowHeightRule.wdRowHeightAtLeast);
rowcount = wordDocument.Tables[j].Rows.Count;
iDT16++;
}
}
// Fill the row with data and add checkbox in particular table column.
iDT16 = 0; iEST16 = 0;
for (int row = 3; row <= rowcount; row++)
{
wTable.AllowAutoFit = true;
wTable.Rows[row].Range.Font.Bold = 0;
wTable.Rows[row].Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
wTable.Rows[row].Range.ParagraphFormat.SpaceBefore = 5;
wTable.Cell(row, 1).Range.Text = sDesignation_Componentsrobots[iDT16];
wTable.Cell(row, 3).Range.Text = sEmergency_stopcircuit_T16[iEST16];
Word.FormField checkBox2 = wTable.Cell(row, 6).Range.FormFields.Add(wTable.Cell(row, 6).Range, Word.WdFieldType.wdFieldFormCheckBox);
iDT16++; iEST16++;
}
break;
}
Table with merged row:

How to add two column value in one final amount?

I have datatable in c#, I want to add two column value in one, Add means 2+2 =4 like that. please help me this my below code not working
for (int i = 0; i < dtOrdersDetail.Rows.Count; i++)
{
DataRow dtItemRow = dtOrderReceipt.NewRow();
dtItemRow["FinalAmount"] = dtOrdersDetail.Rows[i]["ComboAmount"] + dtOrdersDetail.Rows[i]["TotalPrice"];
}
In this Case you have to correct the code in two place.
Cast the source data table Columns.
dtItemRow["FinalAmount"] = Convert.ToInt32(dtOrdersDetail.Rows[i]["ComboAmount"]) + Convert.ToInt32(dtOrdersDetail.Rows[i]["TotalPrice"]);
Add the row in data table.
dtOrderReceipt.Rows.Add(dtItemRow);
So in summary your code look likes as below
for (int i = 0; i < dtOrdersDetail.Rows.Count; i++)
{
DataRow dtItemRow = dtOrderReceipt.NewRow();
dtItemRow["FinalAmount"] = Convert.ToInt32(dtOrdersDetail.Rows[i]["ComboAmount"]) + Convert.ToInt32(dtOrdersDetail.Rows[i]["TotalPrice"]);
dtOrderReceipt.Rows.Add(dtItemRow);
}
#john, look at the below logic
DataTable table = new DataTable();
table.Columns.Add("TotalPrice", typeof(int));
table.Columns.Add("ComboAmount", typeof(int));
// Here we add five DataRows.
table.Rows.Add(10,10);
table.Rows.Add(20,20);
table.Rows.Add(30,20);
DataTable dtOrderReceipt = new DataTable();
dtOrderReceipt.Columns.Add("FinalAmount", typeof(int));
for (int i = 0; i < table.Rows.Count; i++)
{
DataRow dtItemRow = dtOrderReceipt.NewRow();
dtOrderReceipt.Rows.InsertAt(dtItemRow, i);
dtItemRow["FinalAmount"] = (int)table.Rows[i]["ComboAmount"] + (int)table.Rows[i]["TotalPrice"];
}
I think after create a newrow you forgot to Inject it on your Datatable. i.e.
DataRow dtItemRow = dtOrderReceipt.NewRow();
dtOrderReceipt.Rows.InsertAt(dtItemRow, i);
Let me know is that logic works for you?

Unable to move row from one datatable to another

I have asked a question previously based on the errors of this code. However, after the suggestions given, there is no more error. However, the data from the row in queue table would not move to the the missedQueue table.
I'm not sure why it won't work :(
this is my code:
DataSet queue = DBMgr.GetDataSet("SELECT * FROM queue");
DataTable missedQueue = queue.Tables[0].Clone();
DataRow dr = queue.Tables[0].NewRow();
for (int i = 0; i < queue.Tables[0].Columns.Count; i++)
{
dr[queue.Tables[0].Columns[i].ColumnName] = queue.Tables[0].Rows[0][i];
}
missedQueue.Rows.Add(dr.ItemArray);
}
Your DataRow should be of missedQueue table and add the row inside loop like
DataRow dr = null;
for (int i = 0; i < queue.Tables[0].Columns.Count; i++)
{
dr = missedQueue.NewRow();
dr[queue.Tables[0].Columns[i].ColumnName] = queue.Tables[0].Rows[0][i];
missedQueue.Rows.Add(dr);
}

Linq to Sql insert fail for inserting multiple rows from gridview to database

I'm using Linq to Sql for Database connection.I got 2 gridviews with CheckBoxRowSelect.The code below inserting just the last selected row,not all the records.My point was from first grid selecting 1 row from the second grid selecting 2 rows and i want to insert 2 times with first row value.Any suggestions?
LinqDataContext dtx=new LinqDataContext();
YETKI kul = new YETKI();
int[] firmasec = gridView2.GetSelectedRows();
int[] kullanıcısec=gridView1.GetSelectedRows();
for (int a = 0; a < gridView1.SelectedRowsCount; a++)
{
for (int i = 0; i < gridView2.SelectedRowsCount; i++)
{
kul.FIRMA_ROWID = int.Parse(gridView2.GetRowCellValue(firmasec[i], "ROWID").ToString());
kul.KULLANICI_ROWID = int.Parse(gridView1.GetRowCellValue(kullanıcısec[a], "ROWID").ToString());
kul.KULLANABILIR = 1;
}
dtx.YETKIs.FirstOrDefault();
dtx.YETKIs.InsertOnSubmit(kul);
dtx.SubmitChanges();
}
MessageBox.Show("Inserted!");

Empty DataGridView after adding values from DataTable with large number of columns

my DataTable has over 1000 columns and I want to display values on the datagridview. Because of the FillWeigth problem I use the following method to fill the gridview,
public bool TransferDataTableToGrid(DataGridView dataGrid, DataTable dataTable)
{
dataGrid.SuspendLayout();
if ((dataGrid != null) && (dataTable != null))
{
dataGrid.Columns.Clear();
dataGrid.AutoGenerateColumns = false;
dataGrid.DataSource = dataTable;
for (int i = 0; i < dataTable.Columns.Count; i++)
{
DataGridViewColumn column = new DataGridViewColumn();
column.Name = dataTable.Columns[i].ColumnName;
column.FillWeight = 1;
column.CellTemplate = new DataGridViewTextBoxCell();
column.ValueType = dataTable.Columns[i].DataType;
dataGrid.Columns.Add(column);
}
for (int i = 0; i < dataTable.Columns.Count; i++)
{
for (int ii = 0; ii < dataTable.Rows.Count; ii++)
{
dataGrid[i, ii].Value = dataTable.Rows[ii][i];
}
}
}
dataGrid.ResumeLayout();
return true;
}
and sometimes I have an effect that my gridview is empty. Only after second execution data is displayed. Do you have any ideas, why...?
Thanks.
I recommend to use paging, i mean that you can show about 20 columns with navigation buttons
under your grid, it's like Google or others... even your are not programming a web application.
Use binding source to fill your grid
SqlDataAdapter adapter = new SqlDataAdapter(database.cmd);
dataSet1.Tables.Clear();
adapter.Fill(dataSet1, "Table");
bs = new BindingSource();
bs.DataSource = dataSet1.Tables["Table"];
dataGridView1.DataSource = bs;
now you dont need to worry about creating columns and fill cells in loops and its much better performance
Bind Data to Datagridview
Well, I solved my problem. With Ivan's suggestion I tried the alternative way to fill data: instead of using DataSource I add new rows manually
foreach (DataRow row in dataTable.Rows)
{
var dataGridRow = new DataGridViewRow();
dataGridRow.CreateCells(dataGrid);
for (int i = 0; i < row.ItemArray.Length; i++)
{
dataGridRow.Cells[i].Value = row.ItemArray[i];
}
dataGrid.Rows.Add(dataGridRow);
}
...and it works - data in dgv is displayed. Thanks!

Categories

Resources