Jump to content

C#/SQL - Inserting Multiple Rows into databse

- - - - -

  • Please log in to reply
1 reply to this topic

#1
pi1983

pi1983

    Newbie

  • Members
  • Pip
  • 1 posts
Hey all, I'm having a hard time understanding this so I appreciate your support in baring with me as I try to explain it.

I'm developing a CMS for my media site and I'm trying to work out some bugs with the "Add Artist" page. I have two tables in my DB, one is Artists which has "ID" "DateCreated" "Name" and the other is ArtistsItems which has "ID" "ArtistID" "Name" "Value"

When I go to my "Add Artist" page in my CMS I have a field for "Name" which inserts into Artists "Name". There is also a field that inserts Information into the ArtistsItems "Value". So when I click "Save" it inserts the artist name into the Artists table and inserts any information I entered into the ArtistsItems table.

What I want to do is make it so that it inserts more than just "Information" into the ArtistsItems table. Here's my c#... Sorry if this is all convoluted, I'm pretty stuck at this point.

protected void btnSave_Click(object sender, ImageClickEventArgs e)
{
Artist page = null;
ArtistsItem item = null;

try
{
if (Session["Mode"].ToString() == "Add")
{
List<Artist> pageList = new List<Artist>();

int maxPosition = 0;

if (pageList.Count > 0)
{
maxPosition = pageList.Max(p => p.Ordering);
}

page = PenguinDataContext.Instance.Artists.CreateNew();
item = PenguinDataContext.Instance.ArtistItems.CreateNew();

page.Name = txtName.Text;
txtName.Text = "";
page.DateCreated = DateTime.Now;

page.Save();

if (fckEntryContent.Visible)
{
item.Name = "Information";
item.ArtistID = page.ID;
item.Value = fckEntryContent.Value;

item.Save();
}

if (txtMembers.Text == "")
{
item.Name = "Members";
item.ArtistID = page.ID;
item.Value = "Coming Soon";

item.Save();
}
else
{
item.Name = "Members";
item.ArtistID = page.ID;
item.Value = txtMembers.Text;

item.Save();
}

if (txtWebsite.Text == "")
{
item.Name = "Website";
item.ArtistID = page.ID;
item.Value = "NA";

item.Save();
}
else
{
item.Name = "Website";
item.ArtistID = page.ID;
item.Value = txtWebsite.Text;

item.Save();
}

item.Name = "Image";
item.ArtistID = page.ID;
item.Value = "placeholder_med";

item.Name = "ImageThumb";
item.ArtistID = page.ID;
item.Value = "placeholder_sm";
}
else
{
page = PenguinDataContext.Instance.Artists.GetByID(Convert.ToInt64(Session["ArtistID"]));
item = PenguinDataContext.Instance.ArtistItems.GetByID(Convert.ToInt64(Session["ArtistItemID"]));

page.Name = txtName.Text;

page.Save();

if (fckEntryContent.Visible)
{
item.ArtistID = page.ID;
item.Value = fckEntryContent.Value;

item.Save();
}
}

}
catch (Exception ex)
{
return;
}

if (Session["Mode"].ToString() == "Add")
{
pagingArtists.GetPagingController<Artist>().DataSource = PenguinDataContext.Instance.Artists.GetAllArtists().ToList<Artist>();
pagingArtists.GetPagingController<Artist>().CurrentPageIndex = pagingArtists.GetPagingController<Artist>().PageCount;
}
else
{
int cp = pagingArtists.GetPagingController<Artist>().CurrentPageIndex;

if (cp > pagingArtists.GetPagingController<Artist>().PageCount)
cp = pagingArtists.GetPagingController<Artist>().PageCount;

pagingArtists.GetPagingController<Artist>().CurrentPageIndex = cp;
}

pagingArtists.Update();
LoadPagesList();
mvwManager.ActiveViewIndex = 0;
}

Note how I keep saying "item.Save();" hoping it'll add another row? It was only adding the "Information" part, now it's only adding the "Website" info.

God I hope this makes sense to someone.

Thanks for your time and support.

#2
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
Hey, this typically, is not how I work with databases, but I think your problem is that you've only generated one item. Like at the beginning, you have this.

page = PenguinDataContext.Instance.Artists.CreateNew();
item = PenguinDataContext.Instance.ArtistItems.CreateNew( );

which is the only time you're creating records.

after that, your just repeatedly setting values to item, and hitting item.Save()

so, my guess, is that after you create it once and save it, thats an INSERT operation, and after that, it's an UPDATE.

changing the code to something like this:



protected void btnSave_Click(object sender, ImageClickEventArgs e) {

	Artist page = null;

	ArtistsItem item = null;


	try {

		if (Session["Mode"].ToString() == "Add") {

			List<Artist> pageList = new List<Artist>();

			int maxPosition = 0;


			if (pageList.Count > 0) {

				maxPosition = pageList.Max(p => p.Ordering);

			}


			page = PenguinDataContext.Instance.Artists.CreateNew();

			


			page.Name = txtName.Text;

			txtName.Text = "";

			page.DateCreated = DateTime.Now;


			page.Save();


			if (fckEntryContent.Visible) {

				[COLOR="red"]item = PenguinDataContext.Instance.ArtistItems.CreateNew( );[/COLOR]

				item.Name = "Information";

				item.ArtistID = page.ID;

				item.Value = fckEntryContent.Value;


				item.Save();

			}


			if (txtMembers.Text == "") {

				[COLOR="red"]item = PenguinDataContext.Instance.ArtistItems.CreateNew( );[/COLOR]

				item.Name = "Members";

				item.ArtistID = page.ID;

				item.Value = "Coming Soon";


				item.Save();

			} else {

				[COLOR="red"]item = PenguinDataContext.Instance.ArtistItems.CreateNew( );[/COLOR]

				item.Name = "Members";

				item.ArtistID = page.ID;	

				item.Value = txtMembers.Text;


				item.Save();

			}


			if (txtWebsite.Text == "") {

				[COLOR="red"]item = PenguinDataContext.Instance.ArtistItems.CreateNew( );[/COLOR]

				item.Name = "Website";

				item.ArtistID = page.ID;

				item.Value = "NA";


				item.Save();

			} else {

				[COLOR="red"]item = PenguinDataContext.Instance.ArtistItems.CreateNew( );[/COLOR]

				item.Name = "Website";

				item.ArtistID = page.ID;

				item.Value = txtWebsite.Text;


				item.Save();

			}


			[COLOR="orange"]//No save is called here for these two items (maybe that's intentional, or a result of your testing)[/COLOR]


			[COLOR="red"]item = PenguinDataContext.Instance.ArtistItems.CreateNew( );[/COLOR]

			item.Name = "Image";

			item.ArtistID = page.ID;

			item.Value = "placeholder_med";


			[COLOR="red"]item = PenguinDataContext.Instance.ArtistItems.CreateNew( );[/COLOR]

			item.Name = "ImageThumb";

			item.ArtistID = page.ID;

			item.Value = "placeholder_sm";

		} else {

			page = PenguinDataContext.Instance.Artists.GetByID(Conver t.ToInt64(Session["ArtistID"]));

			item = PenguinDataContext.Instance.ArtistItems.GetByID(Co nvert.ToInt64(Session["ArtistItemID"]));


			page.Name = txtName.Text;


			page.Save();


			if (fckEntryContent.Visible) {

				item.ArtistID = page.ID;

				item.Value = fckEntryContent.Value;


				item.Save();

			}

		}


	} catch (Exception ex) {

		return;

	}


	if (Session["Mode"].ToString() == "Add") {

		pagingArtists.GetPagingController<Artist>().DataSo urce = PenguinDataContext.Instance.Artists.GetAllArtists( ).ToList<Artist>();

		pagingArtists.GetPagingController<Artist>().Curren tPageIndex = pagingArtists.GetPagingController<Artist>().PageCo unt;

	} else {

		int cp = pagingArtists.GetPagingController<Artist>().Curren tPageIndex;


		if (cp > pagingArtists.GetPagingController<Artist>().PageCo unt)

			cp = pagingArtists.GetPagingController<Artist>().PageCo unt;


		pagingArtists.GetPagingController<Artist>().Curren tPageIndex = cp;

	}


	pagingArtists.Update();

	LoadPagesList();

	mvwManager.ActiveViewIndex = 0;

}



anyways, that's my initial take, hope its helpful




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users