Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Fot
    Fot is offline
    Regular Coder
    Join Date
    Mar 2011
    Posts
    111
    Thanks
    88
    Thanked 0 Times in 0 Posts

    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;

  • #2
    Fot
    Fot is offline
    Regular Coder
    Join Date
    Mar 2011
    Posts
    111
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Please, I would like help.Thanks.

  • #3
    Regular Coder
    Join Date
    Apr 2009
    Posts
    244
    Thanks
    1
    Thanked 20 Times in 20 Posts
    Hey FOt,

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

    Mike

  • Users who have thanked Mike_O for this post:

    Fot (04-27-2011)

  • #4
    Fot
    Fot is offline
    Regular Coder
    Join Date
    Mar 2011
    Posts
    111
    Thanks
    88
    Thanked 0 Times in 0 Posts
    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();
            }
        }
    }

  • #5
    Regular Coder
    Join Date
    Apr 2009
    Posts
    244
    Thanks
    1
    Thanked 20 Times in 20 Posts
    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

  • Users who have thanked Mike_O for this post:

    Fot (04-28-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •