sql server 2008 - C++ ADO.NET Cannot insert the value NULL into column -
i'm trying insert lines in database, got error while executing code :
#using <mscorlib.dll> #using <system.dll> #using <system.data.dll> #using <system.xml.dll> #include <sstream> #include <time.h> #include <string> #include <iostream> #include <tchar.h> using namespace system; using namespace system::data; using namespace system::xml; using namespace system::data::sqlclient; // entry point application int _tmain(void) { sqlconnection ^ mysqlconnection; sqldataadapter ^ mydataadapter; dataset ^ mydataset; datarow ^ myrow; sqlparameter ^ myparameter; try { mysqlconnection = gcnew sqlconnection("data source=nectarys-pc;initial catalog=monitoringn;integrated security=true;"); mydataadapter = gcnew sqldataadapter(); mydataset = gcnew dataset(); // open connection mysqlconnection->open(); mydataadapter->selectcommand = gcnew sqlcommand("select motif,datealerte,fixee,nomposte," + "nomapplication,nomfichier,fichiermodel_id alerte", mysqlconnection); mydataadapter->insertcommand = gcnew sqlcommand("insert alerte (motif,datealerte," + "fixee,nomposte,nomapplication,nomfichier,fichiermodel_id) values (@motif,@datealerte," + "@fixee,@nomposte,@nomapplication,@nomfichier,@fichiermodel_id)", mysqlconnection); myparameter = mydataadapter->insertcommand->parameters->add( gcnew sqlparameter("@motif", sqldbtype::varchar)); myparameter->sourcecolumn = "motif"; myparameter->sourceversion = datarowversion::current; myparameter = mydataadapter->insertcommand->parameters->add( gcnew sqlparameter("@datealerte", sqldbtype::varchar)); myparameter->sourcecolumn = "datealerte"; myparameter->sourceversion = datarowversion::current; myparameter = mydataadapter->insertcommand->parameters->add( gcnew sqlparameter("@fixee", sqldbtype::bit)); myparameter->sourcecolumn = "fixee"; myparameter->sourceversion = datarowversion::current; myparameter = mydataadapter->insertcommand->parameters->add( gcnew sqlparameter("@nomposte", sqldbtype::varchar)); myparameter->sourcecolumn = "nomposte"; myparameter->sourceversion = datarowversion::current; myparameter = mydataadapter->insertcommand->parameters->add( gcnew sqlparameter("@nomapplication", sqldbtype::varchar)); myparameter->sourcecolumn = "nomapplication"; myparameter->sourceversion = datarowversion::current; myparameter = mydataadapter->insertcommand->parameters->add( gcnew sqlparameter("@nomfichier", sqldbtype::varchar)); myparameter->sourcecolumn = "nomfichier"; myparameter->sourceversion = datarowversion::current; myparameter = mydataadapter->insertcommand->parameters->add( gcnew sqlparameter("@fichiermodel_id", sqldbtype::int)); myparameter->sourcecolumn = "fichiermodel_id"; myparameter->sourceversion = datarowversion::current; time_t = time(0); tm ltm; localtime_s(<m,&now); std::stringstream dateajoutsysteme; dateajoutsysteme << ltm.tm_mday << "/" << 1 + ltm.tm_mon << "/" << 1900 + ltm.tm_year << " " << 1 + ltm.tm_hour << ":" << 1 + ltm.tm_min << ":" << 1 + ltm.tm_sec; string^ str = gcnew system::string(dateajoutsysteme.str().c_str()); boolean fixee = true; mydataadapter->fill(mydataset, "alerte"); myrow = mydataset->tables["alerte"]->newrow(); myrow->itemarray[0] = "a"; myrow->itemarray[1] = str; myrow->itemarray[2] = fixee; myrow->itemarray[3] = "b"; myrow->itemarray[4] = "c"; myrow->itemarray[5] = "d"; myrow->itemarray[6] = 0ul; mydataset->tables["alerte"]->rows->add(myrow); //we use insertcommand property update. mydataadapter->update(mydataset, "alerte"); } catch (exception ^ e) { console::write(e->tostring()); } __finally { mysqlconnection->close(); system("pause"); } return 0; }
error :
system.data.sqlclient.sqlexception (0x80131904): cannot insert value null column 'fixee', table 'monitoringn.dbo.alerte'; column not allow nulls.
any brilliant idea, please ?
you select * on alerte ... don't know order of columns ... assign values due column index ... guess there problem ... fixee not 2nd column in mydataset->tables["alerte"]
Comments
Post a Comment