C# Web-App: how to retreive data form SQL tables?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • waterfall
    New Member
    • Dec 2007
    • 22

    C# Web-App: how to retreive data form SQL tables?

    How to get data from database in text box on selecting a value from combobox in C# web forms?
    Im using two tables employee and worker table.i succeeded in populating combobox with data from SQL employee table.When I click employee’s data in combobox I cant display related data from worker table in textbox. plz help me….
    I KNOW HOW TO DO THIS IN WINDOWS FORMS . PLZ TELL ME ABOUT WEB FORMS.
  • raghulvarma
    New Member
    • Oct 2007
    • 90

    #2
    u could use joins in order to get the data of worker.

    Comment

    • waterfall
      New Member
      • Dec 2007
      • 22

      #3
      Originally posted by raghulvarma
      u could use joins in order to get the data of worker.
      im not asking for the SQl query.im asking for the code for textbox.
      im having problem with displaying data in textboxes.

      Comment

      • nateraaaa
        Recognized Expert Contributor
        • May 2007
        • 664

        #4
        Originally posted by waterfall
        How to get data from database in text box on selecting a value from combobox in C# web forms?
        Im using two tables employee and worker table.i succeeded in populating combobox with data from SQL employee table.When I click employee’s data in combobox I cant display related data from worker table in textbox. plz help me….
        I KNOW HOW TO DO THIS IN WINDOWS FORMS . PLZ TELL ME ABOUT WEB FORMS.
        I would recommend that you create a stored procedure that returns data for an employee and a worker. You can then fill a dataset with this information and use the data from the dataset to fill the combobox as well as display related data in your textbox. You can use the SelectedIndexCh anged event of your combobox to automatically populate the textbox with the data that you want.

        Nathan

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by waterfall
          How to get data from database in text box on selecting a value from combobox in C# web forms?
          Im using two tables employee and worker table.i succeeded in populating combobox with data from SQL employee table.When I click employee’s data in combobox I cant display related data from worker table in textbox. plz help me….
          I KNOW HOW TO DO THIS IN WINDOWS FORMS . PLZ TELL ME ABOUT WEB FORMS.
          I'm a little confused here.

          Are you developing a Web application or Desktop application?
          If you're developing an ASP.NET application, there is no such thing as comboboxes...in stead there are DropDownLists.

          Anyways, you should store the primary key for each employee in your Employee DropDownList as the value for your ListItems in the DropDownList.

          This way when the person selects an employee in your DropDownList, you can use the SelectedValue to retrieve the data from your database for their information.

          Make sure to configure the DropDownList so that it AutoPostback is set to True. This will post back to the server when the user selects something from the DropDownList ...so that you can display the data from the database when the SelectedIndexCh ange event is raised.

          -Frinny

          Comment

          • waterfall
            New Member
            • Dec 2007
            • 22

            #6
            Im gonna try what you suggested.
            Im developing web application using DropDownList.(y ou pointed out the right thing).and using stored procedures.Im populating DropDownList with employee name.When i debug my app. it runs quite successfully showing employee name inDropDownList but doesnt show any value in textboxes.altho ugh ive written stored procedures for showing worker's data in textbox.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Originally posted by waterfall
              Im gonna try what you suggested.
              Im developing web application using DropDownList.(y ou pointed out the right thing).and using stored procedures.Im populating DropDownList with employee name.When i debug my app. it runs quite successfully showing employee name inDropDownList but doesnt show any value in textboxes.altho ugh ive written stored procedures for showing worker's data in textbox.
              Please post any relevant code so that we can help you solve your problem :)

              -Frinny

              Comment

              • waterfall
                New Member
                • Dec 2007
                • 22

                #8
                The code for populating DropDownList is:
                [code=cpp]
                protected void Page_Load(objec t sender, EventArgs e)
                {
                name_combo.Item s.Clear();


                DataAccess data = null;

                data = new DataAccess();
                SqlConnection oConnection = null;



                oConnection = new SqlConnection(" server=ABC -F78782C30;uid=s a;pwd=sasa;data base=Employee") ;

                data.openConnec tion();
                data.ProcName = "DisplayEmploye eName";
                data.CreateComm and();

                data.Parameters ("@name", SqlDbType.VarCh ar, 50, name_combo.Sele ctedValue);

                SqlDataReader dr = data.ExecuteDat aReader();
                name_combo.Data Source = dr;

                name_combo.Data TextField = "name";
                name_combo.Data ValueField = "name";
                name_combo.Data Bind();


                data.openConnec tion();
                data.DisposeCon nection();

                }
                [/code]

                THe code for displaying data in textbox is :
                [code=cpp]
                protected void name_combo_Sele ctedIndexChange d(object sender, EventArgs e)
                {
                dept = name_combo.Sele ctedValue;
                SqlConnection con1 = null;
                con1 = new SqlConnection(" server=ABC-F78782C30;uid=s a;pwd=sasa;data base=Emp");
                DataAccess dataB = null;
                dataB = new DataAccess();
                dataB.openConne ction();

                dataB.ProcName = "Worker_Shift_D etail";
                dataB.CreateCom mand();

                dataB.Parameter s("@shift_detai l", SqlDbType.VarCh ar, 50, shift_box.Text) ;
                SqlDataReader drB = dataB.ExecuteDa taReader();
                //return drB;

                drB.Read();

                if (drB.GetValue(0 ).Equals(null))
                {
                //Response.Write( "Not reading data");

                }
                else
                {

                name_combo.Sele ctedValue = drB.GetValue(1) .ToString();
                shift_box.Text = drB.GetValue(2) .ToString();


                }
                SqlConnection con2 = null;
                con2 = new SqlConnection(" server=ABC-F78782C30;uid=s a;pwd=sasa;data base=Emp");
                DataAccess dataC = null;
                dataC = new DataAccess();
                dataC.openConne ction();

                dataC.ProcName = "Worker_Departm ent_Info";
                dataC.CreateCom mand();
                dataC.Parameter s("@department" , SqlDbType.VarCh ar, 50, depart_box.Text );
                SqlDataReader drC = dataC.ExecuteDa taReader();
                //return drB;

                drC.Read();

                if (drC.GetValue(0 ).Equals(null))
                {
                //Response.Write( "Not reading data");

                }
                else
                {

                name_combo.Sele ctedValue = drC.GetValue(3) .ToString();
                depart_box.Text = drB.GetValue(4) .ToString();

                }
                }
                [/code]



                Plz help me..ive been stucked here 4 a couple of weeks.im so desperate about it.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Originally posted by waterfall
                  The code for populating DropDownList is:
                  ...
                  Plz help me..ive been stucked here 4 a couple of weeks.im so desperate about it.
                  Could you please post your code within [code] tags in the future. For more information on how to use them please see the posting guidelines on how to ask a question.

                  I've never used the DataAccess class before to connect to a database.
                  What type of database are you using?

                  -Frinny

                  Comment

                  • waterfall
                    New Member
                    • Dec 2007
                    • 22

                    #10
                    Im using SQl server 2005.

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      Originally posted by waterfall
                      Im using SQl server 2005.
                      Check out this article on how to use a database in your program.

                      It may help.

                      Comment

                      • waterfall
                        New Member
                        • Dec 2007
                        • 22

                        #12
                        sorry i didnt work.PLZ help me.My prog. runs successfully without any error but i cant get value in textbox.
                        ive searched alot but i get help for windows forms and i dont know how to convert it for C# web form.

                        Comment

                        • chaitanya1167
                          New Member
                          • Dec 2007
                          • 8

                          #13
                          I think Problem solves using DataReader

                          Comment

                          • waterfall
                            New Member
                            • Dec 2007
                            • 22

                            #14
                            Im already using SqlDataReader.. .if u check out my coding..
                            i dont know what's the problem.

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              Originally posted by waterfall
                              Im already using SqlDataReader.. .if u check out my coding..
                              i dont know what's the problem.
                              You're using the DataAccess Class in the above posted code (not the SqlDataRead class).

                              Could you please post your code again if it is not the same.

                              Also, could you post any errors that you may be getting.

                              -Frinny

                              Comment

                              Working...