T
T
Therapyx2015-08-21 04:17:20
ASP.NET
Therapyx, 2015-08-21 04:17:20

Where exactly is it necessary to run the function with loading the list of categories from the database into the DetailsView, which is visible = false by default?

Such is the problem. There is a DetailsView as a "pop-up" that has a dropdownlist with categories.
Made the following function
p.s. ddl_text is the ddl that is in the DetailsView, and ddl_Category is on the page itself, and is visible on load

private void loadCategoryList()
    {
        DropDownList ddl_Text = (DropDownList)dvPurchase.FindControl("ddl_Text");
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DB_Name"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("usp_Category_Select", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
            cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = Session["UserID"];
            ddl_Text.DataSource = cmd.ExecuteReader();
            ddl_Text.DataTextField = "Category";
            ddl_Text.DataValueField = "CategoryID";
            ddl_Text.DataBind();
            cmd.Connection.Close();
        }
        ddl_Category.DataSource = ddl_Text.Items;
        ddl_Category.DataBind();        
    }

Duck, here's the goal, this is to load the dl into the DetailsView at Page_Loade and transfer the categories from it to the main page (you can do it vice versa).
I tried to open a function in Page_Load, Page_Preload, but the following error pops up there and there. "Object reference not set to an instance of an object." on the line "ddl_Text.DataSource = cmd.ExecuteReader();"
If I do everything the same, just fill in the DropDownList, which is on the main page, then everything works fine, so the procedure itself is written without errors.
Perhaps I just did not understand some of the nuances, if possible, please also explain with explanations :) Thank you in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Therapyx, 2015-08-21
@Therapyx

Eh, I decided, but it seems to me - in a very dirty way. Or is this the very abyss that is not liked in asp.net because of the View States :)

protected void dvPurchase_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl_Text = (DropDownList)dvPurchase.FindControl("ddl_Text");
        if (dvPurchase.CurrentMode == DetailsViewMode.Insert)
        {            
            loadCategoryList();
            Label1.Text = "Insert";         
        }
        if (dvPurchase.CurrentMode == DetailsViewMode.Edit)
        {           
            loadCategoryList();
            Label1.Text = "Edit";        
        }
        if (dvPurchase.CurrentMode == DetailsViewMode.ReadOnly)
            Label1.Text = "ReadOnly";  
        
    }

D
Dmitry Kovalsky, 2015-08-21
@dmitryKovalskiy

If you have solved your problem - congratulations. The problem of using WebForms remains. Today the technology is too heavy and opaque. Try to teach the site to understand both WebForms and MVC and slowly migrate there. on MVC, requests and markup generation are more transparent.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question