Greetings experts,
This is really annoying.
I am using GridView CRUD operations to insert, update, delete, select records.
What we would like to do is before inserting records, we would like to have the blank cells for inserting records to display on the form.
I tried to handle this on the GetSpeakerDetails() method but the blank form is not displaying.
Any ideas what I am doing wrong? Gee, frustrating day!
//Markup:
public partial class Home : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetSpeakerDetail();
}
}
private SqlConnection GetConnection()
{
string connStr = ConfigurationManager.ConnectionStrings["XString"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
con.Open();
return con;
}
public void GetSpeakerDetail()
{
SqlConnection con = GetConnection();
SqlCommand cmd = new SqlCommand("_sp_GetSpeakerDetail", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gvSpeaker.DataSource = ds;
gvSpeaker.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); // if record not found then returning a blank table structure
gvSpeaker.DataSource = ds;
gvSpeaker.DataBind();
int columncount = gvSpeaker.Rows[0].Cells.Count;
gvSpeaker.Rows[0].Cells.Clear();
gvSpeaker.Rows[0].Cells.Add(new TableCell());
gvSpeaker.Rows[0].Cells[0].ColumnSpan = columncount;
gvSpeaker.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void gvSpeaker_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(gvSpeaker.DataKeys[e.RowIndex].Value.ToString());
SqlConnection con = GetConnection();
SqlCommand cmd = new SqlCommand("_sp_DeleteSpeaker", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", id);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
GetSpeakerDetail();
}
protected void gvSpeaker_RowEditing(object sender, GridViewEditEventArgs e)
{
gvSpeaker.EditIndex = e.NewEditIndex;
GetSpeakerDetail();
}
protected void gvSpeaker_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvSpeaker.EditIndex = -1;
GetSpeakerDetail();
}
protected void gvSpeaker_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(gvSpeaker.DataKeys[e.RowIndex].Value.ToString());
string speaker_name = ((TextBox)gvSpeaker.Rows[e.RowIndex].FindControl("txtempname")).Text;
string ministry_name = ((TextBox)gvSpeaker.Rows[e.RowIndex].FindControl("txtempcode")).Text;
string client_name = ((TextBox)gvSpeaker.Rows[e.RowIndex].FindControl("txtempcode")).Text;
string email = ((TextBox)gvSpeaker.Rows[e.RowIndex].FindControl("txtempcode")).Text;
string date_added = ((TextBox)gvSpeaker.Rows[e.RowIndex].FindControl("txtempcode")).Text;
string website_url = ((TextBox)gvSpeaker.Rows[e.RowIndex].FindControl("txtempcode")).Text;
SqlConnection con = GetConnection();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("_sp_UpdateSpeaker", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id ", id);
cmd.Parameters.AddWithValue("@Speaker_Name ", speaker_name);
cmd.Parameters.AddWithValue("@Ministry_Name ", ministry_name);
cmd.Parameters.AddWithValue("@Client_Name ", client_name);
cmd.Parameters.AddWithValue("@Email ", email);
cmd.Parameters.AddWithValue("@date_Added ", date_added);
cmd.Parameters.AddWithValue("@Website_URL ", website_url);
cmd.ExecuteNonQuery();
con.Close();
gvSpeaker.EditIndex = -1;// no row in edit mode
GetSpeakerDetail();
}
}
Sorry for the long code:
PS: I don't have the INSERT records method yet. I will add that once this issue is resolved.
<asp:GridView ID="gvSpeaker" runat="server" AutoGenerateColumns="False" DataKeyNames="SpeakerID"
onrowcancelingedit="gvSpeaker_RowCancelingEdit" onrowediting="gvSpeaker_RowEditing"
onrowdeleting="gvSpeaker_RowDeleting" onrowupdating="gvSpeaker_RowUpdating"
GridLines="None" AllowPaging="true" CssClass="gridview gridheader"
AlternatingRowStyle-CssClass="alt" Width="100%" CellPadding="3" PageSize="5" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2">
<Columns>
<asp:TemplateField HeaderText="S.No." ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%#Container.DataItemIndex+1%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Speaker Name">
<ItemTemplate>
<%# Eval("SpeakerName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtspeakername" runat="server" Text='<%#Eval("SpeakerName") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtcSpeakerName1" runat="server" Width="150px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ministry Name">
<ItemTemplate>
<%#Eval("MinistryName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtministryname" runat="server" Text='<%#Eval("MinistryName") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtMinistryName1" runat="server" Width="150px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Client Name">
<ItemTemplate>
<%# Eval("ClientName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtclientname" runat="server" Text='<%#Eval("ClientName") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtcClientName1" Width="100px" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<%#Eval("Email")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtemail" runat="server" Text='<%#Eval("Email") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtcEmail1" Width="150px" runat="server"></asp:TextBox>
</FooterTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="Date Added">
<ItemTemplate>
<%#Eval("dateAdded")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtdateadded" runat="server" Text='<%#Eval("dateAdded") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtcDateAdded1" Width="150px" CssClass="clientDate" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Website URL">
<ItemTemplate>
<%#Eval("WebsiteURL")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtwebsiteurl" runat="server" Text='<%#Eval("WebsiteURL") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtcWebsiteURL1" Width="150px" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" ButtonType ="Image" EditImageUrl="Image/edit.png" UpdateImageUrl="Image/accept.png"
CancelImageUrl="Image/cancel.png" HeaderText="Edit" />
<asp:CommandField ShowDeleteButton="true" ButtonType="Image" DeleteImageUrl="Image/delete.png" HeaderText="Delete" />
</Columns>
</asp:GridView>