Link to home
Create AccountLog in
Avatar of Tim313
Tim313Flag for United States of America

asked on

Insert Into Statement

I have a form with multiple txt and ck boxes, some filled by query, others by user.

How do I refer to these boxes in the "values" of a Insert Into statement?
Avatar of nmarun
nmarun
Flag of India image

You probably have a 'Save' or an 'Update' button. In the click event of the button, you can do the following:

// note the usage of the single quotes;
 string insertString                                                              = @"
                                                                  insert into Categories
                                                                  (Col1, Col2, Col3)
                                                                  values ('" + txtField1.Text + "', " + chkBox1.Checked + ", '" + txtField2.Text + "')";
                                                             
                                                              // 1. Instantiate a new command                                                              with a query and connection
                                                              SqlCommand cmd =                                                              new SqlCommand(insertString, conn);
                                                             
                                                              // 2. Call ExecuteNonQuery to send                                                              command
                                                              cmd.ExecuteNonQuery();

For a better understanding please read:
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson01.aspx
looks ugly.. try this:

// note the usage of the single quotes;
string insertString = @"insert into tbl1 (Col1, Col2, Col3) values ('" + txtField1.Text + "', " + chkBox1.Checked + ", '" + txtField2.Text + "')";
 
// 1. Instantiate a new command with a query and connection
SqlCommand cmd = new SqlCommand(insertString, conn);
 
// 2. Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();

Open in new window

Avatar of Tim313

ASKER

nmarun,

Thank you for your reply.

I neglected to include that I'm using Visual Basic 2005 Express and MS Access 2003.

Can you help me again with the same question, based on the above?
Go throught this link for connecting to an MS Access database:
http://www.geekpedia.com/tutorial60_ADO-.NET-and-Access-database-I.html
Avatar of Tim313

ASKER

nmarun,

Thanks for the link, but I have my connection set-up. Just need to know if the ' " +  txtField.Text + " ' and the " + chkBox1.Checked  + " references are still applicable with VB2005 & MS Access 2003.
ASKER CERTIFIED SOLUTION
Avatar of nmarun
nmarun
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Tim313

ASKER

Thanks.