Solved

C# syntax with insert

Posted on 2012-04-09
5
275 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
  • 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now