3
Reply

insert data gridview and database clicking on button

Elshen Zamanov

Elshen Zamanov

Aug 11 2013 3:18 AM
1.8k
hi , i have a gridview and i want it shows data in gridview and database when i click o button, but i doesn't add data either gridview or database...
please , help me ...here is my code...
<asp:Label ID="lblTeamName"
                    runat="server"
                    Text="Team name :">
                </asp:Label>

                <asp:TextBox ID="tbxTeamName"
                    runat="server">
                </asp:TextBox>
                
                <asp:RegularExpressionValidator ID="reqvalTeamName"
                    runat="server"
                    ControlToValidate="tbxTeamName"
                    Display="Dynamic"
                    ErrorMessage="*"
                    ValidationExpression="^[A-Z][a-z]{2,10}$"
                    ValidationGroup="NameValidation" />

                <asp:RequiredFieldValidator ID="rqrdTeamName"
                    runat="server"
                    ControlToValidate="tbxTeamName"
                    Display="Dynamic"
                    ErrorMessage="Please,enter a team name!" />
            </div>
            <div class="Header_Parametres">
                <asp:Label ID="lblTeamEmail"
                    runat="server"
                    Text="Team email :">
                </asp:Label>

                <asp:TextBox ID="tbxTeamEmail"
                    runat="server">
                </asp:TextBox>

                <asp:RegularExpressionValidator ID="reqvalTeamEmail"
                    runat="server"
                    ControlToValidate="tbxTeamEmail"
                    Display="Dynamic"
                    ErrorMessage="*"
                    ValidationExpression="^[^_,=,+,@,!,$,%,*,(,),-,<,>,\d]\w*@\w{2,}[^\d].\w{1,}[^\d]$"
                    ValidationGroup="EmailValidation" />

                <asp:RequiredFieldValidator ID="rqrdTeamEmail"
                    runat="server"
                    ControlToValidate="tbxTeamEmail"
                    Display="Dynamic"
                    ErrorMessage="Please,enter a team email!" />

            </div>
            <div id="Header_btn">
                <asp:Button ID="BtnAdd"
                    runat="server"
                    Text="Add"
                    OnClick="BtnAdd_Click" />
            </div>
        </div>

        <div id="Footer">
            
            <asp:GridView ID="GridViewTeams" runat="server" CellPadding="4" ForeColor="#333333" 
                GridLines="None" Width="496px" AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField HeaderText="ID">
                        <EditItemTemplate>
                            <asp:TextBox ID="tbx_ID" runat="server" Text='<%# Bind("teamID") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="lbl_ID" runat="server" Text='<%# Bind("teamID") %>'></asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Team_name">
                        <EditItemTemplate>
                            <asp:TextBox ID="tbx_team_name" runat="server" Text='<%# Bind("team_name") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="lbl_team_name" runat="server" Text='<%# Bind("team_name") %>'></asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Team_email">
                        <EditItemTemplate>
                            <asp:TextBox ID="tbx_team_email" runat="server" Text='<%# Bind("team_email") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="lbl_team_email" runat="server" Text='<%# Bind("team_email") %>'></asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <AlternatingRowStyle BackColor="White" />
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <SortedAscendingCellStyle BackColor="#FDF5AC" />
                <SortedAscendingHeaderStyle BackColor="#4D0000" />
                <SortedDescendingCellStyle BackColor="#FCF6C0" />
                <SortedDescendingHeaderStyle BackColor="#820000" />
            </asp:GridView> 

team.asp.cs ....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Futsal.DataAccess;

namespace Futsal.Admin
{
    public partial class Teams : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Bind();
        }
        public void Bind()
        {
            try
            {
                CTeam cTeam = new CTeam();
                GridViewTeams.DataSource = cTeam.GetAllTeams();
                GridViewTeams.DataBind();
            }
            catch (Exception err)
            {
                Response.Write(err.Message);
            }
        }
        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            CTeam cteam = new CTeam();
            cteam.AddTeams(tbxTeamName.Text,tbxTeamEmail.Text);
        }
        
    } 
    
}   

Cteam.cs ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Futsal.BusinessLogic;
using System.Data.Linq;
using System.Data;

namespace Futsal.DataAccess
{
    public class CTeam
    {
        #region Properties
        #endregion

        #region Constructor
        #endregion

        #region Method
        public bool AddTeams(string _name, string _email)
        {
            try
            {
                using (var db = new BusinessLogic.FutsalDataContext())
                {
                    Team team = new Team();
                    team.team_name = _name;
                    team.team_email = _email;

                    db.Teams.InsertOnSubmit(team);
                    db.SubmitChanges();


                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public DataTable GetAllTeams()
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("teamID", typeof(short));
                dt.Columns.Add("team_name", typeof(string));
                dt.Columns.Add("team_email", typeof(string));

                using (var db = new FutsalDataContext())
                {
                    var data = db.Teams;
                    foreach (Team team in data)
                    {
                        DataRow dr = dt.NewRow();
                        dr["teamID"] = team.teamID;
                        dr["team_name"] = team.team_name;
                        dr["team_email"] = team.team_email;

                        dt.Rows.Add(dr);
                    }
                    return dt;
                }
            }
            catch
            {
                return null;
            }
        }
        #endregion
        
    }
}

Answers (3)