Go Back   CodingForums.com > :: Server side development > ASP.NET

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-27-2011, 01:56 AM   PM User | #1
Fot
Regular Coder

 
Join Date: Mar 2011
Posts: 111
Thanks: 88
Thanked 0 Times in 0 Posts
Fot is an unknown quantity at this point
Data Set Problem

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 is offline   Reply With Quote
Old 04-27-2011, 10:56 AM   PM User | #2
Fot
Regular Coder

 
Join Date: Mar 2011
Posts: 111
Thanks: 88
Thanked 0 Times in 0 Posts
Fot is an unknown quantity at this point
Please, I would like help.Thanks.
Fot is offline   Reply With Quote
Old 04-27-2011, 02:42 PM   PM User | #3
Mike_O
Regular Coder

 
Join Date: Apr 2009
Posts: 244
Thanks: 1
Thanked 20 Times in 20 Posts
Mike_O is an unknown quantity at this point
Hey FOt,

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

Mike
Mike_O is offline   Reply With Quote
Users who have thanked Mike_O for this post:
Fot (04-27-2011)
Old 04-28-2011, 02:44 PM   PM User | #4
Fot
Regular Coder

 
Join Date: Mar 2011
Posts: 111
Thanks: 88
Thanked 0 Times in 0 Posts
Fot is an unknown quantity at this point
Hello,if declare the code with value I will have another problem.Thanks.This the code:


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();
        }
    }
}
Fot is offline   Reply With Quote
Old 04-28-2011, 09:11 PM   PM User | #5
Mike_O
Regular Coder

 
Join Date: Apr 2009
Posts: 244
Thanks: 1
Thanked 20 Times in 20 Posts
Mike_O is an unknown quantity at this point
Hey Fot,

There are 2 issues, as I see it.

#1. You have to instantiate your Code object by saying something like:
Code:
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
Mike_O is offline   Reply With Quote
Users who have thanked Mike_O for this post:
Fot (04-28-2011)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:48 PM.


Advertisement
Log in to turn off these ads.