|  |  | 
          
            | ผมต้องการ get ค่าจาก text Box ที่สร้างขึ้นเองจาก dropdown แต่เมื่อใช้ FIndcontrol แล้ว ใช้ Hn.text เพื่อ get ค่า  แต่มัน error ที่  Hn.text ครับ รบกวนช่วยหน่อยครับ ว่าผิดตรงไหน 
 ใน Code ผมลองบันทึกแค่ค่าของ Hn อย่างเดียวนะครับ Error: System.NullReferenceException: Object reference not set to an instance of an object ตัวนี้ครับ
 
 MyFrmAppList.ascx
 
 Code (C#)
 
 void ddlNum_Changed(Object sende, EventArgs e)
        {
            Label No;
            TextBox Hn;
            TextBox firstname;
            TextBox lastname;
            TextBox telephone;
            int i;
            for (i = 0; i <= Convert.ToInt32(this.ddlNum.SelectedItem.Value); i++)
            {
                No = new Label();
                No.Text = i.ToString() + "<br>";
                No.ID = "no" + (i+1).ToString();
                Hn = new TextBox();
                Hn.ID = "hn" + (i+1).ToString();
                Hn.Text = "";
              
                
                firstname = new TextBox();
                firstname.ID = "firstname" + i;
                lastname = new TextBox();
                lastname.ID = "lastname" + i;
                telephone = new TextBox();
                telephone.ID = "telephone" + i;
                
                
                this.lblno.Controls.Add(No);
                
                this.lblhn.Controls.Add(Hn);//lblHn คือ Panel ที่ผมสร้างไว้แสดง TextBox
                this.fname.Controls.Add(firstname);
                this.lname.Controls.Add(lastname);
                this.lbltelephone.Controls.Add(telephone);
                
            }
        }    
    
        void Page_Load(object sender, EventArgs e)
        {
            strConnString = "Server=localhost;User Id=root; Password=35860; Database=advanceappointment; Pooling=false";
            objConn.ConnectionString = strConnString;
            objConn.Open();
            CalendarExtender1.SelectedDate = DateTime.Parse(Session["Data"].ToString());
            
        }
        void Checkbox_selected(object sender, EventArgs e)
        {
            this.lblcheckbox.Text = "";
            for (int i = 0; i <= this.CheckBoxListAddit.Items.Count - 1; i++)
            {
                if (this.CheckBoxListAddit.Items[i].Selected == true)
                {
                    this.lblcheckbox.Text = this.lblcheckbox.Text + "," + this.CheckBoxListAddit.Items[i].Value; //*** Or this.myCBoxList4.Items[i].Text ***//
                }
            }
        }
        void btnsubmit_Click(Object sender, EventArgs e)
        {
            this.Panel3.Visible = true;
            for (int i = 0; i <= 2; i++)
            {
                String boxhn = "hn" + (i+1).ToString();
                TextBox Hn = (TextBox)(this.lblhn.FindControl(boxhn));
               
                strSQL = "INSERT INTO user_with_ultra (HNID,FName,LName,Tel,Date,DateBook,Program,Additional,Comment,StaffName,Status)" +
                    " VALUES " +
                    " ('" + Hn.Text + "','" + 
                    ""+ "','" +
                    "" + "','" +
                    "" + "','" +
                    Datenow + "','" +
                    this.dateselected.Text + "','" +
                    this.DropDownListprogram.SelectedItem.Text + "','" +
                    this.lblcheckbox.Text + "','" +
                    this.txtcomment.Text + "','" +
                    this.txtstaffname.Text + "','" +
                    this.DropDownListstatus.SelectedItem.Text + "')";
                    objCmd = new MySqlCommand();
                    objCmd.Connection = objConn;
                    objCmd.CommandText = strSQL;
                    objCmd.CommandType = CommandType.Text;
                    this.pnlAdd.Visible = false;
                    try
                    {
                        objCmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        this.lblStatus.Visible = true;
                        this.lblStatus.Text = "Record can not insert Error (" + ex.Message + ")";
                    }
                }
            }
 
 
 Tag : .NET, MySQL, Web (ASP.NET), C#
 
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2011-11-06 18:00:46 | By :
                          zytofle | View :
                          914 | Reply :
                          3 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |