3

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>

3 Answers 3

1

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();   
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

but System.Web.UI.Control doesn't consist of Text method, i can't use it
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.