Jump to content

Trouble updating a sql database through my c# program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
dragonskullinc

dragonskullinc

    Newbie

  • Members
  • Pip
  • 3 posts
I am having issues doing an update to my sql server with a program I'm building. It can see the database and see the entries but when I need to save a new addition or change something with a current entry it gives me the following error: "Update requires a valid UpdateCommand when passed DataRow collection with modified rows." and then highlights the current line of code: "this.tableAdapterManager.UpdateAll(this.animeDataSet);" I am very new to this programming language so any help would be much apprieciated(I quite literally started yesterday haha) The program is in the end going to update a database on my webserver which will then update on my site via a coldfusion connection. This program is ment to allow me to update the database anywhere on my lan and take place of a excel sheet and do all the math equations as well but first I need it to be able to connect and update and save new entries . I realize cold fusion is capable of doing this as well but I do not know enough cold fusion to do this. I just know enough for it to access the database, make new entries and display on the site.


"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Anime_Database_Modifier
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void main_listBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.main_listBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.animeDataSet);

}

private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'animeDataSet.main_list' table. You can move, or remove it, as needed.
this.main_listTableAdapter.Fill(this.animeDataSet.main_list);

}

private void main_listBindingNavigator_Refre****ems(object sender, EventArgs e)
{

}
}
}

"
I know its probably something simple but for the life of me I cannot figure it out. If needed I can attach the project and database. I have the database local at the moment but I am going to build a client that can see it on the network. Just need to get it working first.

Edited by dragonskullinc, 16 June 2010 - 09:43 AM.
added source


#2
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
Since you are using .NET 3.5, you can use LINQ to edit your database.

Add a new item to your project, a "LINQ to SQL connection", name it however you want. Open that file and drag the tables you want to edit into it.
Once you do that, assuming that you named file "DataClasses1" you can use LINQ the following name:

DataClasses1DataContext db = new DataClasses1DataContext();

TableName x = new TableName();

x.TableColumn = 5;
x.AnotherTableColumn = "Hi";

db.InsertOnSubmit(x);
db.SubmitChanges();

LINQ lets you use tables as classes and makes everything easier. Ask if you have any other questions.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#3
dragonskullinc

dragonskullinc

    Newbie

  • Members
  • Pip
  • 3 posts
And where do I put the code you posted? or will it automatically add it? also will this allow me to use my current form that I created or will I need to create a new set of forms to accomodate the new code association. I have attached how the form looks. Very basic, simple and when done will do what I need it to do.

Attached Files



#4
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
Ok, create a database and add tables in it with columns and stuff. Create the LINQ to SQL file. Once you open that file, DRAG & DROP the table from your database to that file. Save, then go to your form. Let's say you want to add when you click a button, just add the code when you click the button (Note: Replace TableName and TableColumn with the appropriate values).

The form that you use is just fine.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#5
dragonskullinc

dragonskullinc

    Newbie

  • Members
  • Pip
  • 3 posts
The way I've been trying to do it is add the database as a data source and then dragging the fields over that I want to change and see to the form. I already have an existing database with existing entries. I havent added that many entries so starting over is no big deal. Pretty much the data to be changed is displayed in the form I attached.