I want to bind data from SQL Server table column at page load in ASP.NET, but it is not working












0














I want to bind data from a SQL Server table column at page load in ASP.NET, but it is not working; code is working in a button click.



This is my code - can anyone help please?



if (!IsPostBack)
{
DataTable subjects = new DataTable();

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
SqlDataAdapter adapter = new SqlDataAdapter("SELECT [name] FROM [supplier]", con);
adapter.Fill(subjects);

con.Open();

DropDownreturn.DataSource = subjects;
DropDownreturn.DataTextField = "name";
DropDownreturn.DataValueField = "name";
DropDownreturn.DataBind();
DropDownreturn.Items.Insert(0, new ListItem("Select", "NA"));

con.Close();
}









share|improve this question
























  • Are you receiving an error?
    – jtate
    Nov 12 '18 at 22:17










  • no i don't receive any errors
    – Sekani
    Nov 12 '18 at 22:21










  • Unrelated tips: SqlConnection and SqlDataAdapter are both IDisposable so each should be in a using block. Once you've done that, you don't need to Close the connection because it will be closed by the implicit Dispose as it exits the block.
    – Richardissimo
    Nov 13 '18 at 5:59








  • 2




    Please clarify "it is not working": Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself.
    – Richardissimo
    Nov 13 '18 at 6:04










  • i fixed the problem thanks for helping
    – Sekani
    Nov 13 '18 at 20:40
















0














I want to bind data from a SQL Server table column at page load in ASP.NET, but it is not working; code is working in a button click.



This is my code - can anyone help please?



if (!IsPostBack)
{
DataTable subjects = new DataTable();

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
SqlDataAdapter adapter = new SqlDataAdapter("SELECT [name] FROM [supplier]", con);
adapter.Fill(subjects);

con.Open();

DropDownreturn.DataSource = subjects;
DropDownreturn.DataTextField = "name";
DropDownreturn.DataValueField = "name";
DropDownreturn.DataBind();
DropDownreturn.Items.Insert(0, new ListItem("Select", "NA"));

con.Close();
}









share|improve this question
























  • Are you receiving an error?
    – jtate
    Nov 12 '18 at 22:17










  • no i don't receive any errors
    – Sekani
    Nov 12 '18 at 22:21










  • Unrelated tips: SqlConnection and SqlDataAdapter are both IDisposable so each should be in a using block. Once you've done that, you don't need to Close the connection because it will be closed by the implicit Dispose as it exits the block.
    – Richardissimo
    Nov 13 '18 at 5:59








  • 2




    Please clarify "it is not working": Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself.
    – Richardissimo
    Nov 13 '18 at 6:04










  • i fixed the problem thanks for helping
    – Sekani
    Nov 13 '18 at 20:40














0












0








0







I want to bind data from a SQL Server table column at page load in ASP.NET, but it is not working; code is working in a button click.



This is my code - can anyone help please?



if (!IsPostBack)
{
DataTable subjects = new DataTable();

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
SqlDataAdapter adapter = new SqlDataAdapter("SELECT [name] FROM [supplier]", con);
adapter.Fill(subjects);

con.Open();

DropDownreturn.DataSource = subjects;
DropDownreturn.DataTextField = "name";
DropDownreturn.DataValueField = "name";
DropDownreturn.DataBind();
DropDownreturn.Items.Insert(0, new ListItem("Select", "NA"));

con.Close();
}









share|improve this question















I want to bind data from a SQL Server table column at page load in ASP.NET, but it is not working; code is working in a button click.



This is my code - can anyone help please?



if (!IsPostBack)
{
DataTable subjects = new DataTable();

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
SqlDataAdapter adapter = new SqlDataAdapter("SELECT [name] FROM [supplier]", con);
adapter.Fill(subjects);

con.Open();

DropDownreturn.DataSource = subjects;
DropDownreturn.DataTextField = "name";
DropDownreturn.DataValueField = "name";
DropDownreturn.DataBind();
DropDownreturn.Items.Insert(0, new ListItem("Select", "NA"));

con.Close();
}






c# asp.net sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 5:56









marc_s

572k12811051252




572k12811051252










asked Nov 12 '18 at 22:12









SekaniSekani

11




11












  • Are you receiving an error?
    – jtate
    Nov 12 '18 at 22:17










  • no i don't receive any errors
    – Sekani
    Nov 12 '18 at 22:21










  • Unrelated tips: SqlConnection and SqlDataAdapter are both IDisposable so each should be in a using block. Once you've done that, you don't need to Close the connection because it will be closed by the implicit Dispose as it exits the block.
    – Richardissimo
    Nov 13 '18 at 5:59








  • 2




    Please clarify "it is not working": Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself.
    – Richardissimo
    Nov 13 '18 at 6:04










  • i fixed the problem thanks for helping
    – Sekani
    Nov 13 '18 at 20:40


















  • Are you receiving an error?
    – jtate
    Nov 12 '18 at 22:17










  • no i don't receive any errors
    – Sekani
    Nov 12 '18 at 22:21










  • Unrelated tips: SqlConnection and SqlDataAdapter are both IDisposable so each should be in a using block. Once you've done that, you don't need to Close the connection because it will be closed by the implicit Dispose as it exits the block.
    – Richardissimo
    Nov 13 '18 at 5:59








  • 2




    Please clarify "it is not working": Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself.
    – Richardissimo
    Nov 13 '18 at 6:04










  • i fixed the problem thanks for helping
    – Sekani
    Nov 13 '18 at 20:40
















Are you receiving an error?
– jtate
Nov 12 '18 at 22:17




Are you receiving an error?
– jtate
Nov 12 '18 at 22:17












no i don't receive any errors
– Sekani
Nov 12 '18 at 22:21




no i don't receive any errors
– Sekani
Nov 12 '18 at 22:21












Unrelated tips: SqlConnection and SqlDataAdapter are both IDisposable so each should be in a using block. Once you've done that, you don't need to Close the connection because it will be closed by the implicit Dispose as it exits the block.
– Richardissimo
Nov 13 '18 at 5:59






Unrelated tips: SqlConnection and SqlDataAdapter are both IDisposable so each should be in a using block. Once you've done that, you don't need to Close the connection because it will be closed by the implicit Dispose as it exits the block.
– Richardissimo
Nov 13 '18 at 5:59






2




2




Please clarify "it is not working": Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself.
– Richardissimo
Nov 13 '18 at 6:04




Please clarify "it is not working": Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself.
– Richardissimo
Nov 13 '18 at 6:04












i fixed the problem thanks for helping
– Sekani
Nov 13 '18 at 20:40




i fixed the problem thanks for helping
– Sekani
Nov 13 '18 at 20:40












1 Answer
1






active

oldest

votes


















0















but it is not working; code is working in a button click.




When user press a button then it create a postback if Postback is enabled on button and you have placed !IsPostBack check that may be preventing the code block to bind the dropdown.



To solve this issue.. At Page_Load, check if your button raised the postback then execute the code to bind the dropdown by following below suggested approach:



Which control caused the postback?



Way To Know Which Control Has Raised PostBack



if(IsPostBack)
{
if(postbackcontrol is button1)
{
//Bind dropdown
}
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270842%2fi-want-to-bind-data-from-sql-server-table-column-at-page-load-in-asp-net-but-it%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0















    but it is not working; code is working in a button click.




    When user press a button then it create a postback if Postback is enabled on button and you have placed !IsPostBack check that may be preventing the code block to bind the dropdown.



    To solve this issue.. At Page_Load, check if your button raised the postback then execute the code to bind the dropdown by following below suggested approach:



    Which control caused the postback?



    Way To Know Which Control Has Raised PostBack



    if(IsPostBack)
    {
    if(postbackcontrol is button1)
    {
    //Bind dropdown
    }
    }





    share|improve this answer


























      0















      but it is not working; code is working in a button click.




      When user press a button then it create a postback if Postback is enabled on button and you have placed !IsPostBack check that may be preventing the code block to bind the dropdown.



      To solve this issue.. At Page_Load, check if your button raised the postback then execute the code to bind the dropdown by following below suggested approach:



      Which control caused the postback?



      Way To Know Which Control Has Raised PostBack



      if(IsPostBack)
      {
      if(postbackcontrol is button1)
      {
      //Bind dropdown
      }
      }





      share|improve this answer
























        0












        0








        0







        but it is not working; code is working in a button click.




        When user press a button then it create a postback if Postback is enabled on button and you have placed !IsPostBack check that may be preventing the code block to bind the dropdown.



        To solve this issue.. At Page_Load, check if your button raised the postback then execute the code to bind the dropdown by following below suggested approach:



        Which control caused the postback?



        Way To Know Which Control Has Raised PostBack



        if(IsPostBack)
        {
        if(postbackcontrol is button1)
        {
        //Bind dropdown
        }
        }





        share|improve this answer













        but it is not working; code is working in a button click.




        When user press a button then it create a postback if Postback is enabled on button and you have placed !IsPostBack check that may be preventing the code block to bind the dropdown.



        To solve this issue.. At Page_Load, check if your button raised the postback then execute the code to bind the dropdown by following below suggested approach:



        Which control caused the postback?



        Way To Know Which Control Has Raised PostBack



        if(IsPostBack)
        {
        if(postbackcontrol is button1)
        {
        //Bind dropdown
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 6:06









        Niranjan SinghNiranjan Singh

        15.7k23065




        15.7k23065






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270842%2fi-want-to-bind-data-from-sql-server-table-column-at-page-load-in-asp-net-but-it%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma