Friday, June 27, 2008

Inserting in Gridview

<asp:GridView ID="gv" runat="server"
AutoGenerateColumns="False"
HorizontalAlign="Center"
Width="90%"
ShowFooter="True"
OnRowCommand="gv_RowCommand" CellPadding="4" ForeColor="#333333"
GridLines="None">
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle HorizontalAlign="Center"  />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle  />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"  />
<Columns>
<asp:TemplateField HeaderText="companyname"
ItemStyle-HorizontalAlign="Center"
FooterStyle-HorizontalAlign="Center">
<ItemTemplate>
<%#Eval("companyname")%>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtcompanyname" runat="server"
Columns="3" />
</FooterTemplate>

<FooterStyle HorizontalAlign="Center"></FooterStyle>

<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>

<asp:TemplateField HeaderText="Address"
ItemStyle-HorizontalAlign="Left"
FooterStyle-HorizontalAlign="Left">
<ItemTemplate>
<%#Eval("Companyaddress")%>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtCompanyaddress" runat="server"
Columns="40" />
</FooterTemplate>

<FooterStyle HorizontalAlign="Left"></FooterStyle>

<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:TemplateField>

<asp:TemplateField HeaderText="CompanyCity"
ItemStyle-HorizontalAlign="Center"
FooterStyle-HorizontalAlign="Center">
<ItemTemplate>
<%#Eval("CompanyCity")%>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtCompanyCity" runat="server"
Columns="40" />
</FooterTemplate>

<FooterStyle HorizontalAlign="Center"></FooterStyle>

<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>

<asp:TemplateField FooterStyle-HorizontalAlign="Center">
<FooterTemplate>
<asp:Button ID="btnInsert" runat="server"
Text="Insert"
CommandName="Insert" />
</FooterTemplate>

<FooterStyle HorizontalAlign="Center"></FooterStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>

coding pages

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bindData();
}

}

protected void gv_RowCommand(Object sender,GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert"))
{

String   comp = ((TextBox)(gv.FooterRow.FindControl("txtCompanyName"))).Text;
String  address = ((TextBox)(gv.FooterRow.FindControl("txtCompanyAddress"))).Text;
String city = ((TextBox)(gv.FooterRow.FindControl("txtCompanycity"))).Text;
SqlConnection dbConn =new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand dCmd = new SqlCommand("insert into companydetail(companyname,companyaddress,companycity)values(@comp,@address,@city)", dbConn);
dCmd.Connection.Open();
dCmd.Parameters.Add("@comp", SqlDbType.NVarChar).Value = comp;
dCmd.Parameters.Add("@address", SqlDbType.NVarChar).Value = address;
dCmd.Parameters.Add("@city", SqlDbType.NVarChar).Value = city;
dCmd.ExecuteNonQuery();
bindData();
dbConn.Close();
}

 

}
private void bindData()
{
SqlConnection dbConn =new SqlConnection( ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("SELECT companyname,companyaddress,companycity  from companydetail order by companyname", dbConn);
DataTable dTable= new DataTable();
da.Fill(dTable);
gv.DataSource = dTable;
gv.DataBind();
dbConn.Close();

}
Share:

1 comment:

  1. Hey, I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say GREAT blog!.....I"ll be checking in on a regularly now....Keep up the good work! :)


    I'm Out! :)

    ReplyDelete