  | 
              
	              
	                
  
    |   | 
   
  
    
        
        ช่วยด้วยค้าบ ! ติดปัญหาการเอาข้อมูลเก็บใส่ DB มือใหม่หัดใช้ Sql บน C#     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                Code (C#) 
SqlCommand comm = new SqlCommand("Select * From location order by locate_id", conn);
            SqlDataReader dr = comm.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            txtcusLocate.DataSource = dt;
            txtcusLocate.DisplayMember = "locate_name"; 
 
คือ Code ส่วนนี้ผมดึงข้อมูลจังหวัดจาก Db ออกมาเป็นชื่อได้เเล้ว เเต่จะเอาเก็บฐานข้อมูลสมาชิก เป็นค่า Id ได้อย่างไหรครับ กูรู 
 
Code (C#) 
SqlCommand comm = new SqlCommand();
            comm.Connection = conn;
            comm.CommandText = "Insert into tbl_customers values (@Cname , @Cidcard , @Cdate , @Caddress , @Ccounty , @Cdistrict , @Clocate_id , @Cphone , @Creg_date , @Creg_price , @Cemp_id)";
            
            comm.Parameters.Add("@Cname", SqlDbType.VarChar).Value = txtcusName.Text;
            comm.Parameters.Add("@Cidcard", SqlDbType.VarChar).Value = txtcusIdcard.Text;
            comm.Parameters.Add("@Cdate", SqlDbType.Date).Value = txtcusDate.Text;
            comm.Parameters.Add("@Caddress", SqlDbType.VarChar).Value = txtcusAdress.Text;
            comm.Parameters.Add("@Ccounty", SqlDbType.VarChar).Value = txtcusDistrict.Text;
            comm.Parameters.Add("@Cdistrict", SqlDbType.VarChar).Value = txtcusCounty.Text;
            comm.Parameters.Add("@Clocate_id", SqlDbType.Int).Value = txtcusLocate.Text;
            comm.Parameters.Add("@Cphone", SqlDbType.VarChar).Value = txtcusPhone.Text;
            comm.Parameters.Add("@Creg_date", SqlDbType.Date).Value = txtcusRegdate.Text;
            comm.Parameters.Add("@Creg_price", SqlDbType.Int).Value = txtcusRegprice.Text;
            comm.Parameters.Add("@Cemp_id", SqlDbType.VarChar).Value = txtId.Text;
 
 
  Tag : .NET, MySQL, C#               
                        | 
           
          
            | 
			
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2013-01-24 15:21:16 | 
                      By :
                          biggest59 | 
                      View :
                          1099 | 
                      Reply :
                          6 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 เป็น DropDownList หรือเปล่าครับ 
 
Code (C#) 
DropDownList1.SelectedItem.Text;
DropDownList1.SelectedItem.Value;
  
 
ลองดูครับ                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2013-01-24 16:25:23 | 
                        By :
                            mr.win | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (C#) 
comm.Parameters.Add("@Clocate_id", SqlDbType.Int).Value = txtcusLocate.Text; <--- error
 
 
ระวังเรื่อง type ด้วยนะ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2013-01-24 16:44:06 | 
                        By :
                            ห้ามตอบเกินวันละ 2 กระทู้ | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ปกติเค้าก็เก็บแค่รหัสแค่นั้นแหละครับ                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2013-01-24 17:08:52 | 
                        By :
                            mr.win | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                   ตอบความคิดเห็นที่ : 4 เขียนโดย : mr.win เมื่อวันที่ 2013-01-24 17:08:52 
รายละเอียดของการตอบ ::
  ช่วยดูให้หน่อยครับว่าทำไมผมถึงเก็บค่า locate_id ใน ตาราง สมาชิกไม่ได้ 
ภาพเเรก เป็น DB สมาชิก 
ภาพ 2 เป็น DB จังหวัดต่างๆ 
ภาพ 3 เป็นการดึงชื่อจังหวัดออกมาโชว์ได้เเล้ว
  
 
 
ต่อไปจะเป็น Code ทั้งหมดนะครับ Code (C#)
using System.Data.SqlClient;
namespace Project_Pm2
{
    public partial class frmRegister : Form
    {
        public frmRegister(string ID)
        {
            InitializeComponent();
            txtId.Text = ID;
        }
        private void frmRegister_Load(object sender, EventArgs e)
        {
            connectDB();
            reDataGrid();
            SqlCommand comm = new SqlCommand("Select * From location order by locate_id", conn);
            SqlDataReader dr = comm.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            txtcusLocate.DataSource = dt;
            txtcusLocate.DisplayMember = "locate_name";
         
            
            
        }
        private SqlConnection conn;
        private void connectDB()
        {
            string conStr = "Data Source = BIGGEST-PC; Initial Catalog = db_motorcycle; User = user1; Password = 1234 ";
            conn = new SqlConnection();
            conn.ConnectionString = conStr;
            if (conn.State == ConnectionState.Open)
            { conn.Close(); }
            conn.Open();
        }
        private void reDataGrid()
        {
            //SqlConnection conn = new SqlConnection("Data Source = localhost; Initial Catalog = student_db; user = user1; Password = 1234;");
            SqlDataAdapter dap = new SqlDataAdapter("select * from test1", conn);
            DataSet ds = new DataSet();
            dap.Fill(ds, "cus");
            DataTable dt = ds.Tables["cus"];
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "cus";
            dataGridView1.Columns[0].HeaderText = "รหัสลูกค้า";
            dataGridView1.Columns[1].HeaderText = "ชื่อ-นามสกุล";
            dataGridView1.Columns[2].HeaderText = "รหัสบัตรประชาชน";
            dataGridView1.Columns[3].HeaderText = "วันเกิด";
            
            
            
            
        }
        private void insertBT_Click(object sender, EventArgs e)
        {
            connectDB();
            SqlCommand comm = new SqlCommand();
            comm.Connection = conn;
            comm.CommandText = "Insert into custome values (@Pname,@Pidcard,@Pdate,@Paddr,@Pdist,@Pcounty,@Plocate)";
            comm.Parameters.Add("@Pname", SqlDbType.VarChar).Value = txtCusname.Text;
            comm.Parameters.Add("@Pidcard", SqlDbType.VarChar).Value = txtcusIdcard.Text;
            comm.Parameters.Add("@Pdate", SqlDbType.Date).Value = txtcusDate.Text;
            comm.Parameters.Add("@Paddr", SqlDbType.VarChar).Value = txtcusAdress.Text;
            comm.Parameters.Add("@Pdist", SqlDbType.VarChar).Value = txtcusDistrict.Text;
            comm.Parameters.Add("@Pcounty", SqlDbType.VarChar).Value = txtcusCounty.Text;
            comm.Parameters.Add("@Plocate", SqlDbType.Int).Value = txtcusLocate.Text;
            
            int numRec = comm.ExecuteNonQuery();
            if (numRec > 0)
            {
                MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว");
            }
            else
            {
                MessageBox.Show("ไม่สามารถเพิ่มข้อมูลได้");
            }
            reDataGrid();
        }
        private void txtcusDatePicker_ValueChanged(object sender, EventArgs e)
        {
            txtcusDate.Text = txtcusDatePicker.Value.ToLongDateString();
        }
        private void txtcusRegdatePicker_ValueChanged(object sender, EventArgs e)
        {
            txtcusRegdate.Text = txtcusRegdatePicker.Value.ToLongDateString();
        }
        
        
    }
}
คือผม อยากจะบันทึกข้อมูลตรงชื่อจังหวัด เเต่เก็บค่าเป็น id ของมัน ลง ตารางสมาชิกครับ เเต่ผมทำไม่ได้สักที ช่วยหน่อยครับพี่
                         
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2013-01-25 02:15:49 2013-01-25 02:18:24 2013-01-25 02:19:17              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2013-01-25 02:12:51 | 
                        By :
                            biggest59 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (C#) 
comm.Parameters.Add("@Clocate_id", SqlDbType.Int).Value = Convert.ToInt32(txtcusLocate.Text);
 
 
หรือเขียนสั้นๆ ลงหน่อย 
 
Code (C#) 
comm.Parameters.AddWithValue("@Clocate_id", Convert.ToInt32(txtcusLocate.Text);
 
 
ฝึก linq สิ แล้วการ query จะเป็นเรียกที่มีความสุขมาก                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2013-01-25 07:41:11 | 
                        By :
                            ห้ามตอบเกินวันละ 2 กระทู้ | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |