Solved

C# syntax with insert

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

867 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

24 Experts available now in Live!

Get 1:1 Help Now