2017-09-14 78 views
0

這是我的第一篇文章,我是編程新手。現在,我正在嘗試更改DataTable中從sql數據庫中獲取數據的列名稱。使用foreach循環更改列名後,看起來在更改數據表的列名後,程序在將其綁定到gridview後立即崩潰。 the error occurC#Datatable列名更改問題

任何想法是什麼導致錯誤?

C#代碼隱藏:

//custom query generator 
Session["query"] = db.generate_query(parameter1, parameter2, parameter3, parameter4); 
//connecting sql 
SqlConnection conn = new SqlConnection("Data Source=USER-PC;Initial Catalog=ISMS;Integrated Security=True"); 
SqlCommand cmd1 = new SqlCommand(Session["query"].ToString(),conn); 
SqlDataAdapter sdal = new SqlDataAdapter(cmd1); 


//when search is needed 
dt_table = new DataTable(); 
sdal.Fill(dt_table); 
// dataTable.Columns["ExistingColumnName"].ColumnName = "regnum"; 
//renaming column in datatable 

foreach (DataColumn dtclm in dt_table.Columns) { 
    dt_table.Columns[dtclm.ToString()].ColumnName = dt_table.Columns[dtclm.ToString()].ColumnName.ToString().Replace("_"," "); 

} 

staff_attendance_gridview.Visible = true; 
staff_attendance_gridview.DataSource = dt_table; 
staff_attendance_gridview.DataBind(); 
result_message.Text = dt_table.Rows.Count.ToString() + " rows in the table"; 

ASPX文件:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="admin_attendance_.aspx.cs" Inherits="ISMS.admin_attendance" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> 
<!--start container box--> 
<div id="container_box"> 
<div id="text"> 
<asp:Label runat="server" ID="welcome_message" /><br /> 
<br /> 
<!--search bar--> 
<p> 
<br /> 
    Employee ID: <asp:TextBox runat="server" ID="employee_search" Text="" /><br /> 
    <br /> 
    Attendance ID: <asp:TextBox runat="server" ID="attendance_ID_text_box" Text="" /><br /> 
    <br /> 
    fill up both attendance ID and Employee ID and click delete to delete certain record.<br /> 
    <br /> 
<asp:Button ID="search_btn" runat="server" Text="search" OnClick="search_btn_Click" /> 
<asp:Button ID="clock_in_or_out_btn" runat="server" Text="clock in/out" OnClick="clock_in_or_out_btn_Click" /> 
<asp:Button ID="delete_button" runat="server" Text="delete" OnClick="delete_button_Click" /> 
    <br /> 
</p> 
<!--gridview--> 
    <hr /> 
    <br /> 
<div id="gridview"> 
<asp:GridView runat="server" ID="staff_attendance_gridview" OnPageIndexChanging="gridview_PageIndexChanging" AutoGenerateColumns="true" DataKeyNames="Attendance_id" > 
</asp:GridView> 



</div> 
<!--end gridview--> 
<p> 
<!--validator--> 
<asp:RegularExpressionValidator ID="employee_search_validator" runat="server"  
ErrorMessage="Invalid searching citeria. Only exactly 8 numbers is allowed." 
ControlToValidate="employee_search" 
ValidationExpression="^[0-9]{8}$" /><br /> 
<br /> 
</p> 
<br /> 
<!--hyperlink to main menu--> 
<asp:Label runat="server" ID="result_message" /><br /> 
<a href="admin.aspx" >Main menu</a> 
</div>  
</div> 
<!--end container box--> 
</asp:Content> 

Database table structure

+0

您應該避免使用數據表,並學習如何正確的處理[IDisposable接口(https://msdn.microsoft.com/en-us/library/system.idisposable(V = vs.110)。 ASPX)。 – mason

+0

ok注意。這是我第一次聽說IDisposable接口,會學到它 – moja

+0

爲什麼你在會話中存儲你的查詢? – mason

回答

2

你在你的GridView是指一列Attendance_id的屬性。這必須匹配列的更新名稱Attendance id

我已更正以下代碼,以正確處理IDisposable並刪除過於複雜的位。

Session["query"] = db.generate_query(parameter1, parameter2, parameter3, parameter4); 

using(SqlConnection conn = new SqlConnection("Data Source=USER-PC;Initial Catalog=ISMS;Integrated Security=True")) 
{ 
    using(SqlCommand cmd1 = new SqlCommand(Session["query"].ToString(), conn)) 
    { 
     using(SqlDataAdapter sdal = new SqlDataAdapter(cmd1)) 
     { 
      dt_table = new DataTable(); 
      sdal.Fill(dt_table); 
     } 
    } 
} 

foreach (DataColumn column in dt_table.Columns) 
{ 
    column.ColumnName = column.ColumnName.Replace("_", " "); 
} 

staff_attendance_gridview.Visible = true; 
staff_attendance_gridview.DataSource = dt_table; 
staff_attendance_gridview.DataBind(); 
result_message.Text = dt_table.Rows.Count + " rows in the table"; 
+0

@moja如果您在某個地方引用某個您之前重命名的內容時遇到錯誤,請自己幫忙,並對舊值進行文本搜索,看看它是否在其他地方引用。 – mason

+0

沒問題,如果重命名後出現錯誤,將搜索舊值文本。感謝您的建議 – moja