Solved

Insert into a SQL server table from a datatable

Posted on 2011-03-11
2
756 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Title # Comments Views Activity
Winform Module - What is the ASP.Net equiv 2 28
Winforms -- Capture CTRL+SHIFT+D at the form not working? 3 25
MailAddress in vb 4 29
IEnumerable<T> to a List<T> 8 37
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

840 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