Solved

OleDbParameterCollection - Problem with sql statment with string and int

Posted on 2009-05-07
1
1,091 Views
Last Modified: 2012-06-22
Error message: The OleDbParameterCollection only accepts non-null OleDbParameter type objects, not String objects.

se code. The problem I have is the string variable. How should it be done?
public void registreraNyFilm(Filminfo film)
        {
            registreraRegisor(film);
            int regisorId = getRegisor(film.Regissor);
            registreraGenre(film);
            int genreid = getGenre(film.Genre);
            registreraRentCost(film.RentCost);
            int idprice = getRentCost(film.RentCost);            
           
            String myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source= C:\\Documents and Settings\\MFL\\Dokumenter\\Visual Studio 2008\\Projects\\ProjektFilm\\ProjektFilm\\FemmanFilm_ver1.mdb";
 
            string sql = "INSERT INTO t_movie (title, directorid, genreid, rentcostid) Values(@name, @director, @genre, @rentcost)";
            OleDbConnection cn = new OleDbConnection(myConnectionString);
            using (OleDbCommand cmd = new OleDbCommand(sql, cn))
            {
 
                cmd.Parameters.Add("@rentcost", OleDbType.Integer).Value = idprice;                       
                cmd.Parameters.Add("@genre", OleDbType.Integer).Value = genreid;        
                cmd.Parameters.Add("@director", OleDbType.Integer).Value = regisorId;
                cmd.Parameters.Add("@name"); 
                cn.Open();
                cmd.Parameters[0].Value = idprice;
                cmd.Parameters[1].Value = genreid;
                cmd.Parameters[2].Value = regisorId;
                cmd.Parameters[3].Value = film.Titel;
                cmd.ExecuteNonQuery();
                cn.Close();
            }
        }

Open in new window

0
Comment
Question by:Mickeys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
JulienVan earned 500 total points
ID: 24323313
Can you try to define the type of the parameter as WChar, as in the code below?

You can also define by default Titel = string.Empty when you declare Titel in the FilmInfo class.
cmd.Parameters.Add("@name", OleDbType.WChar);

Open in new window

0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question