Inserting data with .net IDbCommand and IDbDataParameter

Posted on 2006-05-25
Last Modified: 2011-09-20
So, i have the following code:

IDbCommand masterMeterCommand = dbConn.CreateCommand();
masterMeterCommand.CommandText = "insert into metermaster (MeterId, UtilityId, ManufacturerId) values (@MeterId,2,1)";

IDbDataParameter dbIdParam = masterMeterCommand.CreateParameter();
dbIdParam.ParameterName = "@MeterId";
dbIdParam.Value = 1;


It always fails at ExecuteNonQuery with a "Column 'MeterId' cannot be null". So it seems that the parameter is not being populated or something. If i execute:

insert into metermaster (MeterId, UtilityId, ManufacturerId) values (1,2,1)

then it works just fine...what gives?

Question by:MarkoBarko
    LVL 1

    Author Comment

    Never mind, answered my own question...

    the syntax for the prepared statement differs depending on the underlying provider.

    The syntax above works on MS SQL which uses the @ symbol to mark variables...i was using the MySQL provider which uses question marks for the same purpose...

    So much for writing portable queries...

    Accepted Solution

    Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now