C sharp Multiple Data insert

I want to insert all data rows of datagrid into data table...Help Tnks

Page 1 of 1

6 Replies - 2111 Views - Last Post: 01 November 2010 - 07:06 AM Rate Topic: -----

#1 ni3b   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-October 10

C sharp Multiple Data insert

Posted 28 October 2010 - 11:39 PM

I am creating a application for billing. When user selects multiple items for bill. All itmes are shown in datagrid view . I want to store all items in Database when user clicks on save button. Thnks
Is This A Good Question/Topic? 0
  • +

Replies To: C sharp Multiple Data insert

#2 mavarazo   User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: C sharp Multiple Data insert

Posted 29 October 2010 - 12:23 AM

And what is your question?
Was This Post Helpful? 0
  • +
  • -

#3 Chronicade   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 05-September 09

Re: C sharp Multiple Data insert

Posted 29 October 2010 - 12:37 AM

There is a bunch of choices for a database, I would just create a text file in a folder with number names and inside contains all data, then a function to load all text file in that folder and organize their data properly in the datagrid.
Was This Post Helpful? 0
  • +
  • -

#4 mavarazo   User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: C sharp Multiple Data insert

Posted 29 October 2010 - 01:57 AM

First of all, we are in the Year 2010, so we don't use Textfile to store data :).

If you would like to make it simple, use XML.

You have to decide how your structure in the XML should be, depends on your needs.
Be creative :), there are so much ways to do that.

Here an exmpale:
Users -> User -> Bills -> Bill
<xml ...>
<billings>
	<user name="Bob">
		<bills>
			<bill id="001">
				<pos id="001">
					<product>Fish</product>
					<price>10.00</price>
				</pos>
				<pos id="002">
					<product>Chips</product>
					<price>5.00</price>
				</pos>
			</bill>
			<bill id="002">
				<pos id="001">
					<product>Meet</product>
					<price>5.00</price>
				</pos>
				<pos id="002">
					<product>Fries</product>
					<price>5.00</price>
				</pos>
			</bill>
		</bills>
	</user>
	<user name="Puppy">
		<bills>
			<bill id="001">
				...
			</bill>
		</bills>
	</user>
</billings>
</xml>

Was This Post Helpful? 3
  • +
  • -

#5 Vishu Sukhdev   User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: C sharp Multiple Data insert

Posted 01 November 2010 - 06:22 AM

u cn try this n store data in database instead textfiles
  

int iRowCount=DataGridView1.Rows.Count;

for(int i=0; i<iRowCount;i++)
{
sSQL="INSERT INTO tablename(";
sSQL += "Field1,Field2)";
sSQL += " VALUES('";
sSQL += DataGridView1.Rows[i].Cell["gridfieldname1"].FormatedValue.ToString()+"','";
sSQL += DataGridView1.Rows[i].Cell["gridfieldname2"].FormatedValue.ToString()+"')";

//Then Execute Ur Query Here
//
//
}


This post has been edited by macosxnerd101: 01 November 2010 - 06:25 AM
Reason for edit:: Please use code tags when posting your code.

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C sharp Multiple Data insert

Posted 01 November 2010 - 06:33 AM

Vishu, that's an ugly way to do that. First, if you're using a DataGridView, it would be very easy to use a SqlDataAdapter to do all the updates/inserts/deletes automatically.

Second, even if you did decide to do it like this, you certainly don't want to use string concatenation to build your SQL statement. You want to use Parameters, to avoid SQL Injection attacks, and also to actually be able to read your own code.
Was This Post Helpful? 2
  • +
  • -

#7 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: C sharp Multiple Data insert

Posted 01 November 2010 - 07:06 AM

Not to mention using properties that actually exist, unlike FormatedValue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1