Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
string id = (g1.FindControl("txName") as TextBox).Text;

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Posted
Comments
[no name] 13-Jan-15 6:34am    
In which event you want to get this id ,
grid event or other controls event

You can use RowDataBound event,

C#
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string id = ((TextBox)e.Row.Cells[0].FindControl("txName")).Text
    }
}
 
Share this answer
 
Comments
Member 11262250 13-Jan-15 14:55pm    
this is my code what i can do to save the rows entire gridview to databse
_________________________________________________________________


<div>
<asp:GridView ID="grvStudentDetails" runat="server" ShowFooter="True" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="grvStudentDetails_RowDeleting"
Width="97%" Style="text-align: left" OnRowDataBound="gv_RowDataBound">
<columns>
<asp:BoundField DataField="RowNumber" HeaderText="SNo" />
<asp:TemplateField HeaderText="Student Name">
<itemtemplate>
<asp:TextBox ID="txtName" runat="server" MaxLength="50">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"
ErrorMessage="*" SetFocusOnError="True">


<asp:TemplateField HeaderText="Student Age">
<itemtemplate>
<asp:TextBox ID="txtAge" runat="server" MaxLength="3" Width="66px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtAge"
ErrorMessage="*" SetFocusOnError="True">


<asp:TemplateField HeaderText="Student Address">
<itemtemplate>
<asp:TextBox ID="txtAddress" runat="server" Height="55px" TextMode="MultiLine">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtAddress"
ErrorMessage="*" SetFocusOnError="True">


<asp:TemplateField HeaderText="Gender">
<itemtemplate>
<asp:RadioButtonList ID="RBLGender" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="M">Male
<asp:ListItem Value="F">Female

<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="RBLGender"
ErrorMessage="*"> 


<asp:TemplateField HeaderText="Qualification">
<itemtemplate>
<asp:DropDownList ID="drpQualification" runat="server">
<asp:ListItem>Select
<asp:ListItem Value="G">Graduate
<asp:ListItem Value="P">Post Graduate

<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="drpQualification"
ErrorMessage="*" InitialValue="Select">

<footerstyle horizontalalign="Right">
<footertemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />

if you catch it outside of row databound event then use it

C#
foreach (GridViewRow grdRow in g1.Rows)  
   {
        TextBox txName= (TextBox)grdRow.FindControl("txName");
   }



if you want catch it in rowdatabound event then use it

C#
protected void g1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TextBox txName=e.Row.FindCOntrol("txName") as TextBox;
    }
}
 
Share this answer
 
Comments
Member 11262250 13-Jan-15 14:55pm    
this is my code what i can do to save the rows entire gridview to databse
_________________________________________________________________


<div>
<asp:GridView ID="grvStudentDetails" runat="server" ShowFooter="True" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="grvStudentDetails_RowDeleting"
Width="97%" Style="text-align: left" OnRowDataBound="gv_RowDataBound">
<columns>
<asp:BoundField DataField="RowNumber" HeaderText="SNo" />
<asp:TemplateField HeaderText="Student Name">
<itemtemplate>
<asp:TextBox ID="txtName" runat="server" MaxLength="50">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"
ErrorMessage="*" SetFocusOnError="True">


<asp:TemplateField HeaderText="Student Age">
<itemtemplate>
<asp:TextBox ID="txtAge" runat="server" MaxLength="3" Width="66px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtAge"
ErrorMessage="*" SetFocusOnError="True">


<asp:TemplateField HeaderText="Student Address">
<itemtemplate>
<asp:TextBox ID="txtAddress" runat="server" Height="55px" TextMode="MultiLine">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtAddress"
ErrorMessage="*" SetFocusOnError="True">


<asp:TemplateField HeaderText="Gender">
<itemtemplate>
<asp:RadioButtonList ID="RBLGender" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="M">Male
<asp:ListItem Value="F">Female

<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="RBLGender"
ErrorMessage="*"> 


<asp:TemplateField HeaderText="Qualification">
<itemtemplate>
<asp:DropDownList ID="drpQualification" runat="server">
<asp:ListItem>Select
<asp:ListItem Value="G">Graduate
<asp:ListItem Value="P">Post Graduate

<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="drpQualification"
ErrorMessage="*" InitialValue="Select">

<footerstyle horizontalalign="Right">
<footertemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />

this is my code what i can do to save the rows entire gridview to databse
_________________________________________________________________



<asp:gridview id="grvStudentDetails" runat="server" showfooter="True" autogeneratecolumns="False" xmlns:asp="#unknown">
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="grvStudentDetails_RowDeleting"
Width="97%" Style="text-align: left" OnRowDataBound="gv_RowDataBound">
<columns> <asp:boundfield datafield="RowNumber" headertext="SNo">
<asp:templatefield headertext="Student Name">
<itemtemplate>
<asp:textbox id="txtName" runat="server" maxlength="50">
<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" controltovalidate="txtName">
ErrorMessage="*" SetFocusOnError="True">


<asp:templatefield headertext="Student Age">
<itemtemplate>
<asp:textbox id="txtAge" runat="server" maxlength="3" width="66px">
<asp:requiredfieldvalidator id="RequiredFieldValidator2" runat="server" controltovalidate="txtAge">
ErrorMessage="*" SetFocusOnError="True">


<asp:templatefield headertext="Student Address">
<itemtemplate>
<asp:textbox id="txtAddress" runat="server" height="55px" textmode="MultiLine">
<asp:requiredfieldvalidator id="RequiredFieldValidator3" runat="server" controltovalidate="txtAddress">
ErrorMessage="*" SetFocusOnError="True">


<asp:templatefield headertext="Gender">
<itemtemplate>
<asp:radiobuttonlist id="RBLGender" runat="server" repeatdirection="Horizontal">
<asp:listitem value="M">Male
<asp:listitem value="F">Female

<asp:requiredfieldvalidator id="RequiredFieldValidator4" runat="server" controltovalidate="RBLGender">
ErrorMessage="*"> 


<asp:templatefield headertext="Qualification">
<itemtemplate>
<asp:dropdownlist id="drpQualification" runat="server">
<asp:listitem>Select
<asp:listitem value="G">Graduate
<asp:listitem value="P">Post Graduate

<asp:requiredfieldvalidator id="RequiredFieldValidator5" runat="server" controltovalidate="drpQualification">
ErrorMessage="*" InitialValue="Select">

<footerstyle horizontalalign="Right">
<footertemplate>
<asp:button id="ButtonAdd" runat="server" text="Add New Row" onclick="ButtonAdd_Click">


<asp:commandfield showdeletebutton="True">
<footerstyle backcolor="#507CD1" font-bold="True" forecolor="White">
<rowstyle backcolor="#EFF3FB">
<editrowstyle backcolor="#2461BF">
<selectedrowstyle backcolor="#D1DDF1" font-bold="True" forecolor="#333333">
<pagerstyle backcolor="#2461BF" forecolor="White" horizontalalign="Center">
<headerstyle backcolor="#507CD1" font-bold="True" forecolor="White">
<alternatingrowstyle backcolor="White">




<asp:button id="btnSave" runat="server" text="Save Data" onclick="btnSave_Click" xmlns:asp="#unknown">



and my code behind is :

Quote:
protected void btnSave_Click(object sender, EventArgs e)
{


con.Open();
tran = con.BeginTransaction();
cmd.Transaction = tran;
string slno = null;

foreach (GridViewRow g1 in grvStudentDetails.Rows)
{

//string id = ((TextBox)g1.Cells[0].FindControl("txName")).Text;
string id = ((TextBox)g1.FindControl("TextBox1")).Text;
string name = (g1.FindControl("txAge") as TextBox).Text;
string price = (g1.FindControl("txtAddress") as TextBox).Text;
string description = (g1.FindControl("RBLGender") as Label).Text;

string query = "insert into SU_Seeker_Qualifications (Type,Join_Date,Graduated_Date,Univerisity) values(" + id + ",'" + name + "'," + price + ",'" + description + "')";
slno = id;
cmd.CommandText = query;
cmd.ExecuteNonQuery();
}
tran.Commit();
lblMessage.Text = "Records inserted successfully";
//}
con.Close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900