I'm probably going about this the wrong way, but here goes:
I am starting a club database in SQL server that includes ASP login functionality. I have been given all the members' details in a spreadsheet and am trying to import every Club member into the database with their own login.
I have imported the spreadsheet to a table called Historical$ and have used Peter Kellner's Membership Editor to set up the ASP Membership tables in my database. Works great!
Therefore I have all my club "members" as ASP Membership with their own login details but now want to put all their info (address, gender, etc) in a table that uses the UniqueIdentifier "UserId" from the ASP Membership tables as a foreign key.
In short, I want to import selected fields from my source table (Historical$) + the corresponding UserId from aspnet_Membership table into an empty table called "Club_Member". I would prefer to use the structure that I already have in Club_member if possible.
Source table Historical$:
Mem_no
Firstname
Lastname
email1
...
Source table aspnet_Membership:
...
UserId
...
Email
...
Target table Club_Member:
UserId
Firstname
Lastname
Email
...
I thought that the best way would be to use a SQLDataReader to select the fields with a table join, read each row in the results and insert into the new table, but having trouble working out the logic and syntax to insert into the new table.
C# Code:
protected void ButtonTransfer_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connectionString);
string strSQL1 = "Select Historical$.memnum, Historical$.first, Historical$.last, aspnet_Membership.UserId FROM aspnet_Membership INNER JOIN Historical$ ON aspnet_Membership.Email = Historical$.email1";
con.Open();
SqlCommand myCommand = new SqlCommand(strSQL1, con);
SqlDataReader TransReader;
TransReader= myCommand.ExecuteReader();
while (TransReader.Read())
{
SqlDataSource1.Insert();
}
}
ASP Code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:wtcConnectionString %>"
InsertCommand="INSERT INTO member(UserId, mem_number, mem_fname, mem_lname) VALUES (@UserId, @memnum, @first, @last)">
<InsertParameters>
<asp:Parameter Name="UserId" />
<asp:Parameter Name="memnum" />
<asp:Parameter Name="first" />
<asp:Parameter Name="last" />
</InsertParameters>
</asp:SqlDataSource>
I am getting the error: Cannot insert the value NULL into column 'UserId', table Club_Member; column does not allow nulls. INSERT fails.
First question is, Am I on the right track, or is there a better way?
Secondly, What might be causing the error?
I don't mind if someone comes up with an easier way because this is supposed to be a "one-off" but I don't get how to go about it! (BTW ASP.NET/C# knowledge limited, SQL knowledge very limited!)
Thanks, and sorry about the essay.