2017-04-24 49 views
0

我有四個DropDownList,它們綁定到GridView
每當我填寫DropDownList數據顯示在GridView
在四個DropDownList中,我已將兩個TextBox附加到兩個DropDownList以通過TextBox添加新數據到DropDownList
該數據正在存儲在數據庫中,但未在GridView中顯示。新添加的數據沒有在gridview中顯示

protected void Button4_Click(object sender, EventArgs e) 
{ 
    int flag = 0; 
    if (CheckBox1.Checked) 
    { 

     if (TextBox1.Text == "" && TextBox2.Text == "") 
     { 
      Label2.Text = "Enter Value !!"; 
      TextBox1.Focus(); 
      TextBox2.Focus(); 
      Label2.Visible = true; 
      flag = 1; 
     } 
     else 
     { 

     } 

    } 

    if (flag == 0 && CheckBox1.Checked == true) 
    { 
     String strConnString = ConfigurationManager.ConnectionStrings["CallcenterConnectionString"].ConnectionString; 
     SqlConnection con = new SqlConnection(strConnString); 
     SqlCommand cmd = new SqlCommand("insert into CallCenter..Loy_DispMstr (CallType, SUBFormat,Disposition, SubDisposition) values (@CallType, @Format,@Disposition, @SubDisposition)", con); 
     cmd.Parameters.Add("@CallType", ddlCalltype.Text); 
     cmd.Parameters.Add("@Format", ddlFormat.Text); 
     cmd.Parameters.Add("@Disposition", TextBox1.Text); 
     cmd.Parameters.Add("@SubDisposition", TextBox2.Text); 

     con.Open(); 
     int i = cmd.ExecuteNonQuery(); 
     con.Close(); 


     Label2.Visible = CheckBox1.Checked; 
     Label2.Text = " Your data is been saved in the database"; 
     Label2.ForeColor = System.Drawing.Color.ForestGreen; 

    } 
    else { } 
} 

protected void Button2_Click1(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      BindDispositionDetails(); 
     } 



     SqlCommand cmd = new SqlCommand(); 
     SqlDataAdapter da = new SqlDataAdapter(); 
     DataTable dt = new DataTable(); 

     String strConnString = ConfigurationManager.ConnectionStrings["CallcenterConnectionString"].ConnectionString; 

     using (var con = new SqlConnection(strConnString)) 
     { 
      con.Open(); 
      using (cmd = new SqlCommand("ROMA_UserManagement", con)) 
      { 
       cmd.Parameters.Add("@flag", SqlDbType.VarChar).Value = "0"; 
       cmd.Parameters.Add("@CallType", SqlDbType.VarChar).Value = ddlCalltype.SelectedValue.ToString(); 
       cmd.Parameters.Add("@Format", SqlDbType.VarChar).Value = ddlFormat.SelectedValue.ToString(); 
       cmd.Parameters.Add("@disposition", SqlDbType.VarChar).Value = ddlDisp.SelectedValue.ToString(); 
       cmd.Parameters.Add("@SubDisposition", SqlDbType.VarChar).Value = ddlSubdisp.SelectedValue.ToString(); 
       cmd.CommandType = CommandType.StoredProcedure; 
       cmd.ExecuteNonQuery(); 
       da.SelectCommand = cmd; 
       da.Fill(dt); 

      } 
      con.Close(); 
     } 
     gvDetails.DataSource = dt; 
     gvDetails.DataBind(); 
     gvDetails.Visible = true; 


    } 

回答

1

更新數據庫後需要將數據綁定到GridView
呼叫插入語句Button2_Click1(object sender, EventArgs e)

protected void Button4_Click(object sender, EventArgs e) 
{ 
    ....... 
    ........ 
    Label2.Visible = CheckBox1.Checked; 
    Label2.Text = " Your data is been saved in the database"; 
    Label2.ForeColor = System.Drawing.Color.ForestGreen; 
    //add this line 
    Button2_Click1(sender, e); 
} 
+0

@ user31121992後的功能添加的最後一行一看便知。 –

+0

我沒有得到請詳細說明 – user31121992

+0

您需要在代碼中添加最後一行'Button2_Click1(sender,e);'。 –