user3052078
user3052078

Reputation: 495

Parameter has no default value , database c#

I'm trying to add information to my database. I have this code

private OleDbConnection connect;
private OleDbCommand command;

public DataBaseManager()
{
        connect = new OleDbConnection();
        command = new OleDbCommand();
        connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Aurelian\Desktop\Photo.accdb;
        Persist Security Info=False;";
        connect.Open();
        command.Connection = connect;
}

public void AddAlbum (Album newAlbum)
{
   command.CommandText = "INSERT INTO Album VALUES(@Id,@Name,@Description,@Location,@Data)";// + Int32.Parse(newAlbum.ID.ToString()) + ",'" + newAlbum.Name.ToString() + "','" + newAlbum.Description.ToString() + "','" + newAlbum.Location.ToString() + "'," + Convert.ToDateTime(newAlbum.Date.ToString()) + ")";

   command.Parameters.AddWithValue("@Id", newAlbum.ID);
   command.Parameters.AddWithValue("@Name", newAlbum.Name);
   command.Parameters.AddWithValue("@Description", newAlbum.Description);
   command.Parameters.AddWithValue("@Location", newAlbum.Location);
   command.Parameters.AddWithValue("@Data", newAlbum.Date);

   command.ExecuteNonQuery();
}

and the call:

DataBaseManager dm = new DataBaseManager();
Album alb=new Album(5,"TEST","TestDescriere","TestLocatie",new DateTime(2012,12,12));
dm.AddAlbum(alb);

The problem is on this line:

command.ExecuteNonQuery(); 

and this is the error:

An exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll but was not handled in user code

Additional information: Parameter @Name has no default value.

Upvotes: 0

Views: 1603

Answers (2)

Beakie
Beakie

Reputation: 1999

Please check you are actually setting the name property in your class constructor.

Upvotes: 3

Zach
Zach

Reputation: 3207

It sounds like your newAlbum.Name is null.

Upvotes: 1

Related Questions