数据表值未填充 [英] data table values not being filled

查看:103
本文介绍了数据表值未填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网络应用程序中,我试图根据复选框和单选按钮选择在邮件中发送gridview数据。代码如下:



 受保护  void  Button1_Click( object  sender,EventArgs e)
{
string nam = Request.QueryString [ name];
string type = Request.QueryString [ 类型];
string rn = Request.QueryString [ 代码];
DataTable dtable = new DataTable();
dtable.Columns.Add( new DataColumn( date typeof (DateTime)));
dtable.Columns.Add( new DataColumn( hf typeof float )));
dtable.Columns.Add( new DataColumn( status typeof string )));
会话[ dt] = dtable;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings [ leave]。ConnectionString ;
conn.Open();
for int i = 0 ; i < final.Rows.Count; i ++)
{
dtable =(DataTable)Session [ dt];
GridViewRow row = final.Rows [i];
CheckBox chk =(CheckBox)row.Cells [ 3 ]。FindControl( CheckBox1);
if (chk.Checked)

{

RadioButtonList rButton =(RadioButtonList)final.Rows [i] .Cells [ 2 ]。FindControl( RadioButtonList1\" );
if (rButton.SelectedValue == 一半
{
// 更新数据库,用数据表填充gridview值
}
else
{
// 更新数据库,使用数据表值填充gridview
}
}
else
{
// 仅此代码正常工作
}
}
Gridview1.visible = true ;
}





在上面的代码我有gridview的名字,日期,radiobutton,checkbox.last两个控件是templatefields.The问题是复选框被检查或不工作(粗体标记)..这是它进入最后''其他''evertime.Any帮助将不胜感激....

解决方案





首先,这不是一个答案,而是一个建议,你能重构你的代码以便可读吗? />
例如:



  //  < span class =code-comment> ----------------------------------------- ------------------------  
CheckBox cBox =(CheckBox)final.Rows [i] .Cells [ 3 ]。FindControl( CheckBox1);
if (cBox!= null
{
RadioButtonList rButton =(RadioButtonList)final.Rows [i] .Cells [ 2 ]。FindControl( RadioButtonList1\" );
if (rButton.SelectedValue == 一半
{
UpdateDataTable( 0 5 已批准,dtable,nam,type,rn,conn);
}
else
{
UpdateDataTable( 1 0 已批准,dtable ,nam,type,rn,conn);
}
}
else
{
UpdateDataTable( 0 0 已拒登,dtable,nam,type,rn,conn);
}
// --------------- -------------------------------------------------- -----

private void UpdateDataTable(< span class =code-keyword> float
hf, string status,DataTable dTable, string nam, string 类型, string rn,SqlConnection conn)
{
DataRow dd = dtable.NewRow();
dd [ date] = Convert.ToDateTime(final.Rows [i]。单元格[ 1 ]。文本);
dd [ hf] = hf;
dd [ status] = 已批准;
dtable.Rows.Add(dd);
GridView1.DataSource = dTable;
GridView1.DataBind();
SqlCommand cmd = new SqlCommand( insert进入leave_rec VALUES(' + nam + ',' + dd [< span class =code-string> date] + ',' + dd [ hf] + ',' + type + ',' + status + ') ,conn);
cmd.ExecuteNonQuery();
calculate_half(type,nam);
会话[ dt] = dTable;
}





其次,我注意到的一些事情,你在rButton检查中解析错误的类型:

 dd [  date] = final.Rows [i] .Cells [ 1 ]。文字; 
dd [ hf] = 1;



您已将它们定义为DateTime和float,但为它们分配字符串。



最后,这就是你遇到问题的地方我想,这样的行,

 CheckBox cBox =(CheckBox)final.Rows [i] .Cells [ 3 ]。FindControl(  CheckBox1\" ); 

RadioButtonList rButton =(RadioButtonList)final.Rows [i] .Cells [ 2 ]。FindControl( RadioButtonList1);



特定行中的单元格可以控制多少个在您的申请中有

如果单元格只有一个控件,为什么不把这个单元格作为控件,为什么要搜索它?

ie

 CheckBox cBox =(CheckBox)final.Rows [i] .Cells [ 3 ]; 





或者如果您知道有一个名为CheckBox1的复选框控件,则查找该控件。

ie

 如果(CheckBox1。已检查





我不确定我是否回答了您的问题,减少不必要的代码将帮助您更快地找到问题。





问候

Jegan


In my web application i am trying to send gridview data in mail based on checkbox and radiobutton selection. Code for the same is as below:

protected void Button1_Click(object sender, EventArgs e)
        {
            string nam = Request.QueryString["name"];
            string type = Request.QueryString["type"];
            string rn = Request.QueryString["code"];
            DataTable dtable = new DataTable();
            dtable.Columns.Add(new DataColumn("date", typeof(DateTime)));
            dtable.Columns.Add(new DataColumn("hf", typeof(float)));
            dtable.Columns.Add(new DataColumn("status", typeof(string)));
            Session["dt"] = dtable;
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
            conn.Open();
            for (int i = 0; i < final.Rows.Count; i++)
            {
                dtable=(DataTable)Session["dt"];
                GridViewRow row = final.Rows[i];
                CheckBox chk=(CheckBox)row.Cells[3].FindControl("CheckBox1");                
                if (chk.Checked)
                {    
                
                    RadioButtonList rButton = (RadioButtonList)final.Rows[i].Cells[2].FindControl("RadioButtonList1");
                    if (rButton.SelectedValue == "Half")
                    {
                        //update database,fill gridview with data table values
                    }
                    else
                    {
                       //update database,fill gridview with data table values
                    }
                }
                else
                {
                    //Only this code is working
                }
            }            
            Gridview1.visible = true;
        }



In above code i have gridview with name,dates,radiobutton,checkbox.last two controls are templatefields.The problem is checkbox is checked or not is not working(the bold marked)..that is it enters the last ''else'' evertime.Any help would be greatly appreciated....

解决方案

Hi,

first of all, it is not an answer but a suggestion, can you refactor your code so that is readable?
for example:

//-----------------------------------------------------------------
CheckBox cBox = (CheckBox)final.Rows[i].Cells[3].FindControl("CheckBox1");
if (cBox != null)
{
    RadioButtonList rButton = (RadioButtonList)final.Rows[i].Cells[2].FindControl("RadioButtonList1");
    if (rButton.SelectedValue == "Half")
    {
        UpdateDataTable(0.5, "Approved", dtable,  nam,  type,  rn,  conn);
    }
    else
    {
        UpdateDataTable(1.0, "Approved", dtable, nam, type, rn, conn);
    }
}
else
{
    UpdateDataTable(0.0, "Disapproved", dtable, nam, type, rn, conn);
}
//----------------------------------------------------------------------

private void UpdateDataTable(float hf, string status, DataTable dTable, string nam, string type, string rn, SqlConnection conn)
{
    DataRow dd = dtable.NewRow();
    dd["date"] = Convert.ToDateTime(final.Rows[i].Cells[1].Text);
    dd["hf"] = hf;
    dd["status"] = "Approved";
    dtable.Rows.Add(dd);
    GridView1.DataSource = dTable;
    GridView1.DataBind();
    SqlCommand cmd = new SqlCommand("insert into leave_rec VALUES('" + nam + "','" + dd["date"] + "','" + dd["hf"] + "','" + type + "','" + status + "')", conn);
    cmd.ExecuteNonQuery();
    calculate_half(type, nam);
    Session["dt"] = dTable;
}



Second, There are few things that I noticed, where you are parsing the wrong type on rButton check:

dd["date"] = final.Rows[i].Cells[1].Text;
dd["hf"] = "1";


you have defined them as DateTime and float, but assigning them string.

Finally, this is where you are having the problem I guess, lines like this,

CheckBox cBox = (CheckBox)final.Rows[i].Cells[3].FindControl("CheckBox1");

RadioButtonList rButton =(RadioButtonList)final.Rows[i].Cells[2].FindControl("RadioButtonList1");


How many controls a cell in a specific row can have in your application
If the cell can have only one control why don''t you take that cell as the control, why are you searching for it?
i.e.

CheckBox cBox = (CheckBox)final.Rows[i].Cells[3];



or if you know there is a check box control named "CheckBox1" then look for that control.
i.e.

if (CheckBox1.checked)



I am not sure I answered your question, reducing unnecessary code will help you find the problem faster.


Regards
Jegan


这篇关于数据表值未填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆