this image shows the generated gridview by Assign(btn_submitClick) button.
aspx代码:用于gridview。
<div class="row" runat="server">
<div class="col-md-12">
<asp:Panel runat="server" ID="ShowContentPanel" HorizontalAlign="Center"></asp:Panel>
<asp:GridView runat="server"
ID="AT_Sheet_Grid" AutoGenerateColumns="false" Width="80%" HorizontalAlign="Center" >
</asp:GridView>
<br />
<div style="text-align:center">
<asp:LinkButton class="btn btn-primary" runat="server" ID="LinkButton1" OnClick="btn_saveClick" Visible="false">
Save <i class="fa fa-angle-double-right"></i>
</asp:LinkButton>
</div>
</div>
aspx.cs文件上的分配代码(btn_submitClick)按钮:
protected void btn_submitClick(object sender, EventArgs e)
{
//Style
AT_Sheet_Grid.HeaderStyle.Height = 30;
AT_Sheet_Grid.HeaderStyle.Font.Size = 12;
AT_Sheet_Grid.RowStyle.Height = 30;
AT_Sheet_Grid.RowStyle.Font.Size = 11;
//Add columns Dynamically.
BoundField roll_no = new BoundField();
roll_no.HeaderText = "Roll No";
roll_no.DataField = "roll_no";
roll_no.HeaderStyle.Width = new Unit(10, UnitType.Percentage);
AT_Sheet_Grid.Columns.Add(roll_no);
BoundField name = new BoundField();
name.HeaderText = "Name";
name.DataField = "name";
name.HeaderStyle.Width = new Unit(40, UnitType.Percentage);
AT_Sheet_Grid.Columns.Add(name);
BoundField obt_marks = new BoundField();
obt_marks.HeaderText = "Obtained Marks";
obt_marks.DataField = "obt_marks";
obt_marks.HeaderStyle.Width = new Unit(20, UnitType.Percentage);
obt_marks.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
AT_Sheet_Grid.Columns.Add(obt_marks);
//BoundField tot_marks = new BoundField();
//tot_marks.HeaderText = "Total Marks";
//tot_marks.DataField = "tot_marks";
//tot_marks.HeaderStyle.Width = new Unit(15, UnitType.Percentage);
//AT_Sheet_Grid.Columns.Add(tot_marks);
BoundField remarks = new BoundField();
remarks.HeaderText = "Remarks";
remarks.DataField = "remarks";
remarks.HeaderStyle.Width = new Unit(30, UnitType.Percentage);
AT_Sheet_Grid.Columns.Add(remarks);
//Create DataTable and fill the Marks.
DataTable dt = new DataTable();
DataRow dr;
//Add column
dt.Columns.Add(new DataColumn("roll_no"));
dt.Columns.Add(new DataColumn("name"));
dt.Columns.Add(new DataColumn("obt_marks"));
//dt.Columns.Add(new DataColumn("tot_marks"));
dt.Columns.Add(new DataColumn("remarks"));
//Fetch the data and populate the rows.
using (MySqlConnection connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
connection.Open();
try
{
string school_id = null;
//Find the School_id.
MySqlCommand find_id1 = connection.CreateCommand();
find_id1.CommandText = "select school_id from school_table where school_name = '" + DropDownList1.SelectedItem.ToString() + "'";
MySqlDataReader read_id1 = find_id1.ExecuteReader();
while (read_id1.Read())
{
school_id = read_id1["school_id"].ToString();
}
find_id1.Dispose();
read_id1.Close();
string class_id = null;
//Find the Class_id.
MySqlCommand find_id2 = connection.CreateCommand();
find_id2.CommandText = "select class_id from class_table where name = '" + DropDownList2.SelectedItem.ToString() + "' and section = '" + DropDownList3.SelectedItem.ToString() + "' " +
"and session = '" + DropDownList5.SelectedItem.ToString() + "' and school_id = '" + school_id + "'";
MySqlDataReader read_id2 = find_id2.ExecuteReader();
while (read_id2.Read())
{
class_id = read_id2["class_id"].ToString();
}
find_id2.Dispose();
read_id2.Close();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM student_2_table where school_id = '" + school_id + "' and session = '" + DropDownList5.SelectedItem.ToString()
+ "' and class_id = '" + class_id + "'";
MySqlDataReader reader = command.ExecuteReader();
command.Dispose();
if (reader.HasRows)
{
int ind = 0;
string id = null;
while (reader.Read())
{
//Generate the new row of the datatable.
dr = dt.NewRow();
//Create the hidden label.
Label lbl = new Label();
lbl.Visible = false;
lbl.Text = "lbltxt_" + reader["student_2_id"].ToString();
lbl.ID = "lbl_" + ind.ToString();
AT_Sheet_Grid.Controls.Add(lbl);
id = reader["student_1_id"].ToString();
dr["roll_no"] = " " + reader["roll_no"].ToString();
dr["obt_marks"] = "";
dr["remarks"] = "";
//Get Student Name.
using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
con.Open();
try
{
// Student Name:
string sql = "select concat(f_name, ' ', m_name, ' ', l_name) as name from student_1_table where student_1_id = @id";
MySqlCommand com = new MySqlCommand(sql, con);
com.Parameters.AddWithValue("@id", id);
MySqlDataReader read = com.ExecuteReader();
com.Dispose();
while (read.Read())
{
dr["name"] = " " + read["name"].ToString();
}
read.Close();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
finally
{
con.Close();
}
}
//Add Row
dt.Rows.Add(dr);
//increment index value.
ind = ind + 1;
}
}
else
{
}
reader.Close();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
if (connection != null)
connection.Close();
}
//add datatable to grid
AT_Sheet_Grid.DataSource = dt;
AT_Sheet_Grid.DataBind();
LinkButton1.Visible = true;
//Now add the textbox controls
// Add TextBoxes to the Grid.
for (int index = 0; index < AT_Sheet_Grid.Rows.Count; index++)
{
TextBox txt_otbmarks = new TextBox();
txt_otbmarks.ID = "txt_otbmarks_" + index.ToString();
AT_Sheet_Grid.Rows[index].Cells[2].Controls.Add(txt_otbmarks);
AT_Sheet_Grid.Rows[index].Cells[2].HorizontalAlign = HorizontalAlign.Center;
TextBox txt_remarks = new TextBox();
txt_remarks.ID = "txt_remarks_";
AT_Sheet_Grid.Rows[index].Cells[3].Controls.Add(txt_remarks);
AT_Sheet_Grid.Rows[index].Cells[3].HorizontalAlign = HorizontalAlign.Center;
}
}
页面加载事件:
protected void Page_Load(object sender, EventArgs e)
{
}
在保存按钮上单击。当我尝试获取文本框的文本时。它向我显示了空引用。 保存按钮代码:
protected void btn_saveClick(object sender, EventArgs e)
{
//save the data of marks.
using (MySqlConnection connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
connection.Open();
try
{
string school_id = null;
//Find the School_id.
MySqlCommand find_id1 = connection.CreateCommand();
find_id1.CommandText = "select school_id from school_table where school_name = '" + DropDownList1.SelectedItem.ToString() + "'";
MySqlDataReader read_id1 = find_id1.ExecuteReader();
while (read_id1.Read())
{
school_id = read_id1["school_id"].ToString();
}
find_id1.Dispose();
read_id1.Close();
string exam_id = null;
//Find the exam_id.
MySqlCommand find_id2 = connection.CreateCommand();
find_id2.CommandText = "select exam_id from exam_table where name = '" + DropDownList6.SelectedItem.ToString()
+ "' and active = '1' and school_id = '" + school_id + "'";
MySqlDataReader read_id2 = find_id2.ExecuteReader();
while (read_id2.Read())
{
exam_id = read_id2["exam_id"].ToString();
}
find_id2.Dispose();
read_id2.Close();
string subject_id = null;
//Find the subject_id.
MySqlCommand find_id3 = connection.CreateCommand();
find_id3.CommandText = "select subject_id from subject_table where name = '" + DropDownList4.SelectedItem.ToString() + "' and active = '1'" +
" and school_id = '" + school_id + "'";
MySqlDataReader read_id3 = find_id3.ExecuteReader();
while (read_id3.Read())
{
subject_id = read_id3["subject_id"].ToString();
}
find_id3.Dispose();
read_id3.Close();
MySqlCommand command = connection.CreateCommand();
//Save the Marks assigned.
for (int index = 0; index < AT_Sheet_Grid.Rows.Count; index++)
{
command.CommandText = "INSERT INTO marks_table(student_2_id, subject_id, exam_id, obt_marks, remarks) " +
"values( @school_id, @class_id, @subject_id, @session )";
//Label lbl = (Label)FindControl("lbl_0");
//string key = lbl.Text;
//key = key.Split('_')[1];
//command.Parameters.AddWithValue("@student_2_id", key);
command.Parameters.AddWithValue("@subject_id", subject_id);
command.Parameters.AddWithValue("@exam_id", exam_id);
//TextBox txt_otbmarks = (TextBox)FindControl("txt_otbmarks_" + index.ToString());
TextBox txt_otbmarks = (TextBox)AT_Sheet_Grid.Rows[index].Cells[2].FindControl("txt_otbmarks_" + index.ToString());
command.Parameters.AddWithValue("@obt_marks", txt_otbmarks.Text);
//TextBox txt_remarks = (TextBox)FindControl("txt_remarks_" + index.ToString());
TextBox txt_remarks = (TextBox)AT_Sheet_Grid.Rows[index].Cells[3].FindControl("txt_remarks_" + index.ToString());
command.Parameters.AddWithValue("@remarks", txt_remarks.Text);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
if (connection != null)
connection.Close();
}
错误:引发异常:App_Web_wt1tydhq.dll中的“ System.NullReferenceException” System.NullReferenceException:对象引用未设置为对象的实例。