Multiple gridview Rows Insert in Database at time - c#

In my below code give me this error
error is my gridview textbox column value not inserting in
my database parameter missing error
try {
da=DateTime.Now;
foreach(DataGridViewRow row in dataGridView1.Rows) {
string SelectedText=Convert.ToString((row.Cells["article_name"] as DataGridViewComboBoxCell).FormattedValue.ToString());
string SelectedText1=Convert.ToString((row.Cells["size_name"] as DataGridViewComboBoxCell).FormattedValue.ToString());
string SelectedText2=Convert.ToString((row.Cells["color_name"] as DataGridViewComboBoxCell).FormattedValue.ToString());
string SelectedText3=Convert.ToString((row.Cells["unit_name"] as DataGridViewComboBoxCell).FormattedValue.ToString());
SqlCommand comm=new SqlCommand("abc", con);
con.Open();
comm.Parameters.Add("#order_no", SqlDbType.NVarChar).Value="abc123";
comm.Parameters.Add("#article_name", SqlDbType.NVarChar).Value=SelectedText;
//dataGridView1.Rows[i].Cells["article_name"].Value;
comm.Parameters.Add("#size_name", SqlDbType.NVarChar).Value=SelectedText1;
comm.Parameters.Add("#color_name", SqlDbType.NVarChar).Value=SelectedText2;
comm.Parameters.Add("#quantity", SqlDbType.Int).Value=row.Cells["quantity"].Value;
comm.Parameters.Add("#piece_carton", SqlDbType.Int).Value=row.Cells["pcs_carton"].Value;
comm.Parameters.Add("#no_of_carton", SqlDbType.Int).Value=row.Cells["no_carton"].Value;
comm.Parameters.Add("#unit_name", SqlDbType.NVarChar).Value=SelectedText3;
comm.Parameters.Add("#rate", SqlDbType.NVarChar).Value=row.Cells["rate"].Value;
comm.Parameters.Add("#ship_qty", SqlDbType.Int).Value=row.Cells["ship_qty"].Value;
comm.Parameters.Add("#date1", SqlDbType.DateTime).Value=da;
comm.Parameters.Add("#amount", SqlDbType.NVarChar).Value=row.Cells["amount"].Value;
//comm.CommandText = StrQuery;
db.insertprocedure(comm);
}
}
catch(SqlException ex) {
MessageBox.Show(ex.Message);
}
finally {
con.Close();
}
Here Is My Query
create proc abc
#order_no varchar(50),
#article_name varchar(500),
#size_name varchar(100),
#color_name varchar(100),
#quantity int,
#piece_carton int,
#no_of_carton int,
#unit_name varchar(50),
#rate varchar(50),
#ship_qty int,
#date1 date,
#amount varchar(50)
as
begin
set #date1=CONVERT(varchar,#date1,1)
insert into transaction_order(order_id,article_id,size_id,color_id,quantity,piece_carton,no_of_carton,unit_id,rate,ship_qty,date1,amount)
values(#order_no,(select article_id from article_order where article_name=#article_name),(select size_id from size where size_name=#size_name),(select color_id from color where color_name=#color_name),#quantity,#piece_carton,#no_of_carton,(select unit_id from unit where unit_name=#unit_name),#rate,#ship_qty,#date1,#amount)
end
Here Is MY Function insertprocedure this function is in database class
public void insertprocedure(SqlCommand txt)
{
DateTime da = new DateTime();
try
{
da = DateTime.Now;
con.Open();
txt.CommandType = CommandType.StoredProcedure;
txt.ExecuteNonQuery();
con.Close();
}
catch (Exception ex) { throw ex; }
finally { con.Close(); }
}
> Here Is My Gridview TextBox Columns Added Code
private void Form1_Load(object sender, EventArgs e)
{
string query = "SELECT article_name FROM article_order";
DataTable dt = db.select_command(query);
BindingSource bi = new BindingSource();
bi.DataSource = dt;
string query1 = "SELECT size_name FROM size";
DataTable dt1 = db.select_command(query1);
BindingSource bi1 = new BindingSource();
bi1.DataSource = dt1;
string query2 = "SELECT color_name FROM color";
DataTable dt2 = db.select_command(query2);
BindingSource bi2 = new BindingSource();
bi2.DataSource = dt2;
string query3 = "SELECT unit_name FROM unit";
DataTable dt3 = db.select_command(query3);
BindingSource bi3 = new BindingSource();
bi3.DataSource = dt3;
//string query4 = "SELECT order_no FROM master_order";
//DataTable dt4 = db.select_command(query4);
//BindingSource bi4 = new BindingSource();
//bi4.DataSource = dt4;
#region Gridview Columns
//Transaction Id
DataGridViewTextBoxColumn trans_id = new DataGridViewTextBoxColumn();
trans_id.HeaderText = "ID";
trans_id.Width = 50;
trans_id.DataPropertyName = "ID";
trans_id.Name = "trans_id";
dataGridView1.Columns.Add(trans_id);
//Order No.
//DataGridViewTextBoxColumn order_no = new DataGridViewTextBoxColumn();
//order_no.HeaderText = "Order No";
//order_no.Width = 100;
////order_no.DataPropertyName = "yarn_name";
//order_no.Name = "order_no";
//dataGridView1.Columns.Add(order_no);
//query = "select yarn_name from yarn";
//auto.autocomplete1(dataGridView1., textBox9.AutoCompleteCustomSource = namesCollection, query);
//DataGridViewTextBoxColumn order_no = new DataGridViewTextBoxColumn();
//order_no.HeaderText = "Order No.";
//order_no.Width = 50;
//order_no.DataPropertyName = "order_no";
//order_no.Name = "order_no";
//dataGridView1.Columns.Add(order_no);
//Article Name
DataGridViewComboBoxColumn article_name = new DataGridViewComboBoxColumn();
article_name.DataPropertyName = "Article Name";
article_name.HeaderText = "Article Name";
article_name.Name = "article_name";
article_name.Width = 120;
article_name.DataSource = bi;
article_name.ValueMember = "article_name";
//article_name.DisplayMember = "article_name";
dataGridView1.Columns.Add(article_name);
//Size Name
DataGridViewComboBoxColumn size_name = new DataGridViewComboBoxColumn();
size_name.DataPropertyName = "Size Name";
size_name.HeaderText = "Size Name";
size_name.Name = "size_name";
size_name.Width = 120;
size_name.DataSource = bi1;
size_name.ValueMember = "size_name";
////size_name1.DisplayMember = "size_name";
dataGridView1.Columns.Add(size_name);
//Color Name
DataGridViewComboBoxColumn color_name = new DataGridViewComboBoxColumn();
color_name.DataPropertyName = "Color Name";
color_name.HeaderText = "Color Name";
color_name.Name = "color_name";
color_name.Width = 120;
color_name.DataSource = bi2;
color_name.ValueMember = "color_name";
//ColumnItem.DisplayMember = "ItemText";
dataGridView1.Columns.Add(color_name);
//quantity
DataGridViewTextBoxColumn quantity = new DataGridViewTextBoxColumn();
quantity.HeaderText = "Quantity";
quantity.Width = 100;
quantity.DataPropertyName = "Quantity";
quantity.Name = "quantity";
dataGridView1.Columns.Add(quantity);
//Pcs/Carton
DataGridViewTextBoxColumn pcs_carton = new DataGridViewTextBoxColumn();
pcs_carton.HeaderText = "Piece/Carton";
pcs_carton.Width = 100;
pcs_carton.DataPropertyName = "Piece/Carton";
pcs_carton.Name = "pcs_carton";
dataGridView1.Columns.Add(pcs_carton);
//No Of Carton
DataGridViewTextBoxColumn no_carton = new DataGridViewTextBoxColumn();
no_carton.HeaderText = "No Of Carton";
no_carton.Width = 70;
no_carton.DataPropertyName = "No Of Carton";
no_carton.Name = "no_carton";
dataGridView1.Columns.Add(no_carton);
//unit name
DataGridViewComboBoxColumn unit_name = new DataGridViewComboBoxColumn();
unit_name.DataPropertyName = "Unit Name";
unit_name.HeaderText = "Unit Name";
unit_name.Name = "unit_name";
unit_name.Width = 120;
unit_name.DataSource = bi3;
unit_name.ValueMember = "unit_name";
//ColumnItem.DisplayMember = "ItemText";
dataGridView1.Columns.Add(unit_name);
//rate
DataGridViewTextBoxColumn rate = new DataGridViewTextBoxColumn();
rate.HeaderText = "Rate";
rate.Width = 70;
rate.DataPropertyName = "Rate";
rate.Name = "rate";
dataGridView1.Columns.Add(rate);
//amount
DataGridViewTextBoxColumn amount = new DataGridViewTextBoxColumn();
amount.HeaderText = "Amount";
amount.Width = 70;
amount.DataPropertyName = "Amount";
amount.Name = "amount";
dataGridView1.Columns.Add(amount);
//Ship quantity
DataGridViewTextBoxColumn ship_qty = new DataGridViewTextBoxColumn();
ship_qty.HeaderText = "Ship Quantity";
ship_qty.Width = 70;
ship_qty.DataPropertyName = "Ship Quantity";
ship_qty.Name = "ship_qty";
dataGridView1.Columns.Add(ship_qty);
#endregion
}

values(#order_no,
(select article_id from article_order where article_name=#article_name),
(select size_id from size where size_name=#size_name),
(select color_id from color where color_name=#color_name),
#quantity,#piece_carton,#no_of_carton,
(select unit_id from unit where unit_name=#unit_name),
#rate,#ship_qty,#date1,#amount)
The error message would show up if any of the marked subselects in your procedure returns more than one value.

Can we see your select query? it seems that there is a subquery that returns more then 1 value

Run this query:
select article_name,COUNT(*) from article_order GROUP BY article_name HAVING COUNT(*)>1
If it returns ANY rows, then your problem is that you have multiple articles in your table with the same name.
Alternatively, make sure that you have a UNIQUE INDEX on each of article_name, color_name, unit_name, and size_name. If you get an error while trying to create that index (because one will definitely give you an error), then you need to fix that in the appropriate table, then create the index.

Related

C# MySql timeout when trying to Fill a datagridview rows of data

Greetings everyone iam facing this issue where i get an error of timeout(will post error below) when filling my datagridview with MYSQL table data (i have exactly 582 rows of data in it)
this is the error
Message=Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
and this is my code that iam using to populate the data from MySql to my datagridviewer
void populate()
{
Con.Open();
//int int1 = 1;
//int int2 = 216;
string Myquery = "SELECT * FROM SampleTable";
MySqlDataAdapter da = new MySqlDataAdapter(Myquery, Con);
MySqlCommand command = new MySqlCommand(Myquery, Con);
MySqlCommandBuilder builder = new MySqlCommandBuilder(da);
var ds = new DataSet();
da.Fill(ds);
ProductsGv.DataSource = ds.Tables[0];
DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
imageColumn = (DataGridViewImageColumn)ProductsGv.Columns[1];
for (int i = 0; i < ProductsGv.Columns.Count; i++)
{
if (ProductsGv.Columns[i] is DataGridViewImageColumn)
{
((DataGridViewImageColumn)ProductsGv.Columns[i]).ImageLayout = DataGridViewImageCellLayout.Stretch;
}
}
//imageColumn.ImageLayout = DataGridViewImageCellLayout.Normal;
ProductsGv.Columns[2].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
ProductsGv.Columns[3].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
ProductsGv.Columns[4].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
ProductsGv.Columns[5].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
ProductsGv.Columns[6].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
ProductsGv.Columns[7].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
ProductsGv.Columns[8].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
ProductsGv.Columns[9].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
ProductsGv.Columns[10].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
ProductsGv.Columns[11].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
ProductsGv.Columns[12].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
ProductsGv.Columns[13].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
ProductsGv.Columns[14].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
ProductsGv.Columns[15].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
Con.Close();
}

Show combobox item dynamically, depending on the record

I want to display a Comboxbox Item, depending on a data table.
for example:
1 = open
2 = close
....
The combobox content is created with the datatable:
string sqlStr = "select id, description from sdg.pa_status";
MySqlDataAdapter adapt = new MySqlDataAdapter();
MySqlCommand cmd = new MySqlCommand();
DataSet ds = new DataSet();
cmd = new MySqlCommand(sqlStr, conn);
adapt.SelectCommand = cmd;
adapt.Fill(ds);
**_status** = ds.Tables[0];
combox is created:
column.Name = "status";
column.DropDownWidth = 160;
column.Width = 160;
column.FlatStyle = 0;
column.HeaderText = "Status";
column.DataSource = **_status**;
column.DataPropertyName = dGVQuote.Columns["**stat**"].ToString();
column.ValueMember = _status.Columns[1].ToString();
column.DisplayMember = _status.Columns[1].ToString();
datagrid is generated:
private void CustomizeDataGridViewOrder()
{
dGVQuote.DataSource = GetQuote("");
dGVQuote.Columns["ID"].Width = 135;
dGVQuote.Columns["ID"].HeaderText = "Angebotsnummer";
dGVQuote.Columns["idorder"].Visible = false;
dGVQuote.Columns["Description"].Width = 225;
dGVQuote.Columns["Description"].HeaderText = "Beschreibung";
dGVQuote.Columns["comment"].Width = 225;
dGVQuote.Columns["comment"].HeaderText = "interner Kommentar";
dGVQuote.Columns["idcust"].Visible = false;
dGVQuote.Columns["idobj"].Visible = false;
dGVQuote.Columns["cnt"].Width = 60;
dGVQuote.Columns["cnt"].HeaderText = "Anzahl MA";
dGVQuote.Columns["**stat**"].Visible = true;
dGVQuote.Columns.Add(CreateComboBoxColumn());
dGVQuote.Columns["valid"].Width = 135;
dGVQuote.Columns["valid"].HeaderText = "gültig bis";
dGVQuote.DataSource = GetQuote(""); //datatable is handed over
I am desperate. Can someone help me?

datagridview column name C#

i am trying to set custom name for my column names in c# but when i load the form i dont get the names here is an example of my code:
string Konekcija = "datasource=localhost;port=3309;username=root;password=1234";
string Query = "select * from baza.artikli";
MySqlConnection konn = new MySqlConnection(Konekcija);
MySqlCommand komanda = new MySqlCommand(Query, konn);
MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
MyAdapter.SelectCommand = komanda;
DataTable dTable = new DataTable();
MyAdapter.Fill(dTable);
dataGridView1.DataSource = dTable;
dataGridView1.ColumnHeadersVisible = true;
// имиња на header ите
dataGridView1.AutoResizeColumnHeadersHeight();
dataGridView1.Columns[0].Name = "ид";
dataGridView1.Columns[1].Name = "Баркод";
dataGridView1.Columns[2].Name = "Назив";
dataGridView1.Columns[3].Name = "Набавна цена";
dataGridView1.Columns[4].Name = "Продажна цена";
dataGridView1.Columns[5].Name = "Данок";
dataGridView1.Columns[6].Name = "Количина";
dataGridView1.Columns[7].Name = "Опис";
dataGridView1.Columns[8].Name = "Македонски производ";
dataGridView1.Columns[9].Name = "Производител";
}
Maybe i am missing something but i cant seem to find what it is so if you now please le me know
Try this,
dataGridView1.Columns[index].HeaderText= "something";
Hope helps,

How to pass data from local variable to datagridview using c#

How do I pass value of my calculated result in datagridview. In the following code I filled the 2 columns of grid by the values fetched from database through DataTable, then I add a new column named "Text". Now I want to pass a value of my calculated hash in this column but there may b some syntax problem or some mistake which causes an error occurred. Following is my code;
SqlDataAdapter sda = new SqlDataAdapter("Select Image,Hash from UserInput where PINCODE = '" + txt_LPin.Text + "'",conn);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
int nOr = dataGridView1.Rows.Count;
if (nOr == 5)
{
LoginCluePoint lcp = new LoginCluePoint();
lcp.dgv_LCP.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
lcp.dgv_LCP.RowTemplate.Height = 101;
lcp.dgv_LCP.DataSource = dt;
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "Text";
int colIndex = lcp.dgv_LCP.Columns.Add(col);
lcp.dgv_LCP.Columns[2].Visible = false;
}
This problem is resolved by the following code
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "Text";
int colIndex = lcp.dgv_LCP.Columns.Add(col);
dgv_LCP.Rows[0].Cells[2].Value = hash
lcp.dgv_LCP.Columns[2].Visible = false;

Creating column Combobox in Datagridview

I am new to C#. I want to create combobox in first column of my Datagridview. Following is the routine I have wrote. But it is adding combo in last column after setting up my grid.
For setting up Grid, i have tried the below code:
private void SetGrid()
{
dgDetail.AutoGenerateColumns = false;
dgDetail.ColumnCount = 5;
dgDetail.Columns[0].Name = "Debit";
dgDetail.Columns[0].HeaderText = "Debit Account Name";
dgDetail.Columns[1].Name = "Bank";
dgDetail.Columns[1].HeaderText = "Bank";
dgDetail.Columns[2].Name = "ChqNo";
dgDetail.Columns[2].HeaderText = "CC/Chq No";
dgDetail.Columns[3].Name = "ChqDate";
dgDetail.Columns[3].HeaderText = "Chq Date";
dgDetail.Columns[4].Name = "Amount";
dgDetail.Columns[4].HeaderText = "Amount";
dgDetail.AllowUserToDeleteRows = true;
dgDetail.Columns[0].Width = 280;
dgDetail.Columns[1].Width = 160;
dgDetail.Columns[2].Width = 90;
dgDetail.Columns[3].Width = 90;
dgDetail.Columns[4].Width = 120;
dgDetail.RowsDefaultCellStyle.ForeColor = Color.Black;
dgDetail.RowsDefaultCellStyle.BackColor = Color.White;
dgDetail.Font = new Font("Arial", 9, FontStyle.Regular);
}
For Creating the combobox which is filled from DB.
private void FillGridCombo()
{
SqlConnection sqlConnection = new SqlConnection(strCon);
sqlConnection.Open();
try
{
string selectQueryStringMonth = "SELECT accode, GLAC FROM glmast where (actype = 'CSH' and titleac <> 'PDP') OR TITLEAC = 'DIS' ORDER BY GLAC";
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectQueryStringMonth, sqlConnection);
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
BindingSource bindingSourceMonth = new BindingSource();
bindingSource.DataSource = dataTable;
//Adding Combo
DataGridViewComboBoxColumn ColumnAcc = new DataGridViewComboBoxColumn();
ColumnAcc.DataPropertyName = "Debit Account Name";
ColumnAcc.HeaderText = "Debit Account Name";
ColumnAcc.Width = 280;
ColumnAcc.DataSource = bindingSourceMonth;
ColumnAcc.ValueMember = "accode";
ColumnAcc.DisplayMember = "GLAC";
dgDetail.Columns.Add(ColumnAcc);
dgDetail.DataSource = bindingSource;
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (sqlConnection.State != ConnectionState.Closed)
sqlConnection.Close();
}
}
I am calling both procedures on my NewData() like this.
private void NewData()
{
if (dgDetail.DataSource != null)
dgDetail.DataSource = null;
else
dgDetail.Rows.Clear();
ClearData();
CtrlEnable();
SetGrid();
FillGridCombo();
}
Help / Guide me to achieve this.,
use
dgDetail.Columns.Insert(0, ColumnAcc);
instead of
dgDetail.Columns.Add(ColumnAcc);
when you use Add it simply adds it as the last column, while using Insert you can choose where to add it to.
public virtual void Insert( int columnIndex, DataGridViewColumn
dataGridViewColumn )
note that the columnIndex is a zero-based index so 0 is the first column
You can use the Insert method instead of Add like this:
dgDetail.Columns.Insert(0,ColumnAcc);

Categories

Resources