Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i want to fetch sqlserver table data on button click into datagrid view in asp, C#.net.. i want code in c# .net
Posted
Comments
virang_21 8-May-13 1:55am    
So do it ... Where is the problem ?

XML
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" borderwidth="1px" bordercolor="Black" xmlns:asp="#unknown">
 <rowstyle cssclass="RowStyle" backcolor="#C9C9C9" horizontalalign="Center" font-names="Calibri" forecolor="Black" />
 <headerstyle cssclass="HeaderStyle" backcolor="#4DB4EE" horizontalalign="Center" font-names="Calibri" forecolor="Black" />      
 <columns>
     <asp:boundfield datafield="database column" headertext="abc" sortexpression="abc" />
     <asp:boundfield datafield="database column" headertext="xyz" sortexpression="xyz" /> 
 </columns>
 </asp:gridview>                                                             
<asp:button id="Button1" runat="server" text="Update" onclick="Button1_Click" />   

C#
protected void Button1_Click(object sender, EventArgs e)
{ 
      /*Open database connection,fill gridview,close the connection*/
     SqlConnection conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionstring name"].ConnectionString;
            conn.Open();    
            string xyz=textbox1.text;
            SqlCommand cmd = new SqlCommand("select * from tablename where name=@xyz", conn);
            cmd.Parameters.Add(new SqlParameter("@xyz",xyz));
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();         
} 
 
Share this answer
 
v3
Go through the Project ASP.NET GridView control demo (CSASPNETGridView)[^] ane explore.
 
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