2013-10-10 77 views
1

我在這裏有一個小問題,我有以下的主表店鋪ID的數據庫

M_employee

EMPID Name 

1  abc 
2  xyz 

M_Division

DIVID EMPID DIVISON 

1  2  arts 
2  1  science 

M_Designation

DESGID EMPID Designation 

1   2  Teacher 
2   1  Scientist 

和基於ID在主表中的存在我檢索一個表單中的標籤上的幾個字段....我想要做的是當我將這些表格的值存儲在一個新表中我只想要存儲的id而不是正在顯示在窗體的標籤中的文本值。下面是我試過的代碼。任何人都可以幫助我嗎?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Data; 
using System.Data.OleDb; 
using System.Configuration; 
using System.Web.UI.HtmlControls; 
using System.Xml.Linq; 
namespace Travel1.Forms 
{ 
    public partial class temporaryduty : System.Web.UI.Page 
    { 
     SqlConnection conn = new SqlConnection("Server; Database; Integrated security = true"); 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      Lbltoday.Text = DateTime.Now.ToString(); 
      if (!IsPostBack) 
      { 
       GetName();//adding the group to the dropdownbox 
      } 
     } 

     private void GetName() 
     { 
      SqlCommand cmd = new SqlCommand("Select EMPID,Name FROM M_employee where IsActive=1 ORDER BY Name", conn); 
      DataSet objDs = new DataSet(); 
      SqlDataAdapter sd = new SqlDataAdapter(cmd); 
      conn.Open(); 
      sd.Fill(objDs); 
      conn.Close(); 
      if (objDs.Tables[0].Rows.Count > 0) 
      { 
       ddlname.DataSource = objDs.Tables[0]; 
       ddlname.DataTextField = "Name"; 
       ddlname.DataValueField = "EMPID"; 
       ddlname.DataBind(); 
       ddlname.Items.Insert(0, "--Select--"); 
      } 
     } 

     protected void ddlname_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      GetDivision(ddlname.SelectedItem.Value); 
     } 

     private void GetDivision(string Name) 
     { 
      SqlCommand cmd = new SqlCommand("SELECT M_employee.Name, M_Division.DIVISION, M_Division.DIVID AS Expr1, M_Designation.DesigID AS Expr2, M_Designation.Designation FROM M_employee INNER JOIN M_Division ON M_employee.DIVID = M_Division.DIVID INNER JOIN M_Designation ON M_employee.DesigID = M_Designation.DesigID WHERE [email protected] ", conn); 
      cmd.Parameters.AddWithValue("@EMPID", Name); 
      DataSet objDs = new DataSet(); 
      SqlDataAdapter sd = new SqlDataAdapter(cmd); 
      conn.Open(); 
      sd.Fill(objDs); 
      conn.Close(); 
      if (objDs.Tables[0].Rows.Count > 0) 
      { 
       lbldiv.Text = objDs.Tables[0].Rows[0]["DIVISION"].ToString(); 
       lbldesig.Text = objDs.Tables[0].Rows[0]["Designation"].ToString(); 
      } 
     } 

     protected void btnSubmit_Click2(object sender, EventArgs e) 
     { 
      string RelaseDate = Calendar1.SelectedDate.Date.ToString(); 
      SqlCommand cmd = new SqlCommand("Insert into T_TADA_tempform(EMPID,DIVID,DesigID) values(@EMPID,@DIVID,@DesigID)", conn); 
      cmd.Parameters.AddWithValue("@EMPID", ddlname.SelectedValue); 
      cmd.Parameters.AddWithValue("@DIVID", lbldesig.Text); 
      cmd.Parameters.AddWithValue("@DesigID", lbldiv.Text); 
      if (conn.State == ConnectionState.Closed) 
      { 
       conn.Open(); 
       int cnt = cmd.ExecuteNonQuery(); 
       conn.Close(); 
       if (cnt == 1) 
       { 
        Response.Redirect("form.aspx"); 
       } 
       else 
        Response.Write("Form has not been submitted,Please Try again!"); 
      } 
     } 
    } 
} 
+2

廣告順便說一句,你有很多的'這裏IDisposable'資源應該最好被包裹在'使用'陳述進行適當的確定性處理。 –

+0

你能讓我知道更多回合嗎? – user2605927

+0

我會將它作爲答案發布。 –

回答

0

當您在部門和指定讀取,存儲的ID的地方,就像在一個私人提起這個類的:

public partial class temporaryduty : System.Web.UI.Page 
{ 
    private int divisionId; 
    private int designationId; 

...

if (objDs.Tables[0].Rows.Count > 0) 
{ 
    lbldiv.Text = objDs.Tables[0].Rows[0]["DIVISION"].ToString(); 
    lbldesig.Text = objDs.Tables[0].Rows[0]["Designation"].ToString(); 
    divisionId = objDs.Tables[0].Rows[0]["Expr1"]; 
    designationId = objDs.Tables[0].Rows[0]["Expr2"]; 
} 

然後,您的按鈕點擊使用這些字段來插入ID:

cmd.Parameters.AddWithValue("@DIVID", divisionId); 
cmd.Parameters.AddWithValue("@DesigID", designationId); 
+0

哦好吧...我會試試這個喬希...並讓你知道:) – user2605927

+0

聽起來不錯。如果它解決了您的問題,請隨時將其標記爲答案。 –

+0

divisionId = objDs.Tables [0] .Rows [0] [「Expr1」];它給出了一個錯誤,說不能將對象轉換爲字符串... – user2605927

1

按照要求,這裏是爲IDisposable資源使用using的慣用方式。請注意,我已經做任何其他事情,代碼的邏輯但,因此要注意其他的答案:)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Data; 
using System.Data.OleDb; 
using System.Configuration; 
using System.Web.UI.HtmlControls; 
using System.Xml.Linq; 
namespace Travel1.Forms 
{ 
    public partial class temporaryduty : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      Lbltoday.Text = DateTime.Now.ToString(); 
      if (!IsPostBack) 
      { 
       GetName();//adding the group to the dropdownbox 
      } 
     } 
    private void GetName() 
    { 
     using (SqlConnection conn = new SqlConnection("Server; Database; Integrated security = true")) 
     using (SqlCommand cmd = new SqlCommand("Select EMPID,Name FROM M_employee where IsActive=1 ORDER BY Name", conn)) 
     using (DataSet objDs = new DataSet()) 
     using (SqlDataAdapter sd = new SqlDataAdapter(cmd)) 
     { 
      conn.Open(); 
      sd.Fill(objDs); 
      if (objDs.Tables[0].Rows.Count > 0) 
      { 
       ddlname.DataSource = objDs.Tables[0]; 
       ddlname.DataTextField = "Name"; 
       ddlname.DataValueField = "EMPID"; 
       ddlname.DataBind(); 
       ddlname.Items.Insert(0, "--Select--"); 
      } 
     } 
    } 
    protected void ddlname_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     GetDivision(ddlname.SelectedItem.Value); 
    } 
    private void GetDivision(string Name) 
    { 
     using (SqlConnection conn = new SqlConnection("Server; Database; Integrated security = true")) 
     using (SqlCommand cmd = new SqlCommand("SELECT M_employee.Name, M_Division.DIVISION, M_Division.DIVID AS Expr1, M_Designation.DesigID AS Expr2, M_Designation.Designation FROM M_employee INNER JOIN M_Division ON M_employee.DIVID = M_Division.DIVID INNER JOIN M_Designation ON M_employee.DesigID = M_Designation.DesigID WHERE [email protected] ", conn)) 
     using (DataSet objDs = new DataSet()) 
     using (SqlDataAdapter sd = new SqlDataAdapter(cmd)) 
     { 
      cmd.Parameters.AddWithValue("@EMPID", Name); 
      conn.Open(); 
      sd.Fill(objDs); 
      if (objDs.Tables[0].Rows.Count > 0) 
      { 
       lbldiv.Text = objDs.Tables[0].Rows[0]["DIVISION"].ToString(); 
       lbldesig.Text = objDs.Tables[0].Rows[0]["Designation"].ToString(); 
      } 
     } 
    } 
    protected void btnSubmit_Click2(object sender, EventArgs e) 
    { 
     string RelaseDate = Calendar1.SelectedDate.Date.ToString(); 
     int cnt; 
     using (SqlConnection conn = new SqlConnection("Server; Database; Integrated security = true")) 
     using (SqlCommand cmd = new SqlCommand("Insert into T_TADA_tempform(EMPID,DIVID,DesigID) values(@EMPID,@DIVID,@DesigID)", conn)) 
     { 
      cmd.Parameters.AddWithValue("@EMPID", ddlname.SelectedValue); 
      cmd.Parameters.AddWithValue("@DIVID", lbldesig.Text); 
      cmd.Parameters.AddWithValue("@DesigID", lbldiv.Text); 
      conn.Open(); 
      cnt = cmd.ExecuteNonQuery(); 
     } 
     if (cnt == 1) 
      { 
       Response.Redirect("form.aspx"); 
      } 
      else 
       Response.Write("Form has not been submitted,Please Try again!"); 
     } 
    } 
    } 
+0

好吧,我現在得到它!謝謝你:) – user2605927