|  | 
	                
  
    |  |  
    | 
        
        datetime:Conversion failed when converting date and/or time from character string.     |  
    |  |  
 
	
		|  |  |  |  |  
		|  |  | 
          
            | ผม run จาก VS2010 แล้วมันจะบันทึกลงฐานข้อมูลได้ปกตินะครับ แต่พอ publish และเพิ่มข้อมูล มันจะเกิด error :Conversion failed when converting date and/or time from character string. และผมก็ลอง debug เอา sql code ไป run  ใน sql server  เองก็ไม่เกิดปัญหาอะไร เพิ่มได้ปกติ
 รบกวนช่วยชี้จุดด้วยนะครับ
 Code (C#)
 
  protected void btnUpDateInterested_Click(object sender, EventArgs e)
        {
                      
            objConn = new SqlConnection(strConnString);
            objConn.Open();
            objCmd = new SqlCommand();
            objCmd.Connection = objConn;
            if (chkIDCardPeople(requestIDCardPeople))
            {
                Boolean countchbListProgram = false;//ตรวจสอบ checklistbox มีการเลือกหรือมไม่
                for (int i = 0; i <= this.chbListProgram.Items.Count - 1; i++)
                {
                    if (this.chbListProgram.Items[i].Selected == true)
                    {
                       countchbListProgram = true;//ตรวจสอบว่ามีการ ใส่ checkbox หรือไม่
                        string itemProject = this.chbListProgram.Items[i].Text;
                        string  itemIndex = this.chbListProgram.Items[i].Value;
                        // Response.Write("<br/>" + itemProject + "Value = " + itemIndex + "<BR/>");
                      string  strSQLFillInterested = "INSERT INTO PeopleProjects(" +
                                "IDCard,ProjectID,CreatedDate,CreatedBy" +
                                ")" +
                                "VALUES ('" + requestIDCardPeople + "'," + itemIndex + ",'"+ DateTime.Now +"','" + Session["UserName"].ToString() + "')";
                        objCmd.CommandText = strSQLFillInterested;
                        objCmd.CommandType = CommandType.Text;
                        try
                        {
                            objCmd.ExecuteNonQuery();
                            //Response.Write("<b><font color='red'>Fill Interested successful</font></b>");
                        }
                        catch (Exception ex)
                        {
                            Response.Write("<b><font color='red'>Fill Interested failed (" + ex.Message + ")</font></b>");
                            return;
                        }
                      
                    }
                }
                if (countchbListProgram)
                {
                    Response.Write("Fill Interested successful");
                    Response.Redirect("ListPeople.aspx");
                }
                else
                {
                    Response.Write("Fill Interested Unsuccessful,Not fill checkbox");
                }
                objConn.Close();
                objConn = null;
                
            }
            else
            {
                //ไม่พบ id card ที่ระบุ
                Response.Write("คุณระบ id card ไม่ถูกต้อง กรุณากลับไปหน้า List People");
                return;
            }
            
 
        }
        private bool chkIDCardPeople(string requestIDCardPeople)
        {
            objConn = new SqlConnection(strConnString);
            objConn.Open();
            SqlDataAdapter objAdapter;
            DataTable dtChk = new DataTable();
            string strSQLchkIDCard = "SELECT * FROM People WHERE IDCard = '" + requestIDCardPeople + "'";
            objAdapter = new SqlDataAdapter(strSQLchkIDCard, objConn);
            objAdapter.Fill(dtChk);
            if (dtChk.Rows.Count > 0)
            {
                return true;
            }
            return false;
            
        }
 ขอคนที่ไม่เอาลิ้งมาทิ้งแล้วหายไป
 
 
 
 Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), C#, VS 2010 (.NET 4.x)
 
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2010-12-13 15:39:33 | By :
                          chon2008 | View :
                          11409 | Reply :
                          7 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |  
		|  |  |  |  |  
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ลองดูค่า datetime.now ครับว่ามีค่าเป็น อะไรแล้วก็ดูที่ field CreatedDate ด้วยครับว่า type เป็นอะไร 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-12-13 17:57:11 | By :
                            kanchen |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              |  .
 ..
 .
 
  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-12-14 09:48:35 | By :
                            chon2008 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ใครก็ได้ช่วยทีครับ debug บนเครื่องผมเองมันทำได้ครับ แต่พอเอาขึ้นเซิฟ มันเกิด error 
 The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-12-14 15:49:55 | By :
                            chon2008 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | รบกวนช่วยชี้แนะด้วยครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-12-19 14:32:21 | By :
                            chon2008 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ให้เลือกทำอย่างใดอย่างหนึ่งในสามข้อนี้ 
 1. sql datetime คุณต้องเปลี่ยนให้เป็น format yyyy-MM-ssTmm:hh:ss
 โดยใช้
 
 string datetimestr = DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss", new System.Globalization.CultureInfo("en-US", true));
 
 2. sql command paramenter
 
 cmd.Parameters.AddWithValue("paramenter name", "object of value");
 
 3. เปลี่ยน command query ไปเลย โดย createdate = getdate() เพราะ getdate() คือใช้เวลของเครื่อง server
 
 string  strSQLFillInterested = "INSERT INTO PeopleProjects(" +
 "IDCard,ProjectID,getdate(),CreatedBy" +
 ")" +
 "VALUES ('" + requestIDCardPeople + "'," + itemIndex + ",'"+ DateTime.Now +"','" + Session["UserName"].ToString() + "')";
 
 ผมจะบอกว่า วิธีที่ดีที่สุดคือ เรียง 3 2 1 ครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-12-19 20:47:39 | By :
                            numenoy |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              |  ตอบความคิดเห็นที่ : 5 เขียนโดย : numenoy เมื่อวันที่ 2010-12-19 20:47:39 
 รายละเอียดของการตอบ ::
 สุดยอดเลยครับ แก้ได้แล้วครับ ขอบคุณมากเลยครับ ผมทำตามวิธีที่ 3 ถ้าอยู่ใกล้จะโดดหอมแก้มเลยครับ ฮ่าๆ
Code (C#)ก่อนแก้ไข                          string  strSQLFillInterested = "INSERT INTO [PeopleProjects](" +
                                "[IDCard],[ProjectID],[CreatedDate],[CreatedBy]" +
                                ")" +
                                " VALUES ("+
                                "'" + requestIDCardPeople + 
                                "'," + itemIndex +
                                ",'"+ DateTime.Now +"','" + createby + 
                                "')";
Code (C#) อันนี้แก้ไขแล้วstring  strSQLFillInterested = "INSERT INTO [PeopleProjects](" +
                                "[IDCard],[ProjectID],[CreatedDate],[CreatedBy]" +
                                ")" +
                                " VALUES ("+
                                "'" + requestIDCardPeople + 
                                "'," + itemIndex +
                                ",GETDATE(),'" + createby + 
                                "')";
 |  
              | 
 ประวัติการแก้ไข
 2010-12-20 12:42:16
 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-12-20 12:39:45 | By :
                            chon2008 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | private void saveButton_Click(object sender, EventArgs e) {
 SqlConnection objconn = new SqlConnection();
 SqlCommand objcmd = new SqlCommand();
 string strConnString = null;
 
 strConnString = @"Data Source = 127.0.0.1; Initial Catalog=project; user=tuta; password=1234;";
 
 objconn.ConnectionString = strConnString;
 objconn.Open();
 string sql = "INSERT INTO Employees(Employee_id,Card_id,Employee_name,Sex,Phone,Addres,Wage,Worke_date,Username,Passwordd)values(@Employee_id,@Card_id,@Employee_name,@Sex,@Phone,@Addres,@Wage,@Worke_date,@Username,@Passwordd)";
 SqlCommand comm = new SqlCommand(sql, conn);
 comm.Connection = objconn;
 comm.CommandType = CommandType.Text;
 comm.CommandText = sql;
 
 comm.Parameters.Add("@Employee_id", SqlDbType.NVarChar).Value = textem_id.Text;
 comm.Parameters.Add("@Card_id", SqlDbType.NVarChar).Value = card_idTextBox1.Text;
 comm.Parameters.Add("@Employee_name", SqlDbType.NVarChar).Value = textname.Text;
 comm.Parameters.Add("@Sex", SqlDbType.NVarChar).Value = textBoxsex.Text;
 comm.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = textBoxphone.Text;
 comm.Parameters.Add("@Addres", SqlDbType.NVarChar).Value = textBoxadd.Text;
 comm.Parameters.Add("@Wage", SqlDbType.NVarChar).Value = textBoxwage.Text;
 comm.Parameters.Add("@Worke_date", SqlDbType.NVarChar).Value = dateTimePicker1.Text;
 comm.Parameters.Add("@Username", SqlDbType.NVarChar).Value = textBox1user.Text;
 comm.Parameters.Add("@Passwordd", SqlDbType.NVarChar).Value = textBox2pass.Text;
 
 int numRec = comm.ExecuteNonQuery();
 if (numRec > 0)
 {
 MessageBox.Show("คุณต้องการบันทึกข้อมูลหรือไม่", "ยืนยันการบันทึก", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
 }
 else
 {
 MessageBox.Show("ไม่สามารถเพิ่มข้อมูลได้");
 }
 [color=green][color=black][/color][/color]
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-09-19 15:54:37 | By :
                            ปุ่มบันทึกเพิ่มไม่ได้ครับ |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |  |