Solved

C# syntax with insert

Posted on 2012-04-09
5
286 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

738 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