| |

VerySource

 Forgot password?
 Register
Search
View: 1170|Reply: 11

Which big brother can help me see what is wrong with the binding of DropDownList and DataGrid?

[Copy link]

1

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-2 00:20:01
| Show all posts |Read mode
protected void Page_Load (object sender, EventArgs e)
    {
        if (! Page.IsPostBack)
        {
            this.Bind ();
        }
    }
    private void Bind ()
    {
        SqlConnection conn = DB.createcon ();
        conn.Open ();
        SqlCommand cmd = new SqlCommand ("select class, course from teaching plan, subject, teacher status table where teaching plan. Teacher = teacher status table. Teacher number and subject. ID = teaching plan. ID and teacher status table. Name = '" + Session ["TeacherName"]. ToString () + "'", conn);
        SqlDataReader sda = cmd.ExecuteReader ();
        // DataSet dss = new DataSet ();
        //cmd.Fill(dss);
        //this.DropDownList1.DataSource = dss;
        //this.DropDownList1.DataBind ();
        while (sda.Read ())
        {
            this.DropDownList1.DataSource = sda;
            this.DropDownList1.DataTextField = "Class";
            this.DropDownList1.DataValueField = "course";
            this.DropDownList1.DataBind ();
        }
        sda.Close ();

        SqlDataAdapter sdaa = new SqlDataAdapter ("select distinct name from basic information of student enrollment, teaching plan where basic information of student enrollment. Class = teaching plan. Class", conn);
        DataSet ds = new DataSet ();
        sdaa.Fill (ds);
        this.DataGrid1.DataSource = ds;
        this.DataGrid1.DataBind ();
        conn.Close ();
    }

    protected void DropDownList1_SelectedIndexChanged (object sender, EventArgs e)
    {
        SqlConnection conn = DB.createcon ();
        conn.Open ();
        SqlDataAdapter sdaa = new SqlDataAdapter ("Select distinct name from basic information of student enrollment, teaching plan where basic information of student enrollment. Class = teaching plan. Class and basic information of student enrollment. Class = '" + DropDownList1.SelectedValue + "'," ;
        DataSet ds = new DataSet ();
        sdaa.Fill (ds);
        this.DataGrid1.DataSource = ds;
        this.DataGrid1.DataBind ();
        conn.Close ();
    }
 protected void DataGrid1_PageIndexChanged1 (object source, DataGridPageChangedEventArgs e)
    {
        this.DataGrid1.SelectedIndex = e.NewPageIndex;
        SqlConnection conn = DB.createcon ();
        conn.Open ();
        SqlDataAdapter sdaa = new SqlDataAdapter ("select distinct name from basic information of student enrollment, teaching plan where basic information of student enrollment. Class = teaching plan. Class", conn);
        DataSet ds = new DataSet ();
        sdaa.Fill (ds);
        this.DataGrid1.DataSource = ds;
        this.DataGrid1.DataBind ();
        conn.Close ();
    }
Reply

Use magic Report

0

Threads

43

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-3-19 21:45:01
| Show all posts
while (sda.Read ())
        {
            this.DropDownList1.Items.Add (sda.item (""))
        }
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-20 00:45:02
| Show all posts
What error is reported after debugging?
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-3-20 07:15:01
| Show all posts
while (sda.Read ())
        {
            this.DropDownList1.DataSource = sda;
            this.DropDownList1.DataTextField = "Class";
            this.DropDownList1.DataValueField = "course";
            this.DropDownList1.DataBind ();
        }

===> Remove while (sda.Read ())
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-3-20 08:15:01
| Show all posts
What's wrong?
Reply

Use magic Report

1

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-20 13:45:01
| Show all posts
DropDownList cannot be bound and DataGrid and DropDownList are not bound. I don't know why.
Reply

Use magic Report

1

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-20 16:00:01
| Show all posts
It doesn't work to remove while (sda.Read ()), I tried
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-20 16:45:01
| Show all posts
while (sda.Read ())
        {
            this.DropDownList1.DataSource = sda;
            this.DropDownList1.DataTextField = "Class";
            this.DropDownList1.DataValueField = "course";
            this.DropDownList1.DataBind ();
        }
        sda.Close ();
Change to
 if (sda.Read ())
        {
            this.DropDownList1.DataSource = sda;
            this.DropDownList1.DataTextField = "Class";
            this.DropDownList1.DataValueField = "course";
            this.DropDownList1.DataBind ();
        }
        sda.Close ();
Reply

Use magic Report

3

Threads

17

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-3-20 20:15:02
| Show all posts
Still not? I stole my code and posted it online, kid ...
There is no problem debugging on my machine. It is very likely that there is a problem with the connection between the table and the table in the database ...
Reply

Use magic Report

1

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-20 22:45:01
| Show all posts
There is no problem with the watch. I have tried it N times.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list