?
Solved

OleDbParameterCollection - Problem with sql statment with string and int

Posted on 2009-05-07
1
Medium Priority
?
1,094 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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

771 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