Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How do I bind textbox by using sqlDataReader by selecting value in Gridview?

* Textbox not in gridview.

My Code is:

C#
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {   
        SqlConnection conn = new SqlConnection(ConnString);
        SqlCommand cmd = new SqlCommand("Select  * from Customer where Sr_No='" +   GridView2.EditIndex + "' ", conn);

        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        reader.Read();
        {
            txtCity.Text = reader["City"].ToString();          
            reader.Close();
        }
        conn.Close();     
    }


My HTML Code is:
XML
City : <asp:textbox id="txtCity" runat="server" />
<asp:gridview id="GridView2" runat="server" autogeneratecolumns="False"
AutoGenerateDeleteButton="True"
onrowdeleting="GridView2_RowDeleting" DataKeyNames="Sr_No"
onrowediting="GridView2_RowEditing"
onselectedindexchanged="GridView2_SelectedIndexChanged" >
    <asp:boundfield headertext="Sr_No" datafield="Sr_No"
    <headerstyle forecolor="CadetBlue" />

    <asp:boundfield headertext="City" datafield="City"
    <headerstyle forecolor="CadetBlue" />

    <asp:commandfield showeditbutton="True" showcancelbutton="false" />
<asp:gridview />
Posted
Updated 6-May-11 22:22pm
v4
Comments
Ankur\m/ 7-May-11 2:31am    
[moved from answer]
Mr pawan wrote:
Please add your html code so that
its become clear that where have you added your textbox.
Dalek Dave 7-May-11 4:23am    
Minor edits for grammar.

1 solution

try this ..
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {   
        SqlConnection conn = new SqlConnection(ConnString);

        int srNo = Convert.ToInt32(GridView1.DataKeys[e.NewEditIndex].Value);
        SqlCommand cmd = new SqlCommand("Select  * from Customer where Sr_No= @SrNo", conn);
        Cmd.parameters.AddwithValue("@SrNo",srNo);
 
        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        reader.Read();
        {
            txtCity.Text = reader["City"].ToString();          
            reader.Close();
        }
        conn.Close();     
    }
 
Share this answer
 
Comments
sat_100m 7-May-11 4:06am    
Thnx...............
Dalek Dave 7-May-11 4:23am    
Nice.
Mahendra.p25 7-May-11 4:48am    
Thanks

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