...

View Full Version : Data Set Problem



Fot
04-27-2011, 01:56 AM
The browser have this display and I do not know what is that.Thanks

Cannot set Column 'Code' to be null. Please use DBNull instead.
Description: An unhandled exception occurred during the execution of the current web request.

Exception Details: System.ArgumentException: Cannot set Column 'Code' to be null. Please use DBNull instead.

Source Error:

Line 33: MyDT.Columns.Add(MyDC);
Line 34:
Line 35:
Line 36: MyDR = MyDT.NewRow();
Line 37: MyDR["Code"] = Code;

Fot
04-27-2011, 10:56 AM
Please, I would like help.Thanks.

Mike_O
04-27-2011, 02:42 PM
Hey FOt,

In line 37, what is variable Code, and what is it's value?

Mike

Fot
04-28-2011, 02:44 PM
Hello,if declare the code with value I will have another problem.Thanks.This the code:



using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication21
{
public partial class WebForm17 : System.Web.UI.Page
{
private object Code;
private object D15;


protected void Page_Load(object sender, EventArgs e)
{
DataSet MyDS = new DataSet();
DataTable MyDT = new DataTable("Catalogue");
DataColumn MyDC;
DataRow MyDR;
DataColumn[] PKColumn = new DataColumn[1];

MyDC = new DataColumn("Code", Type.GetType("System.Int32"));
MyDT.Columns.Add(MyDC);
PKColumn[0] = MyDC;
MyDT.PrimaryKey = PKColumn;

MyDC = new DataColumn("Price", Type.GetType("System.Int32"));
MyDT.Columns.Add(MyDC);


MyDR = MyDT.NewRow();
MyDR["Code"] = Code;
MyDR["Price"] = 907.00;
MyDT.Rows.Add(MyDR);
MyDR = MyDT.NewRow();
MyDR["Code"] = D15;
MyDR["Price"] = 854.00;
MyDT.Rows.Add(MyDR);

MyDS.Tables.Add(MyDT);
GridView1.DataSource = MyDS;
GridView1.DataBind();

String myString = "Data Source= .\\SQLEXPRESS; AttachDbFilename=|DataDirectory|\\Databasebest.mdf; Integrated Security=true; User Instance=true";


SqlConnection Myconn = new SqlConnection(myString);
SqlDataAdapter MyDA;
SqlCommandBuilder MyCB;
SqlCommand MyCmd;
SqlDataReader MyRdr;

Myconn = new SqlConnection(myString);
Myconn.Open();
MyCmd = new SqlCommand("SELECT * FROM Catalogue", Myconn);
MyRdr = MyCmd.ExecuteReader();
while (MyRdr.Read())
{
Response.Write(MyRdr["Code"] + "<br/>");
}
MyRdr.Close();
Myconn.Close();



MyDA = new SqlDataAdapter("SELECT * FROM Catalogue", Myconn);
MyDA.Fill(MyDS, "Catalogue");
MyDT = MyDS.Tables["Catalogue"];
dgBefore.DataSource = MyDS;
dgBefore.DataBind();


MyDR = MyDT.NewRow();
MyDR["Price"] = 907.00;
MyDT.Rows.Add(MyDR);


MyDR = MyDT.Rows[2];
MyDR["Price"] = 854.00;


// MyDT.Rows[1].Delete();

MyCB = new SqlCommandBuilder(MyDA);
MyDA.Update(MyDS, "Catalogue");
//.Update(MyDS, "rooms");
dgModified.DataSource = MyDS;
dgModified.DataBind();
}
}
}

Mike_O
04-28-2011, 09:11 PM
Hey Fot,

There are 2 issues, as I see it.

#1. You have to instantiate your Code object by saying something like:

private object Code = new object();
#2. The "Code" DataColumn that you are adding to the DataTable, you are setting it as type Int32. That's fine, but then you're trying to assign object Code to it. So, you either have to convert your object Code to an Int32, or just use an Int32 variable.


Regards,
Mike



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum