Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I created one form called NewInvoice and the form contains 3 textBoxes (InvoiceID, Date and Total), one dataGridView and save button. My problem is how can I save at once textBoxes and the dataGridView that contains multiple rows.

----------
InvoiceID
----------
----------
Date
----------

--------------------------------------------------------------------

dataGridView


---------------------------------------------------------------------
-------
Total
-------

SAVE(Button)

Any Idea? :confused:

Thanks in Advance,
Zabamkd
Posted
Updated 2-Dec-09 0:38am
v2

This question makes no sense at all. Are you saving the grid, or does it show the values ? Where are you saving them ? What have you tried ? What problems are you facing ? Please edit your question to reflect this information and then perhaps we can try to help you.
 
Share this answer
 
The question make sense:) . Like before my problem is to save 3 textboxes and one dataGridView that can contain several rows. I made 2 tables in the database called tableHead(InvoiceID, Date and Total) and tableBody (ProductID, InvoiceID, ProductName, Quantity and Price) the tables are in relationship => tableHead.InvoiceID = tableBody.InvoiceID. I am using sql database and language c# for the application.

I try to program some code but I still can not save the form. The code is:


private void SaveButton_Click(object sender, EventArgs e)
{

SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = @"Data Source=IVAN-PC;Initial Catalog=model;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();

//Head textbox without InvoiceID because is increment...
string head = "INSERT INTO tableHead (Date) values('" + dateTextBox.Text +"'";

//InvoiceID select...
string Invoice = "SELECT InvoiceID FROM tableHead";

//Populate datagridview without ProductID...
//Maybe I have to create for or something else to correctly populate row by row...
string InvoiceID = this.DataGridView.CurrentRow.Cells[imeIndex].Value.ToString().Trim();
string ProductName = this.DataGridView.CurrentRow.Cells[imeIndex].Value.ToString().Trim();
string Quantity = this.DataGridView.CurrentRow.Cells[imeIndex].Value.ToString().Trim();
string Price = this.DataGridView.CurrentRow.Cells[imeIndex].Value.ToString().Trim();

string body = "INSERT INTO tableBody(InvoiceID, ProductName, Quantity, Price) values ('" + InvoiceID + "','" + ProductName + "','" + Quantity + "','" + Proce + "')";

cmd.CommandText = head;
cmd2.CommandText = Body;
cmd.Connection = cnn;
cnn.Open();
cmd = new SqlCommand(head, cnn);
cmd = new SqlCommand(Body, cnn);
cmd.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
cnn.Close();

if (MessageBox.Show("Successfully saved! ", "Saving", MessageBoxButtons.OK) == DialogResult.OK)
{
this.Close();
}
}

Can someone Help?

Thanks in advance,
zabamkd
 
Share this answer
 
Comments
Member 11698375 12-Jan-18 0:28am    
there is no problem to save even in one table if you want to save only one line of datagridview with id date etc.......
=======================================
string col1 = dataGridViewSale[0, dataGridViewSale.CurrentCell.RowIndex].Value.ToString();
string col2 = dataGridViewSale[1, dataGridViewSale.CurrentCell.RowIndex].Value.ToString();
string col3 = dataGridViewSale[2, dataGridViewSale.CurrentCell.RowIndex].Value.ToString();
string col4 = dataGridViewSale[3, dataGridViewSale.CurrentCell.RowIndex].Value.ToString();

conn.Open();
string query1 = "INSERT INTO Sale(ItemName,Qty,Rate,Date,nvoiceID, ,Total)VALUES('" + col1 + "','" + col2 + "','" + col3 + "', '" + dateTimeSale.Text + "', '" + txtInvoiceId.Text + "' '" + labelTotal.Text + "')";
SqlDataAdapter sda1 = new SqlDataAdapter(query1, conn);


sda1.SelectCommand.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Saved");
--------------------------------------------
above code is to save textboxes and label of total amount with a single line of datagridview. to save multiple line of datagridview use for loop.
Hi Zabamkd,
If you have a screen with three textboxes and also a grid with multiple rows you are probably saving multiple records to multiple tables in your database. You probably want to look at using a Data Adapter object to do multiple records at a time. You could try googling [^]or searching CP for articles to help you. Here are some that I found that you may find helpful:
Inserting relational data using DataSet and DataAdapter[^]
Building database application with ADO.NET for a beginner.[^]

Also, about the code you posted. You said that it didn't work. Please let us know what error message you get or what is happening that is incorrect and we can try to help you more from there.

Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900