Solved

Insert into a SQL server table from a datatable

Posted on 2011-03-11
2
750 Views
Last Modified: 2012-06-27
I'm trying to insert each row from a datatable into a sql server table. I can;t seem to get the syntax right in trying to get it to work. My code is below. I;m not sure how to tell it how to get the values from certain feilds in the datatable.

My code now looks like this:
protected void Import_Click(object sender, EventArgs e)

string strConn = "data source=(local);Inital Catalog="Testing";Integrated Security=true;);
string strSQL = "Select * from view3";
string password = "Password";

Sql Connection my Connection = new SQLConnection(strConn);

myConnection.Open();
SqlDataAdapter da = new SqlDataAdapter(strSQL, myConnection);
DataTable dt = new DataTable();
da.Fill(dt);

foreach (DataRow row in dt.Rows)
{
      MembershipUser newUser = Membership.CreateUser("username", password, "email");
}

Just running the line:  MembershipUser newUser = Membership.CreateUser("username", password, "email"); works in and of itself. What I want to do is run that line for each row in my datatable. The datatable data has two fields for each user, username and email. I'm setting the same password for everyone initially. What is the syntax for using the columns for each row in the datatable instead of the variables in there now?
0
Comment
Question by:dodgerfan
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35113653
It should be:
foreach (DataRow row in dt.Rows)
{
      MembershipUser newUser = Membership.CreateUser(row["Username"].ToString(), password, row["email"].ToString());
}

Open in new window

0
 

Author Closing Comment

by:dodgerfan
ID: 35114515
Works perfectly. Thanks.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

760 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

22 Experts available now in Live!

Get 1:1 Help Now