|  | 
	                
  
    |  |  
    | 
        
        ผมสามารถโชว์ภาพที่อยู่ตาราง  ของฐานข้อมูลได้แล้วครับ เลยเอา โค๊ดมาฝาก  แต่ติดที่ เมื่อเจอตารางถาพที่เป็น Null ทำไม่มัน Err     |  
    |  |  
 
	
		|  |  |  |  |  
		|  |  | 
          
            | ผมสามารถโชว์ภาพที่อยู่ตาราง  ของฐานข้อมูลได้แล้วครับ เลยเอา โค๊ดมาฝาก  แต่ติดที่ เมื่อเจอตารางถาพที่เป็น Null ทำไม่มัน Err แล้วทำไมมันไม่เข้า Err  ในตำแหน่ง ที่เราตรวจเช็ค กลับไป Err ตำแหน่ง นอกสุด แปลว่ามันไม่เข้าใน ลูปของเราใช่หรือไม่หรือเราเขียนผิดตำแหน่ง  อื่ม  ....
 Code (C#)
 
                 try
                {
                    TextFind = textBox1.Text;
                    strSQL = "SELECT IDTeacher, PreName, NameFirst" +
                             ", NameLast, Status, Sex, TPosition, Office, PositionNew, PicPresent" +
                             " FROM TTeacher" +
                             " WHERE (Status = 1) AND (IDTeacher ="+ textBox1.Text +")";
                    
                    cnSQL = new SqlConnection(Conn);
                    cnSQL.Open();
                    cmSQL = new SqlCommand(strSQL, cnSQL);
                    drSQL = cmSQL.ExecuteReader();
                   
                    if (drSQL.Read())
                    {
                        string chkType = "";
                        txtID.Text = drSQL["IDTeacher"].ToString();
                        txtPer.Text = drSQL["PreName"].ToString();
                        txtFName.Text = drSQL["NameFirst"].ToString();
                        txtLName.Text = drSQL["NameLast"].ToString();
                        chkType = drSQL["Sex"].ToString();
                        if (chkType == "1")
                        {
                            rdoMane.Checked = true;
                        }
                        else
                        {
                            rdoFamale.Checked = true;
                        }
                        txtPLevel.Text = drSQL["Office"].ToString();
                        txtClassRoom.Text = drSQL["TPosition"].ToString();
                        txtGroup.Text = drSQL["PositionNew"].ToString();
                       
                        Byte[] byteBLOBData = new Byte[0];
                        byteBLOBData = (Byte[])drSQL["PicPresent"];
                        MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
                        if (stmBLOBData != null)
                        {
                          pictureBox1.Image = Image.FromStream(stmBLOBData, false, true);                           
                        }
                        else
                        {
                            MessageBox.Show("ไม่มีภาพ", "ข้อผิดผลาด");
                            pictureBox1.Image = null;                           
                        }
                       
                    }
                    drSQL.Close();
                    cnSQL.Close();
                    cmSQL.Dispose();
                    cnSQL.Dispose();
                }
                catch (SqlException e)
                {
                    MessageBox.Show(e.Message, "SQL Error");
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "General Error");
                }
            }
 
  
 
 
 Tag : .NET, Ms SQL Server 2008, C#, VS 2010 (.NET 4.x)
 
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2011-09-08 09:24:55 | By :
                          chowarin | View :
                          1179 | Reply :
                          3 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |  
		|  |  |  |  |  
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | try ใส่ได้ 1 catch หรือเปล่าครับ อันนี้ผมไม่แน่ใจ 
 ถ้าจะดักตรง sql ก็เอา try อีกอันหนึ่งไปคลุมตรง sql เอาไว้
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2011-09-08 09:40:30 | By :
                            Marcuz |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Code (C#) 
 if (drSQL.Read())
{
     Try
     {
            string chkType = "";
            txtID.Text = drSQL["IDTeacher"].ToString();
            txtPer.Text = drSQL["PreName"].ToString();
            txtFName.Text = drSQL["NameFirst"].ToString();
            txtLName.Text = drSQL["NameLast"].ToString();
            chkType = drSQL["Sex"].ToString();
            if (chkType == "1")
            {
                rdoMane.Checked = true;
            }
            else
            {
                rdoFamale.Checked = true;
            }
            txtPLevel.Text = drSQL["Office"].ToString();
            txtClassRoom.Text = drSQL["TPosition"].ToString();
            txtGroup.Text = drSQL["PositionNew"].ToString();
           
            Byte[] byteBLOBData = new Byte[0];
            byteBLOBData = (Byte[])drSQL["PicPresent"];
            MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
            if (stmBLOBData != null)
            {
              pictureBox1.Image = Image.FromStream(stmBLOBData, false, true);                           
            }
            else
            {
                MessageBox.Show("ไม่มีภาพ", "ข้อผิดผลาด");
                pictureBox1.Image = null;                           
            }
     }
     catch (Exception e)
     {
                MessageBox.Show(e.Message, "SQL Error");
     }
}
 |  
              | 
 ประวัติการแก้ไข
 2011-09-08 09:46:34
 2011-09-08 09:50:15
 2011-09-08 09:51:44
 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2011-09-08 09:45:22 | By :
                            Marcuz |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | แก้ไขได้แล้วครับ   เลยเอามาฝาก  สามารถใส่ Catch ได้ 2 อัน ครับ เพราะดัก Err คนละอย่าง คือ SqlException และ Exception try{................ }catch(SqlException){.............}catch(SqlException){......}
 Code (C#)
 
 private void TextShowData()
        {
            SqlConnection cnSQL;
            SqlCommand cmSQL;
            SqlDataReader drSQL;
            string strSQL;
           
            if (rdbStudunts.Checked == true)
            {
                try
                {
                   
                    strSQL = "SELECT IDStudent, PreName, NameFirst" +
                           ", NameLast, Sex, Status, PLevel, PClass, PRoom " +
                           " FROM TBStudent " +
                           " WHERE (Status = 1) AND (IDStudent=" + textBox1.Text +")";
                    
                    cnSQL = new SqlConnection(Conn);
                    cnSQL.Open();
                    cmSQL = new SqlCommand(strSQL, cnSQL);
                    drSQL = cmSQL.ExecuteReader();
                    if (drSQL.Read())
                    {
                        string chkType = "";
                        txtID.Text = drSQL["IDStudent"].ToString();
                        txtPer.Text = drSQL["PreName"].ToString();
                        txtFName.Text = drSQL["NameFirst"].ToString();
                        txtLName.Text = drSQL["NameLast"].ToString();
                        chkType = drSQL["Sex"].ToString();
                        if (chkType == "1")
                        {
                            rdoMane.Checked = true;
                        }
                        else
                        {
                            rdoFamale.Checked = true;
                        }
                        txtPLevel.Text = drSQL["PLevel"].ToString();
                        txtClassRoom.Text = drSQL["PClass"].ToString() + "/" + drSQL["PRoom"].ToString();
                    }
                    drSQL.Close();
                    cnSQL.Close();
                    cmSQL.Dispose();
                    cnSQL.Dispose();
                }
                catch (SqlException e)
                {
                    MessageBox.Show(e.Message, "SQL Error/ข้อผิดผลาดคำสั่ง SQL");
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "General Error/ข้อผิดผลาดอื่น ๆ");
                }
            }
            else if(rdbHumman.Checked==true)
            {
                try  //Try 2
                {
                   //เลือกจากตารางบุคคลากร
                    strSQL = "SELECT IDTeacher, PreName, NameFirst" +
                             ", NameLast, Status, Sex, TPosition, Office, PositionNew, PicPresent" +
                             " FROM TTeacher" +
                             " WHERE (Status = 1) AND (IDTeacher ="+ textBox1.Text +")";
                    
                    cnSQL = new SqlConnection(Conn);
                    cnSQL.Open();
                    cmSQL = new SqlCommand(strSQL, cnSQL);
                    drSQL = cmSQL.ExecuteReader();
                    
                        if (drSQL.Read())
                        {
                            string chkType = "";
                            txtID.Text = drSQL["IDTeacher"].ToString();
                            txtPer.Text = drSQL["PreName"].ToString();
                            txtFName.Text = drSQL["NameFirst"].ToString();
                            txtLName.Text = drSQL["NameLast"].ToString();
                            chkType = drSQL["Sex"].ToString();
                            if (chkType == "1")
                            {
                                rdoMane.Checked = true;
                            }
                            else
                            {
                                rdoFamale.Checked = true;
                            }
                            txtPLevel.Text = drSQL["Office"].ToString();
                            txtClassRoom.Text = drSQL["TPosition"].ToString();
                            txtGroup.Text = drSQL["PositionNew"].ToString();
                            if (drSQL["PicPresent"] == System.DBNull.Value)
                            {
                                pictureBox1.Image = null;
                            }
                            else
                            {
                                Byte[] byteBLOBData = new Byte[0];
                                byteBLOBData = (Byte[])drSQL["PicPresent"];
                                MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
                                this.pictureBox1.Image = Image.FromStream(stmBLOBData, false, false);
                            }
                                
                        
                        
                    
                    }
                    drSQL.Close();
                    cnSQL.Close();
                    cmSQL.Dispose();
                    cnSQL.Dispose();
                } //จบ Try 2
                catch (SqlException e)
                {
                    MessageBox.Show(e.Message, "SQL Error/ข้อผิดผลาดคำสั่ง SQL");
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "General Error/ข้อผิดผลาดอื่น ๆ");
                }
            }//end else if
        }
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2011-09-09 11:12:37 | By :
                            chowarin |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |  |