Insert Multiple Items From An ArraryList Into Db
Jan 4, 2008Hi
I am working on a site right now. Basicly how it works is they choose from a list of checkboxes what they want to be quized on. This all gets stored in an arrayList. Once they done that they have an option to save there settings so they don't have to go through the list and choose everything again if they want to come back and do that same quiz.
So I am trying to take all those options in the arrayList and put them into the database.
So I have 4 questions.
Question 1.
This is how I was thinking my database design should be.
This table that all this stuff is going to be inserted into is called QuickLinks. Originally I was thinking of having a PK to FK relationship with the Asp membership table called Membership(or User Table was another posibility). I am now thinking that is a waste of time and pointless. My first reasoning was well this way I can get the UserID from that table but now I realize no I actually need the UserID to be in the QuickLink table before I try to join the two tables together. With joining the 2 tables I don't get anything out of it since if I just do a check if the User is logged in before I insert the data into the database I can just slip the userID in there as well. Then the QuickLink table should have everything it needs.
So My question is I am thinking of having the quick Link talbe to operate like this. Say you have 50 items in the array(this is all from the same item). Each item will gets its own row that will have the UserID,CharacterName,CharacterImagePath, QuickLinkName plus the LinkID.
So when I need to call that information I just have to look at 2 pieces of information. Find all the rows that have the same QuickLink name and UserID. So is this a good way to store the data?
Question 2.
Currently right now I have UserID as unquieIdentifer but I am guessing if I have 50 rows with the same UserID it won't like that very much since its not Unquie. Should I just change that to a varChar?
LinkID UserID CharacterName CharacterImagePath
----------- ---------------------------------------------------------------------------------------------------- ------------- --------------------------------------------------
1 15bde69a-c6fe-4159-b60f-405a013ae4c3 hi image
2 15bde69a-c6fe-4159-b60f-405a013ae4e2 hi image
3 15bde69a-c6fe-4159-b60f-405a013ae4c3 hi image
Find it actually kinda weird that it allowed 2 of the same UserIDs in.
Question 3.
Right now my primary key is LinkID. I just made that up but right now I have no use for it since I can't think of anything that would be useful to be a primary key but I figured I need a primary key. So does anyone have any ideas if I should leave that as the primary key or not have one? Or what I should do?
Question 4
I been playing around on a new webpage I made so I don't have so much cludder code to get in the way I don't screw anything up(I just find it sometimes easier to make a watered down version of what I want and get that work before I tackle the big version and have no clue what is going on. I am not sure if this is the best approach). So right now I have text box that I type in the UserID into(I have gotten the part to work where it checks the login of the user and grabs it but I don't want use it for now since it might complicate things a bit).
I have then a textbox where you type in the image path and then 2 other textboxes that you type in the characterName. Those 2 textBoxes for the characterName gets stored in array(so it is almost like the real thing I want to do). I then tired to put my code in a for loop and tired to keep inserting it in until everything from the array was out.
However it does not work.
I get this error
The variable name '@UserID' has already been declared. Variable names must be unique within a query batch or stored procedure.
I am not sure what it means or wants.
My code.
ArrayList InsertCharcter = new ArrayList();
InsertCharcter.Add(TextBox1.Text);
InsertCharcter.Add(TextBox2.Text);
SqlCommand comm;
string holdInsert = "INSERT INTO QuickLinks (UserID, CharacterName, CharacterImagePath) VALUES (@UserID,@CharacterName,@CharacterImagePath)";
comm = new SqlCommand(holdInsert, getConnection());
Guid userID = new Guid(txtuserID.Text);
comm.Connection.Open();
for (int i = 0; i < InsertCharcter.Count; i++)
{
comm.Parameters.Add("@UserID", SqlDbType.VarChar);
comm.Parameters["@UserID"].Value = txtuserID.Text;
comm.Parameters.Add("@CharacterName", SqlDbType.VarChar);
comm.Parameters["@CharacterName"].Value = InsertCharcter[i].ToString();
comm.Parameters.Add("@CharacterImagePath", SqlDbType.VarChar);
comm.Parameters["@CharacterImagePath"].Value = txtImage.Text;
comm.ExecuteNonQuery();
}
comm.Connection.Close();
Thanks