c# - insert data using WriteToServer method datetime column gives exception -
i trying insert datetime column data type datetime. value assigned using datetime.now, know there isn't issue incorrect types in code.
the exception is:
the given value of type string data source cannot converted type datetime of specified target column. this how specifying datatable columns:
datatable hodetails = new datatable(); hodetails.columns.add("masterid", typeof(long)); hodetails.columns.add("itemid", typeof(int)); hodetails.columns.add("subcategoryid", typeof(int)); hodetails.columns.add("batchno", typeof(string)); hodetails.columns.add("expirydate", typeof(datetime)); and here how setting data:
hodetails.rows[hodetails.rows.count - 1]["itemid"] = convert.toint32(lblitemid.text); hodetails.rows[hodetails.rows.count - 1]["masterid"] =horeceipt_id; hodetails.rows[hodetails.rows.count - 1]["subcategoryid"] = subcatid; hodetails.rows[hodetails.rows.count - 1]["batchno"] = convert.tostring(txtbatchno.text == "" ? "" : txtbatchno.text); hodetails.rows[hodetails.rows.count - 1]["expirydate"] = datetime.now; so why writetoserver() think "value of type string data source cannot converted type datetime"?
my problem solved change writetoserver method
if (dtwithtablename.rows.count > 0) { using (sqlbulkcopy s = new sqlbulkcopy(strconnection)) { try { s.destinationtablename = "homaterialreceiptdetails"; s.columnmappings.add("masterid", "masterid"); s.columnmappings.add("itemid", "itemid"); s.columnmappings.add("subcategoryid", "subcategoryid"); s.columnmappings.add("expirydate", "expirydate"); s.columnmappings.add("brandname", "brandname"); s.columnmappings.add("qty", "qty"); s.columnmappings.add("freeqty", "freeqty"); s.columnmappings.add("returnqty", "returnqty"); s.columnmappings.add("replacementqty", "replacementqty"); s.writetoserver(dtwithtablename); } catch (exception) { isinserted = false; } } }
Comments
Post a Comment