Bogdan Zubar
Bogdan Zubar

Reputation: 73

TextBox in Gridview return null or 'System.Web.UI.WebControls.TextBox'

I have a problem with retrieving of data from textbox. TextBox is located in Gridview. I tried to debug and it showed that my variable where I collect a value of textbox returns null or System.Web.UI.WebControls.TextBox.

Could you please help me to figurr out what the problem is?

Here is the code from back-end and gridview

public void Button_Submit_Onclick(object sender, EventArgs e)
{
    for (int i = 0; i < GridView2.Rows.Count; i++)
    {
        //Textbox from Gridview
        TextBox txt_field = (TextBox)GridView2.Rows[i].Cells[12].FindControl("txt_Comment");
        //Connection string
        con.ConnectionString = ConfigurationManager.ConnectionStrings["TestDeductionsConnectionString2"].ToString();
        //id of column
        int recordid = Convert.ToInt32(GridView2.DataKeys[i].Values[0]);
        //checkbox value
        CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("CheckBox1");
        bool private1 = Convert.ToBoolean(cbox.Checked); 
        //open connection and creating sqlcommand
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        con.Open();
        cmd.CommandText = "Update DetailCosts set private='" + private1 + "' Komentar='" + txt_field + "'  where recordid=" + recordid;
        //
        cmd.Parameters.AddWithValue("@private1", SqlDbType.Bit).Value = private1;
        cmd.Parameters.AddWithValue("@recordid", SqlDbType.Int).Value = recordid.ToString();
        cmd.Parameters.AddWithValue("@Komentar", SqlDbType.NChar).Value = txt_field.ToString();               
        cmd.ExecuteNonQuery();

        if (private1==true)
        {
            //DateTime date = DateTime.Now;
            //cmd.CommandText = "Update AprovedCosts set AprovedCosts.AprovalUser = ";

            cmd.CommandText = "Update DetailCosts set privateCost=Costs where recordid=" + recordid;
            cmd.ExecuteNonQuery();
        }
        else
        {

            cmd.CommandText = "Update DetailCosts set privateCost=0 where recordid=" + recordid;
            cmd.ExecuteNonQuery();
        }
        con.Close();
    }
}

Gridview:

<asp:TemplateField HeaderText="Comment">
    <ItemTemplate>
        <asp:TextBox ID="txt_Comment" runat="server" Text='<%# Eval("Komentar") %>'></asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>

Upvotes: 3

Views: 1678

Answers (3)

ArunPratap
ArunPratap

Reputation: 5020

To make it more simple just use it like this

TextBox txt_field = (TextBox)GridView2.Rows[i].Cells[12].FindControl("txt_Comment");
string txt = txt_field.text; //store it's text in string

and change command

in place of txt_field just write txt or txt.ToString();

 cmd.CommandText = "Update DetailCosts set private='" + private1 + "' Komentar='" + txt + "'  where recordid=" + recordid;
    //
    cmd.Parameters.AddWithValue("@private1", SqlDbType.Bit).Value = private1;
    cmd.Parameters.AddWithValue("@recordid", SqlDbType.Int).Value = recordid.ToString();
    cmd.Parameters.AddWithValue("@Komentar", SqlDbType.NChar).Value = txt.ToString();   

Upvotes: 1

user6630782
user6630782

Reputation:

You are searching for a control that could possibly not exist and assigning it to a textbox control.

TextBox txt_field = (TextBox)GridView2.Rows[i].Cells[12].FindControl("txt_Comment");

Why don't you assign the textbox.text value to the GridView2.Rows[i].Cells[12].Text ?

Which will return a value assuming it exists.

Upvotes: 1

sibel1us
sibel1us

Reputation: 111

Try to use txt_field.Text instead of txt_field.ToString() to get the value.

Upvotes: 1

Related Questions