Solved

C# syntax with insert

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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!
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

820 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