insert value into database from datagridview control

Hi experts,
i'm using mysql database and
select notes_date as Date,notes_message as Notes from client_notes where client_id=?cid query to show data in datgridview control.

now i want to insert data into client_notes tabele from the datagrid view control
as i'am  novoice to c# , can anybody tell me how this can be done in a best way programatically

Thanks
APPIREDDYAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Priest04Commented:
If you want to use databinding mechanism provided by ADO.NET, bascally it goes like this:

1) you use DataAdapter to fill data to datatable

- create new datadapter, using theSELECT statemtent above mentioned-
MySqlDataAdapter da = new MySqlDataAdapter(SQLSelectString, myConnection);

- create new datatable
DataTable dt = new DataTable();

- use dataadapters Fill method to fill data to datatable
da.Fill(dt);

2) bind datagridview to this datatable
datahridview1.DataSource = dt;
3) after you have done editing datagridview, use dataadapters Update method to update data from tatatable to database
da.Update(dt);

Gpran
0
APPIREDDYAuthor Commented:
IF I DO LIKE THAT , I am getting runtime exception client_id doesn't have a default value.
because in my datagrid control I am only displaying two values and i'm taking client_id value from combobox
0
Priest04Commented:
Well, you should have then explained better what you want to achieve. You can try inserting recors manually, something like this:

MySqlCommand cmd = new MySqlCommand ("INSERT INTO MyTable (field, field2, field3) VALUES (" + value1 + ", " + value2 + ", " + value3 + ")", myConnection);

myConnection.Open();
cmd.ExecuteNonQuery();
myConnection.Close();

the above example assumes all the fields are of numeric type, if you have string column, then you would need to put value between ' '.
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

APPIREDDYAuthor Commented:
CAN YOU HELP ME ,HO TO ACCESS THE CURRENT COLUM VALUE FROM DATAGRID CONTROL
0
Priest04Commented:
No need for capital letters. :) There is a difference between datagridview and datagrid control, I gues you are talking about datagridview control, based on the title of the topic

// if you want to get string value of the first cell in first row
string temp = datagridview1[0, 0].Value.ToString();

// if you want to get integer value of the 2nd cell in fourth row
int temp = Convert.ToInt32(datagridview1[2, 4].Value);

0
Priest04Commented:
Ah, forgot to add how to get the current cell value

dg.CurrentCell.Value

if you want to know what is the column/row index of the currently selected cell

dg.CurrentCell.ColumnIndex
dg.CurrentCell.RowIndex
0
APPIREDDYAuthor Commented:
Thanks Priest04,

I have placed datagridview control on the form.

What i want to do is, when the user clicks add button,the datagridview control should  insert an extra row and the cursor should be focused on that row,so that user can enter the row

currently the datagridviewcontrol is showing 1 extra empty row,which i dont want to show
can you tell me how can i achieve this
0
Priest04Commented:
APPIREDDY, different question should have a new topic, Since now we are dealing with questions that are not directly corelated to inserting records into database, I suggest you to open a new topic for new questions in the future and close this one.

To answer your last question, there is a property called AllowUserToAddRows, which if set to True, automatically  adds one empty row at the bottom of the grid, and this row is used to add new records (usually used with databinding). You will set this property to false, and on the Click event for button, you will use

Goran
private void button1_Click(object sender, EventArgs e)
{
    // add new row
    dataGridView1.Rows.Add();
    // set focus to first column in last row
    dataGridView1.CurrentCell = dataGridView1[0, dataGridView1.RowCount - 1];last row
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
APPIREDDYAuthor Commented:
Thankyou
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.