Click here to Skip to main content
15,908,445 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Grid_View_RegisteredUsers.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" style="background-image: url('Penguins.jpg')">
    <div>
        <asp:GridView ID="GridView1" runat="server" 
            AutoGenerateColumns="False"  CellPadding="4" 
            EnableModelValidation="True" ForeColor="#333333" HorizontalAlign="Center" 
            Height="164px" onrowcommand="GridView1_RowCommand" 
            onrowupdating="GridView1_RowUpdating" >
            <AlternatingRowStyle BackColor="White" />
        <Columns>
         <asp:TemplateField>
            <ItemTemplate>
                <asp:HiddenField ID="HiddenField1" runat="server" Visible="true" Value='<%#Bind("id")%>'/>
            </ItemTemplate>            
            
            </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
        <ItemTemplate>
        <asp:Label ID="lblName" runat="server" Text='<%#Bind("Name") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="Gender">
        <ItemTemplate>
        <asp:Label ID="lblGender" runat="server" Text='<%#bind("Gender") %>'>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Address">
        <ItemTemplate>
        <asp:Label ID="lblAddress" runat="server" Text='<%#Bind("Address") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="State">
        <ItemTemplate>
        <asp:Label ID="lblState" runat="server" Text='<%#bind("State") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="City">
        <ItemTemplate>
        <asp:Label ID="lblCity" runat="server" Text='<%#bind("City") %>'>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Zipcode">
        <ItemTemplate>
        <asp:Label ID="lblZipcode" runat="server" Text='<%#bind("Zipcode") %>'>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="USERNAME">
        <ItemTemplate>
        <asp:Label ID="lblUSERNAME" runat="server" Text='<%#bind("UserName") %>'>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="btnUpdate" runat="server" Text="Edit" CommandArgument="<%#((GridViewRow)Container).RowIndex %>" CommandName="update"></asp:LinkButton>
            </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

        </asp:GridView>
    </div>
    <div style="height: 50px; width: 595px;">    
         </div>
    <div>
      <table>

    <tr>
    <td>
    Name:  
    </td>
    <td>
    <asp:TextBox ID="TxtName" runat="server" Width="200" onfocus="if(this.value=='Enter your Name please')this.value='';"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="Lbl_Gender" runat="server" Text="Gender"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="Txt_Gender" runat="server" Width="198px"></asp:TextBox>
     </td>
    </tr>
    <tr>
    <td>
    Address:
    </td>
    <td>
   <asp:TextBox ID="txtAddress" runat="server" Width="200" onfocus="if(this.value='Enter Address')this,value=''"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    State:
    </td>
    <td>
    <asp:textbox ID="TxtState" runat="server" Width="200" onfocus="if(this.value='Enter your State Name Please')this.value=''"></asp:textbox>
    </td>
    </tr>
    <tr>
    <td>
    City:
    </td>
    <td>
    <asp:TextBox ID="Txtcity" runat="server" Width="200" onfocus="if(this.value=='Enter your City Name Please')this.value=''"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    Zipcode:
    </td>
    <td>
    <asp:TextBox ID="TxtZipcode" runat="server" Width="200" onfocus="if(this.value=='Enter Your Zip Code Please')this.value=''"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID ="lblUsername" runat="server" Text="USERNAME"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="Txt_Username" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr align="justify">
    <td>
    <asp:Button ID="bttn_Register" runat="server" Text="REGISTER" 
            onclick="bttn_Register_Click" />
    </td>
    <td align="center">
    <asp:Button ID="bttn_Update" runat="server" Text="UPDATE" 
            onclick="bttn_Update_Click"/>
    </td>
    <td>
    <asp:Button ID="Bttn_CLEAR" runat="server" Text="CLEAR" BorderStyle="Double" 
            onclick="Bttn_CLEAR_Click" />
    </td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>


<pre lang="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page 
{
    string str = System.Configuration.ConfigurationManager.ConnectionStrings["_constr"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(str);  
        SqlCommand cmd = new SqlCommand("select * from Registration_Simple",con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();    
    }
    public void getDataset(string sql, DataSet ds)
    {
        SqlConnection con = new SqlConnection(str);
        SqlDataAdapter da = new SqlDataAdapter();
        con.Open();
        da = new SqlDataAdapter(sql, con);
        da.Fill(ds);
        con.Close();
    }
    public void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        String ID;
        SqlConnection con = new SqlConnection(str);
         try
            {
            if (e.CommandName == "update")
            {                
                LinkButton linbtnupdate = (LinkButton)e.CommandSource;
                int index = Convert.ToInt32(linbtnupdate.CommandArgument);
                this.ID = ((HiddenField)GridView1.Rows[index].FindControl("HiddenField1")).Value;
                DataSet ds = new DataSet();              
                con.Open();
                string sqlquery = "select Name, Gender, Address, State, city, Zipcode,Username from  Registration_Simple where id=" + this.ID;
                getDataset(sqlquery, ds);
                string Name = ds.Tables[0].Rows[0]["Name"].ToString();
                string Gender = ds.Tables[0].Rows[0]["Gender"].ToString();
                string Address = ds.Tables[0].Rows[0]["Address"].ToString();
                string State = ds.Tables[0].Rows[0]["State"].ToString();
                string city = ds.Tables[0].Rows[0]["city"].ToString();
                string Zipcode = ds.Tables[0].Rows[0]["Zipcode"].ToString();
                string Username = ds.Tables[0].Rows[0]["Username"].ToString();
                TxtName.Text = Name;
                Txt_Gender.Text = Gender;
                txtAddress.Text = Address;
                TxtState.Text = State;
                Txtcity.Text = city;
                TxtZipcode.Text = Zipcode;
                Txt_Username.Text = Username;
            }

         }
            catch (Exception ex)
            {
                throw ex;
         
            }
            finally
            {
                con.Close();
            }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }
    protected void Bttn_CLEAR_Click(object sender, EventArgs e)
    {
        TxtName.Text = null;
        Txt_Gender.Text = null;
        txtAddress.Text = null;
        TxtState.Text = null;
        Txtcity.Text = null;
        TxtZipcode.Text = null;
        Txt_Username.Text = null;
    }
    protected void bttn_Register_Click(object sender, EventArgs e)
    {
        Response.Redirect("Registration.aspx");
    }
    public void bttn_Update_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(str);
        SqlDataAdapter da = new SqlDataAdapter();
        SqlCommand cmd = new SqlCommand();
        int id = int.Parse(this.ID);
          try
            {
                con.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "sp_Update_Registration_Simple";
                cmd.Parameters.AddWithValue("@id",id);
                cmd.Parameters.AddWithValue("@Name", TxtName.Text);
                cmd.Parameters.AddWithValue("@Gender", Txt_Gender.Text);
                cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
                cmd.Parameters.AddWithValue("@State", TxtState.Text);
                cmd.Parameters.AddWithValue("@city", Txtcity.Text);
                cmd.Parameters.AddWithValue("@Zipcode", TxtZipcode.Text);
                cmd.Parameters.AddWithValue("@USERNAME", Txt_Username.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
          Response.Write("DATA UPDATED");
        }       
     
}
Posted
Updated 4-Oct-13 2:08am
v2

You have not execute your command in the code like cmd.ExecuteNonQuery(); :)
 
Share this answer
 
Comments
Itz_ashish_Singh 4-Oct-13 8:16am    
i just did that it still shows error that i am submitting below:(The problem is with Identity "ID" i am not able to use it correctly..plz have a luk at it.. :)

Server Error in '/GridViewTO_TextBox' Application.
--------------------------------------------------------------------------------

Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 95: SqlDataAdapter da = new SqlDataAdapter();
Line 96: SqlCommand cmd = new SqlCommand();
Line 97: int id = int.Parse(this.ID);
Line 98: try
Line 99: {


Source File: e:\Users\ashish.s\Documents\Visual Studio 2010\WebSites\GridViewTO_TextBox\Grid_View_RegisteredUsers.aspx.cs Line: 97

Stack Trace:
Madhu Nair 6-Oct-13 11:04am    
What ID are you expecting here??? The ID of the control or any value from the table?
Itz_ashish_Singh 7-Oct-13 2:07am    
hi.
i was trying to retrieve the value of id from
public void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) EVENT
to..
public void bttn_Update_Click(object sender, EventArgs e) Event..

i got it with the help of ViewState.. i'll post the correct code now..

Thnx all for ur help.. :)
C#
public partial class _Default : System.Web.UI.Page 
{
    string str = System.Configuration.ConfigurationManager.ConnectionStrings["_constr"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {        
        SqlConnection con = new SqlConnection(str);  
        SqlCommand cmd = new SqlCommand("select * from Registration_Simple",con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();    
    }
    public void getDataset(string sql, DataSet ds)
    {
        SqlConnection con = new SqlConnection(str);
        SqlDataAdapter da = new SqlDataAdapter();
        con.Open();
        da = new SqlDataAdapter(sql, con);
        da.Fill(ds);
        con.Close();
    }
    public void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        SqlConnection con = new SqlConnection(str);
         try
            {
            if (e.CommandName == "update")
            {                
                LinkButton linbtnupdate = (LinkButton)e.CommandSource;
                int index = Convert.ToInt32(linbtnupdate.CommandArgument);

//changes made:-->starts from here  
             
String ID1 = ((HiddenField)GridView1.Rows[index].FindControl("HiddenField1")).Value;
                int a = int.Parse(ID1);
                ViewState["HiddenID"] = a;

//changes made till here !!
                DataSet ds = new DataSet();              
                con.Open();
                string sqlquery = "select Name, Gender, Address, State, city, Zipcode,Username from  Registration_Simple where id=" + ID1;
                getDataset(sqlquery,ds);     
                string Name = ds.Tables[0].Rows[0]["Name"].ToString();
                string Gender = ds.Tables[0].Rows[0]["Gender"].ToString();
                string Address = ds.Tables[0].Rows[0]["Address"].ToString();
                string State = ds.Tables[0].Rows[0]["State"].ToString();
                string city = ds.Tables[0].Rows[0]["city"].ToString();
                string Zipcode = ds.Tables[0].Rows[0]["Zipcode"].ToString();
                string Username = ds.Tables[0].Rows[0]["Username"].ToString();
                TxtName.Text = Name;
                Txt_Gender.Text = Gender;
                txtAddress.Text = Address;
                TxtState.Text = State;
                Txtcity.Text = city;
                TxtZipcode.Text = Zipcode;
                Txt_Username.Text = Username;
            }
         }
            catch (Exception ex)
            {
                throw ex;        
            }
            finally
            {
                con.Close();
            }
    }
    protected void Bttn_CLEAR_Click(object sender, EventArgs e)
    {
        TxtName.Text = null;
        Txt_Gender.Text = null;
        txtAddress.Text = null;
        TxtState.Text = null;
        Txtcity.Text = null;
        TxtZipcode.Text = null;
        Txt_Username.Text = null;
    }
    protected void bttn_Register_Click(object sender, EventArgs e)
    {
        Response.Redirect("Registration.aspx");
    }
    public void bttn_Update_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(str);
        SqlCommand cmd = new SqlCommand("sp_Update_Registration_Simple", con);
        try
        {            
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_Update_Registration_Simple";
            cmd.Parameters.AddWithValue("@id", ViewState["HiddenID"]);
            cmd.Parameters.AddWithValue("@Name", TxtName.Text);
            cmd.Parameters.AddWithValue("@Gender", Txt_Gender.Text);
            cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
            cmd.Parameters.AddWithValue("@State", TxtState.Text);
            cmd.Parameters.AddWithValue("@City", Txtcity.Text);
            cmd.Parameters.AddWithValue("@Zipcode", TxtZipcode.Text);
            cmd.Parameters.AddWithValue("@USERNAME", Txt_Username.Text);
            cmd.ExecuteNonQuery();
            Response.Write("DATA UPDATED");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            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