2013-09-24 3 views
1

winform 프로젝트를위한 의제 기능을 만들려고합니다. 사용자가 monthCalendar 컨트롤에서 날짜를 선택하면 특정 날짜에 대해 textBox에 데이터베이스 레코드를 표시하려고합니다. 아래에서 내 db 테이블 디자인, 내 winform 디자인 및 내 코드 및 예외 메시지를 볼 수 있습니다. 이 문제를 어떻게 해결할 수 있습니까?calendarMonth를 클릭하여 textBox의 내용을 변경하십시오.

* ps : 매개 변수화 된 쿼리 사용을 제안하지 않아도됩니다. 내가 그리고 난 결국 사용자가 날짜를 선택하면 해고 될 MonthCalendar 컨트롤의

enter image description here

enter image description here

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

using System.Data.SqlClient; 

namespace EKS 
{ 
    public partial class Agenda : Form 
    { 
     public Agenda() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      panel1.BackColor = Color.FromArgb(100, 88, 55, 55); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      try { 

       string myQuery = "insert into agenda (input_agenda, input_date) values ('"+textBox1.Text.ToString()+"', '"+ monthCalendar1.SelectionStart +"')"; 

       SqlConnection myConn = new SqlConnection(); 
       myConn.ConnectionString = "Data Source=localhost; database=EKS; uid=sa; pwd=123; connection lifetime=20; connection timeout=25; packet size=1024;"; 

       SqlCommand myComm = new SqlCommand(); 
       myComm.Connection = myConn; 

       myComm.CommandText = myQuery; 

       myConn.Open(); 
       myComm.ExecuteNonQuery(); 
       myConn.Close(); 

       MessageBox.Show("agenda updated"); 
      } 
      catch (Exception x) { 
       MessageBox.Show(x.ToString()); 
      } 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      try { 
       string deleteQuery = "DELETE FROM agenda WHERE input_date = '" + monthCalendar1.SelectionStart +"'"; 
       SqlConnection myConn = new SqlConnection(); 
       myConn.ConnectionString = "Data Source=localhost; database=EKS; uid=sa; pwd=123; connection lifetime=20; connection timeout=25; packet size=1024;"; 

       SqlCommand myComm = new SqlCommand(); 
       myComm.Connection = myConn; 

       myComm.CommandText = deleteQuery; 

       myConn.Open(); 
       myComm.ExecuteNonQuery(); 
       myConn.Close(); 

       MessageBox.Show("delete succeeded"); 
      } 
      catch(Exception x){ 
       MessageBox.Show(x.ToString()); 
      } 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 

     private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e) 
     { 
      GetAgendaDetails(e.Start.Date); 
     } 

     private void GetAgendaDetails(DateTime x){ 
      string myQuery = "select input_agenda from agenda where input_date = '" + x.Date.ToString() + "'"; 
      SqlConnection myConn = new SqlConnection(); 
      myConn.ConnectionString = "Data Source=localhost; database=EKS; uid=sa; pwd=123; connection lifetime=20; connection timeout=25; packet size=1024;"; 

      try { 
       myConn.Open(); 
       SqlDataReader myReader = null; 
       SqlCommand myCommand = new SqlCommand(myQuery,myConn); 
       myReader = myCommand.ExecuteReader(); 
       while (myReader.Read()) { 
        textBox1.Text = myReader.GetString(100); 
       } 
       myConn.Close(); 
      } 
      catch(Exception z){ 
       MessageBox.Show(z.ToString()); 
      } 
     } 
    } 
} 

enter image description here

답변

3

사용 DateSelected 이벤트를이 변경됩니다 수 있습니다.

private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e) 
    { 
     AganedaInformation info = GetAgendaDetails(e.Start.Date); 
    } 

Add a private method to query the database based on the passed selected date

Private AganedaInformation GetAgendaDetails(DateTime selectedDate) 
{ 
    //Add logic to query the database with the selected date and return the information 
} 
+0

비주얼 스튜디오의 IntelliSense를 말한다 : 형식 또는 네임 스페이스 이름 'AganedaInformation'을 (를) 찾을 수 없습니다 (당신은 using 지시문 또는 어셈블리 referance에 누락?) –

+1

@ TimurAykutYıldırım :) . Aganeda 정보를 호출자에게 반환 할 수있는 클래스 이름이 추가되었습니다. 원하는 경우 void로 변경하고 동일한 에피소드 내에서 텍스트 필드 값을 설정할 수 있습니다. – Kurubaran

+0

귀하의 조언에 따라 코드를 업데이트했습니다. 매일의 일정 내용 캐릭터 양이 다른 동안 어떻게 GetString() 메서드에 대한 매개 변수를 지정할 수 있습니까? 또한 예외의 스크린 샷을 추가했습니다. –

관련 문제