如何重新审视文本框值... [英] how to retriving textbox value ...

查看:99
本文介绍了如何重新审视文本框值...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected   void  Imgbtnsave_Click(对象发​​件人,ImageClickEventArgs e)
{
ImageButton b =(ImageButton)sender;
GridViewRow row =(GridViewRow)b.NamingContainer;
int rowIndex = row.RowIndex;
string ParameterId = grdcareplan.DataKeys [rowIndex] .Value.ToString();

TextBox txttestresult =(TextBox)grdcareplan.Rows [rowIndex] .Cells [ 8 ]。FindControl( txttestresult);
MySqlConnection StrConnection = null ;
MySqlCommand StrCommand = null ;

try
{
StrConnection = new MySqlConnection ();
StrConnection.ConnectionString = StrConn;
StrConnection.Open();

StrCommand = new MySqlCommand();
StrCommand.CommandType = CommandType.StoredProcedure;
StrCommand.CommandText = Select_LastDate_Result;
StrCommand.Connection = StrConnection;

// StrCommand.Parameters.AddWithValue(_ DiseaseId,ddlselectdisease.SelectedValue);
/// / StrCommand.Parameters.AddWithValue(_ ParameterId,ddlItems.SelectedValue);
// StrCommand.Parameters.AddWithValue(_ Branch_Id,Convert.ToInt32(Session [ Branchid]));
// StrCommand.Parameters.AddWithValue(_ Tenant_Id ,Convert.ToInt32(Session [Tenantid]));
StrCommand.Parameters.AddWithValue( _ Patient_Id,Convert.ToInt32(会话[ DiagnosisClickId])) ;
StrCommand.Parameters.AddWithValue( _ ParameterId,ParameterId);
StrCommand.Parameters.AddWithValue( _ Current_Test_Date,DateTime.Now);
StrCommand.Parameters.AddWithValue( _ Test_Result,txttestresult.Text);

StrCommand.ExecuteNonQuery();
BindCarePlan();
ModalPopupExtender1.Show();
Biomade.Visible = true ;
}
catch (例外情况)
{
obj.InfoEntry(DateTime.Now + + BtnSave1_Click + + AccountBalance.aspx + + ex.Message, );
obj.InfoEntry(DateTime.Now + + ex.StackTrace, );
会话[ strerrormsg] = DateTime.Now + + BtnSave1_Click + + AccountBalance.aspx + + ex.Message;
会话[ strerrortrace] = DateTime.Now + + ex.StackTrace;
Response.Redirect( ErrorPage.aspx);
}
finally
{
StrConnection.Close();
StrConnection.Dispose();
}



它给出异常,即对象引用未设置为对象的瞬间。因为它没有取值textbox txttestresult的颜色。请帮助我..

解决方案

对象引用未设置为对象的实例



当您尝试使用属性或调用null对象的方法时,会发生此错误。更多细节:此处 [ ^ ]



Visual Studio DEBUGGER的简单使用可以告诉你对象因为它正在发生。只需查看堆栈跟踪并将调试器放在该行上即可。检查该行的对象,看看是否有任何一个为null,并且您正在尝试使用该对象属性。处理相同。





这里:

 TextBox txttestresult = (TextBox)grdcareplan.Rows [rowIndex] .Cells [ 8 ]。FindControl(  txttestresult); 



确保文本框ID为 txttestresult 并且每行都有在网格的第9列。 (列也是0索引。)。如果FindControl没有返回任何内容,则显式转换为TextBox会抛出Object引用错误。因此,首先,找到控件,看看它是否为null并且在将其转换为文本框之前是否符合预期。


TextBox txttestresult =(TextBox)grdcareplan.Rows [ rowIndex] .Cells [8] .FindControl(txttestresult); 为空。

因此你得到这个错误。



尝试找出为什么这个控件为空。

在添加txttestresult值作为命令参数时检查null,以确保不会出现此错误。

检查你是否为gridview中的文本框提供了相同的id txttestresult


protected void Imgbtnsave_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton b = (ImageButton)sender;
            GridViewRow row = (GridViewRow)b.NamingContainer;
            int rowIndex = row.RowIndex;
            string ParameterId = grdcareplan.DataKeys[rowIndex].Value.ToString();

            TextBox txttestresult = (TextBox)grdcareplan.Rows[rowIndex].Cells[8].FindControl("txttestresult");
            MySqlConnection StrConnection = null;
            MySqlCommand StrCommand = null;

            try
            {
                StrConnection = new MySqlConnection();
                StrConnection.ConnectionString = StrConn;
                StrConnection.Open();

                StrCommand = new MySqlCommand();
                StrCommand.CommandType = CommandType.StoredProcedure;
                StrCommand.CommandText = "Select_LastDate_Result";
                StrCommand.Connection = StrConnection;

                //StrCommand.Parameters.AddWithValue("_DiseaseId", ddlselectdisease.SelectedValue);
                ////StrCommand.Parameters.AddWithValue("_ParameterId", ddlItems.SelectedValue);
                //StrCommand.Parameters.AddWithValue("_Branch_Id", Convert.ToInt32(Session["Branchid"]));
                //StrCommand.Parameters.AddWithValue("_Tenant_Id", Convert.ToInt32(Session["Tenantid"]));
                StrCommand.Parameters.AddWithValue("_Patient_Id", Convert.ToInt32(Session["DiagnosisClickId"]));
                StrCommand.Parameters.AddWithValue("_ParameterId", ParameterId);
                StrCommand.Parameters.AddWithValue("_Current_Test_Date", DateTime.Now);
                StrCommand.Parameters.AddWithValue("_Test_Result",txttestresult.Text);

                StrCommand.ExecuteNonQuery();
                BindCarePlan();
                ModalPopupExtender1.Show();
                Biomade.Visible = true;
            }
            catch (Exception ex)
            {
                obj.InfoEntry(DateTime.Now + "     " + "BtnSave1_Click" + "     " + "AccountBalance.aspx" + "     " + ex.Message, "");
                obj.InfoEntry(DateTime.Now + "     " + ex.StackTrace, "");
                Session["strerrormsg"] = DateTime.Now + "     " + "BtnSave1_Click" + "     " + "AccountBalance.aspx" + "     " + ex.Message;
                Session["strerrortrace"] = DateTime.Now + "     " + ex.StackTrace;
                Response.Redirect("ErrorPage.aspx");
            }
            finally
            {
                StrConnection.Close();
                StrConnection.Dispose();
            }


its giving exception that object reference not set to an instant of an object..because its not taking the value of textbox txttestresult..please help me..

解决方案

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.


Out here:

TextBox txttestresult = (TextBox)grdcareplan.Rows[rowIndex].Cells[8].FindControl("txttestresult");


Make sure that the textbox id is txttestresult and is present in each row at column 9 of grid. (Columns too are 0 indexed.). If FindControl does not return anything, your explicit casting into TextBox would throw the Object reference error. So, first, find the control, see if it is not null and as expected before casting it into textbox.


TextBox txttestresult = (TextBox)grdcareplan.Rows[rowIndex].Cells[8].FindControl("txttestresult"); is null.
Thus you get this error.

Try and figure out why this control is null.
Check for null while adding the txttestresult value as the command parameter to be sure you don''t get this error.


check u have given same id txttestresult for the textbox in gridview


这篇关于如何重新审视文本框值...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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