开发者

errors trying to insert into mysql database?

Unhandled error when I try to add to database? The OdbcParameterCollection only accepts non-null OdbcParameter type objects, not String objects.

    protected void Button1_Click(object sender, EventArgs e)
    {

    string email = TextBox1.Text; //added variable name
    string firstname = TextBox2.Text;
    string secondname = TextBox3.Text;
    string dob = TextBox4.Text;
    string location = TextBox5.Text;
    string aboutme = TextBox6.Text;

    OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite; User=root; Password=;");
    cn.Open();
    OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email, FirstName, SecondName, DOB, Location, Aboutme) VALUES (email, firstname, secondname, dob, location, aboutme)"); // fixed incomplete insert statement.

        cmd.Parameters.Add(email);
        cmd.Parameters.Add(firstname);
        cmd.Parameters.Add(secondname);
        cmd.Parameters.Add(dob);
        cmd.Parameters.Add(location);
        cmd.Parameters.Add(aboutme);//add the parameter to the command
        cmd.ExecuteNonQuery(); //actually run the sql


    }

}

I tryed this but I get the error System.InvalidOperationException: ExecuteNonQuery: Connection property has not been initialized.

I also changed the database so it wasnt NN (non-null) to see if it would help but no joy

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Odbc;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    string email = TextBox1.Text; //added variable name
    string firstname = TextBox2.Text;
    string 开发者_运维百科secondname = TextBox3.Text;
    string dob = TextBox4.Text;
    string location = TextBox5.Text;
    string aboutme = TextBox6.Text;

    OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite; User=root; Password=;");
    cn.Open();
    OdbcCommand cmd = new OdbcCommand("INSERT INTO User(Email) VALUES('?Email')");



    cmd.Parameters.Add(new OdbcParameter("?email", email));
    cmd.ExecuteNonQuery();


    }

}

The data in the database is capital letters like 'L'ocation and my strings are set to lower case. Not sure if its back to front in my code tho?

-- -----------------------------------------------------

-- Table `gymwebsite`.`User`

-- -----------------------------------------------------

CREATE  TABLE IF NOT EXISTS `gymwebsite`.`User` (

  `UserID` INT NOT NULL AUTO_INCREMENT ,

  `Email` VARCHAR(245) NULL ,

  `FirstName` VARCHAR(45) NULL ,

  `SecondName` VARCHAR(45) NULL ,

  `DOB` VARCHAR(15) NULL ,

  `Location` VARCHAR(45) NULL ,

  `Aboutme` VARCHAR(245) NULL ,

  PRIMARY KEY (`UserID`) )

ENGINE = InnoDB;

INSERT INTO `gymwebsite`.`user`
(`UserID`,
`Email`,
`FirstName`,
`SecondName`,
`DOB`,
`Location`,
`Aboutme`)
VALUES
(
{UserID: INT},
{Email: VARCHAR},
{FirstName: VARCHAR},
{SecondName: VARCHAR},
{DOB: VARCHAR},
{Location: VARCHAR},
{Aboutme: VARCHAR}
);
EDIT `gymwebsite`.`user`;


Use this

OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email, FirstName, SecondName, DOB, Location, Aboutme) VALUES ("+ email +","+ firstname+","+SecondName+","+DOB+","+Location+","+Aboutme+")",cn); 

Try to play using parameters..that i posted earlier and have it working.

Regards


Specify all of the parameters on this line -

var v1 = cmd.Parameters.Add("email",OdbcType.VarChar, , 30); v1.Value = email;


You might also have to swtich your SQL command to use question marks like:

OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email, FirstName, SecondName, DOB, Location, Aboutme) VALUES (?, ?, ?, ?, ?, ?)");


Have you tried this?

cmd.Parameters.Add(new OdbcParameter(email));

There's a few overloads too.


You need to add OdbcParameter instances to the parameter collection:

cmd.Parameters.Add(new OdbcParameter("email", email)); 
...

The first parameter is the name of the parameter in your insert statement, and the second is an object used to determine DbType and content.


Use Parameters.AddWithValue() method,

OdbcCommand cmd = new OdbcCommand("INSERT INTO `User` 
  (Email, FirstName, SecondName, DOB, Location, Aboutme) 
    VALUES 
     ('?Email', '?FirstName', '?SecondName', '?DOB', '?Location','?Aboutme')",cn); 

cmd.Parameters.AddWithValue("?Email", textBox1.Text);
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜