Solved

OleDbParameterCollection - Problem with sql statment with string and int

Posted on 2009-05-07
1
1,092 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

691 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