Solved

C# syntax with insert

Posted on 2012-04-09
5
287 Views
Last Modified: 2012-04-09
I need to insert records into a table from a girdview. I have an asp.net page (c# code behind) that has 2 gridviews. You move selected records from the one on the left to the one on the right. Once you have the list final, you can select Save and have the records save to a table. The table is in a simple Access database. Right now I have it working as far as inserting the ID into the table, but I'm having trouble with the syntax in inserting further fields. Below is my code so far:

protectec void btnSave_Click(object sender, EventArgs e)
{
cn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
DataTable dt  = (DataTable)ViewState["SelectedRecords"];
foreach (DataRow dr in dt.Rows)
{
cmd.CommandText = "insert into MyList(ID)values('" + dr["ID"] + "');
cmd.ExecuteNonQuery();
}
cn.Close()
}
I need to add as part of the insert another field, probably the username. I'm grabbing the username as a session varible elsewhere. I want to insert that along with the ID. But the syntax keeps messing me up. Any help is appreciated.
0
Comment
Question by:dodgerfan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 17

Expert Comment

by:ramrom
ID: 37823921
Add a quote at the end to fix the syntax:
cmd.CommandText = "insert into MyList(ID)values('" + dr["ID"] + "')";

Open in new window

0
 

Author Comment

by:dodgerfan
ID: 37823950
yes, that was a typo. I can't cut and paste. The code reads:
cmd.CommandText = "insert into MyList(ID)values('" + dr["ID"] + "')";

Now I need to add another field to be inserted, like user (which will be a session variable):

cmd.CommandText = "insert into MyList(ID, user)values('" + dr["ID"] + "', user);

That's the syntax I can't seem to get working.
0
 
LVL 17

Accepted Solution

by:
ramrom earned 500 total points
ID: 37823975
To add another field:
cmd.CommandText = "insert into MyList(ID, username)values('" + dr["ID"] + "', '" + dr["username"] + "')";

Open in new window


of course replace username with the correct column name and response key.
0
 

Author Closing Comment

by:dodgerfan
ID: 37824022
That's it, thanks for the help.
0
 
LVL 17

Expert Comment

by:ramrom
ID: 37824439
I have often struggled with keeping track of quotes and apostrophes. I rely in part on a good editor that lets me know when things are not balanced.

That is what helped me spot your syntax error immediately.

I also have lots of experience with expressions that build things like SQL.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question