In my .aspx page I have the following code in my ListView control;
<%# Page Title="User Registration" MasterPageFile="MasterPage.master" Language="C#" AutoEventWireup="true" CodeFile="registration.aspx.cs" Inherits="registration" %>
<asp:Content ContentPlaceHolderID="head" Runat="Server">
<!-- Your Code goes here -->
</asp:Content>
<asp:Content ContentPlaceHolderID="menu" Runat="Server">
<!-- Your Code goes here -->
</asp:Content>
<asp:Content ContentPlaceHolderID="main_content" Runat="Server">
<div class="row">
<div class="col-12">
<asp:Label Visible="false" ID="username" runat="server" Text=""></asp:Label>
<asp:sqldatasource ID="semester1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT modules.cats_points, modules.module_id, modules.compulsory, modules.module_name, modules.year FROM Courses INNER JOIN courses_vs_modules ON Courses.course_id = courses_vs_modules.course_id INNER JOIN modules ON courses_vs_modules.module_id = modules.module_id INNER JOIN student_records ON Courses.course_id = student_records.course_id AND modules.year = student_records.year AND courses_vs_modules.course_id = student_records.course_id WHERE (student_records.user_id = #user_id) AND (modules.semester = 1)"
InsertCommand="INSERT INTO [students_vs_modules]([user_id], [module_id]) VALUES (#user_id, #module_id)">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="user_id" Name="user_id" Type="Int32"></asp:QueryStringParameter>
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="module_id" Type="Int32" />
</InsertParameters>
</asp:sqldatasource>
<asp:sqldatasource ID="semester2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT modules.cats_points, modules.module_id, modules.compulsory, modules.module_name, modules.year FROM Courses INNER JOIN courses_vs_modules ON Courses.course_id = courses_vs_modules.course_id INNER JOIN modules ON courses_vs_modules.module_id = modules.module_id INNER JOIN student_records ON Courses.course_id = student_records.course_id AND modules.year = student_records.year AND courses_vs_modules.course_id = student_records.course_id WHERE (student_records.user_id = #user_id) AND (modules.semester = 2)"
InsertCommand="INSERT INTO [students_vs_modules]([user_id], [module_id]) VALUES (#user_id, #module_id)">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="user_id" Name="user_id" Type="Int32"></asp:QueryStringParameter>
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="module_id" Type="Int32" />
</InsertParameters>
</asp:sqldatasource>
<asp:SqlDataSource ID="emergency_contact" runat="server" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' SelectCommand="SELECT student_records.user_id, student_records.f_name, student_records.l_name, emergency_contact.em_contact_id, emergency_contact.relationship_id, emergency_contact.contact_name, emergency_contact.phone_number, emergency_contact.address_1, emergency_contact.address_2, emergency_contact.town_city, emergency_contact.county, emergency_contact.postcode, emergency_contact.country, relationship.relationship FROM student_records INNER JOIN emergency_contact ON student_records.user_id = emergency_contact.user_id INNER JOIN relationship ON emergency_contact.relationship_id = relationship.relationship_id WHERE (student_records.user_id = #user_id)"
UpdateCommand="UPDATE [emergency_contact] SET [relationship_id]=#relationship_id, [contact_name]=#contact_name, [phone_number]=#phone_number, [address_1]=#address_1, [address_2]=#address_2, [town_city]=#town_city, [county]=#county, [postcode]=#postcode, [country]=#country WHERE [user_id]=#user_id ">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="user_id" Name="user_id" Type="Int32"></asp:QueryStringParameter>
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="relationship_id" Type="Int32" />
<asp:Parameter Name="contact_name" Type="String" />
<asp:Parameter Name="phone_number" Type="String" />
<asp:Parameter Name="address_1" Type="String" />
<asp:Parameter Name="address_2" Type="String" />
<asp:Parameter Name="town_city" Type="String" />
<asp:Parameter Name="county" Type="String" />
<asp:Parameter Name="postcode" Type="String" />
<asp:Parameter Name="country" Type="String" />
<asp:Parameter Name="user_id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="emergency_contact_list" runat="server" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' SelectCommand="SELECT relationship_id, relationship FROM relationship"></asp:SqlDataSource>
<asp:MultiView ID="MultiView" runat="server" ActiveViewIndex="0">
<asp:View ID="em_contact_view" runat="server">
<h1>Emergency Contact</h1>
<asp:ListView ID="em_contact_list" runat="server" DataSourceID="emergency_contact" >
<EditItemTemplate>
<span style="">user_id:
<asp:TextBox Text='<%# Bind("user_id") %>' runat="server" ID="user_idTextBox" /><br />
f_name:
<asp:TextBox Text='<%# Bind("f_name") %>' runat="server" ID="f_nameTextBox" /><br />
l_name:
<asp:TextBox Text='<%# Bind("l_name") %>' runat="server" ID="l_nameTextBox" /><br />
em_contact_id:
<asp:Label Text='<%# Eval("em_contact_id") %>' runat="server" ID="em_contact_idLabel1" /><br />
relationship_id:
<asp:TextBox Text='<%# Bind("relationship_id") %>' runat="server" ID="relationship_idTextBox" /><br />
contact_name:
<asp:TextBox Text='<%# Bind("contact_name") %>' runat="server" ID="contact_nameTextBox" /><br />
phone_number:
<asp:TextBox Text='<%# Bind("phone_number") %>' runat="server" ID="phone_numberTextBox" /><br />
address_1:
<asp:TextBox Text='<%# Bind("address_1") %>' runat="server" ID="address_1TextBox" /><br />
address_2:
<asp:TextBox Text='<%# Bind("address_2") %>' runat="server" ID="address_2TextBox" /><br />
town_city:
<asp:TextBox Text='<%# Bind("town_city") %>' runat="server" ID="town_cityTextBox" /><br />
county:
<asp:TextBox Text='<%# Bind("county") %>' runat="server" ID="countyTextBox" /><br />
postcode:
<asp:TextBox Text='<%# Bind("postcode") %>' runat="server" ID="postcodeTextBox" /><br />
country:
<asp:TextBox Text='<%# Bind("country") %>' runat="server" ID="countryTextBox" /><br />
relationship:
<asp:TextBox Text='<%# Bind("relationship") %>' runat="server" ID="relationshipTextBox" /><br />
<asp:Button runat="server" CommandName="Update" Text="Update" ID="UpdateButton" />
<asp:Button runat="server" CommandName="Cancel" Text="Cancel" ID="CancelButton" /><br />
<br />
</span>
</EditItemTemplate>
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
<InsertItemTemplate>
<span style="">user_id:
<asp:TextBox Text='<%# Bind("user_id") %>' runat="server" ID="user_idTextBox" /><br />
f_name:
<asp:TextBox Text='<%# Bind("f_name") %>' runat="server" ID="f_nameTextBox" /><br />
l_name:
<asp:TextBox Text='<%# Bind("l_name") %>' runat="server" ID="l_nameTextBox" /><br />
relationship_id:
<asp:TextBox Text='<%# Bind("relationship_id") %>' runat="server" ID="relationship_idTextBox" /><br />
contact_name:
<asp:TextBox Text='<%# Bind("contact_name") %>' runat="server" ID="contact_nameTextBox" /><br />
phone_number:
<asp:TextBox Text='<%# Bind("phone_number") %>' runat="server" ID="phone_numberTextBox" /><br />
address_1:
<asp:TextBox Text='<%# Bind("address_1") %>' runat="server" ID="address_1TextBox" /><br />
address_2:
<asp:TextBox Text='<%# Bind("address_2") %>' runat="server" ID="address_2TextBox" /><br />
town_city:
<asp:TextBox Text='<%# Bind("town_city") %>' runat="server" ID="town_cityTextBox" /><br />
county:
<asp:TextBox Text='<%# Bind("county") %>' runat="server" ID="countyTextBox" /><br />
postcode:
<asp:TextBox Text='<%# Bind("postcode") %>' runat="server" ID="postcodeTextBox" /><br />
country:
<asp:TextBox Text='<%# Bind("country") %>' runat="server" ID="countryTextBox" /><br />
relationship:
<asp:TextBox Text='<%# Bind("relationship") %>' runat="server" ID="relationshipTextBox" /><br />
<asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" /><asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="CancelButton" /><br />
<br />
</span>
</InsertItemTemplate>
<ItemTemplate>
<span style="">user_id:
<asp:Label Text='<%# Eval("user_id") %>' runat="server" ID="user_idLabel" /><br />
f_name:
<asp:Label Text='<%# Eval("f_name") %>' runat="server" ID="f_nameLabel" /><br />
l_name:
<asp:Label Text='<%# Eval("l_name") %>' runat="server" ID="l_nameLabel" /><br />
em_contact_id:
<asp:Label Text='<%# Eval("em_contact_id") %>' runat="server" ID="em_contact_idLabel" /><br />
relationship_id:
<asp:Label Text='<%# Eval("relationship_id") %>' runat="server" ID="relationship_idLabel" /><br />
contact_name:
<asp:Label Text='<%# Eval("contact_name") %>' runat="server" ID="contact_nameLabel" /><br />
phone_number:
<asp:Label Text='<%# Eval("phone_number") %>' runat="server" ID="phone_numberLabel" /><br />
address_1:
<asp:Label Text='<%# Eval("address_1") %>' runat="server" ID="address_1Label" /><br />
address_2:
<asp:Label Text='<%# Eval("address_2") %>' runat="server" ID="address_2Label" /><br />
town_city:
<asp:Label Text='<%# Eval("town_city") %>' runat="server" ID="town_cityLabel" /><br />
county:
<asp:Label Text='<%# Eval("county") %>' runat="server" ID="countyLabel" /><br />
postcode:
<asp:Label Text='<%# Eval("postcode") %>' runat="server" ID="postcodeLabel" /><br />
country:
<asp:Label Text='<%# Eval("country") %>' runat="server" ID="countryLabel" /><br />
relationship:
<asp:Label Text='<%# Eval("relationship") %>' runat="server" ID="relationshipLabel" /><br />
<asp:Button runat="server" CommandName="Edit" Text="Edit" ID="EditButton" />
<br />
<br />
</span>
</ItemTemplate>
<LayoutTemplate>
<div runat="server" id="itemPlaceholderContainer" style=""><span runat="server" id="itemPlaceholder" /></div>
<div style="">
</div>
</LayoutTemplate>
<SelectedItemTemplate>
<span style="">user_id:
<asp:Label Text='<%# Eval("user_id") %>' runat="server" ID="user_idLabel" /><br />
f_name:
<asp:Label Text='<%# Eval("f_name") %>' runat="server" ID="f_nameLabel" /><br />
l_name:
<asp:Label Text='<%# Eval("l_name") %>' runat="server" ID="l_nameLabel" /><br />
em_contact_id:
<asp:Label Text='<%# Eval("em_contact_id") %>' runat="server" ID="em_contact_idLabel" /><br />
relationship_id:
<asp:Label Text='<%# Eval("relationship_id") %>' runat="server" ID="relationship_idLabel" /><br />
contact_name:
<asp:Label Text='<%# Eval("contact_name") %>' runat="server" ID="contact_nameLabel" /><br />
phone_number:
<asp:Label Text='<%# Eval("phone_number") %>' runat="server" ID="phone_numberLabel" /><br />
address_1:
<asp:Label Text='<%# Eval("address_1") %>' runat="server" ID="address_1Label" /><br />
address_2:
<asp:Label Text='<%# Eval("address_2") %>' runat="server" ID="address_2Label" /><br />
town_city:
<asp:Label Text='<%# Eval("town_city") %>' runat="server" ID="town_cityLabel" /><br />
county:
<asp:Label Text='<%# Eval("county") %>' runat="server" ID="countyLabel" /><br />
postcode:
<asp:Label Text='<%# Eval("postcode") %>' runat="server" ID="postcodeLabel" /><br />
country:
<asp:Label Text='<%# Eval("country") %>' runat="server" ID="countryLabel" /><br />
relationship:
<asp:Label Text='<%# Eval("relationship") %>' runat="server" ID="relationshipLabel" /><br />
<asp:Button runat="server" CommandName="Edit" Text="Edit" ID="EditButton" />
<br />
<br />
</span>
</SelectedItemTemplate>
</asp:ListView>
<asp:Button CommandName="NextView" ID="em_contact_next" runat="server" Text="Next" />
</asp:View>
<asp:View ID="semester_1_view" runat="server">
<h1>Semester 1</h1>
<asp:DataList ID="semester_1_datalist" runat="server" DataSourceID="semester1">
<ItemTemplate>
module_name:
<asp:Label ID="module_nameLabel" runat="server" Text='<%# Eval("module_name") %>' />
<br />
year:
<asp:Label ID="yearLabel" runat="server" Text='<%# Eval("year") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<script type="text/javascript">
var limit = 3;
$(function () {
$('[id*="module_semester_1"]').on('change', function (evt) {
if ($('[id*="module_semester_1"]:checked').length > limit) {
this.checked = true;
alert('cannot select more than ' + limit);
}
});
});
</script>
<asp:CheckBoxList ID="module_semester_1" runat="server" DataSourceID="semester1" DataTextField="module_name" DataValueField="module_id" ></asp:CheckBoxList>
<asp:Button CommandName="NextView" ID="btnnext2" runat="server" Text="Next" OnClick="btnnext2_Click" />
</asp:View>
<asp:View ID="semester_2_view" runat="server">
<h1>Semester 2</h1>
<div runat="server">
<asp:CheckBoxList ID="moduleselect" runat="server" DataSourceID="semester2" DataTextField="module_name" DataValueField="module_id" ></asp:CheckBoxList>
<asp:DataList ID="semester_2_list" runat="server" DataSourceID="semester2">
<ItemTemplate>
module_name:
<asp:Label ID="module_nameLabel" runat="server" Text='<%# Eval("cats_points") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<br />
<asp:Button ID="uploadbutton" runat="server" Text="Choose Modules" OnClick="uploadbutton_Click" CssClass="submitbtn" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" CssClass="submitbtn" />
</div></asp:View></asp:MultiView>
</div>
</div>
</asp:Content>
I'm trying to update fields in the database table using C#, I have the following C# code, however I am receiving the error stating that TextBox control contact_nameTextBox does not exist in current context and I'm not sure why, any help would be greatly appreciated.
protected void em_contact_list_SelectedIndexChanged(object sender, EventArgs e)
{
string ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);
myConnection.Open();
string Name_data = contact_nameTextBox.Text;
Probably because you´re trying to access it from a protected void, you need to create an instace of it
protected void em_contact_list_SelectedIndexChanged(object sender, EventArgs e)
{
string ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);
myConnection.Open();
contact_nameTextBox.Text = new textBox();
string Name_data = contact_nameTextBox.Text;
protected void em_contact_list_SelectedIndexChanged(object sender, EventArgs e)
{
string ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);
myConnection.Open();
ListView parent = (ListView)sender;
if(parent != null)
var contact_nameTextBox = (TextBox)(parent.FindName("contact_nameTextBox"));
string contact_Name = contact_nameTextBox.Text;
//Your code
}
I have not tested this code, but hope with some small changes it should work.
Related
I have a grid view and I wanna update data in the row. I have written a C# code and I've noticed that the RowUpdating event is not firing.
I am using Template Field in my grid. The RowCommand event is working properly, however, the RowUpdating event is not firing.
What should I do? I need your help, please.
The event is like the below:
protected void grdPeople_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
SqlConnection con = new SqlConnection(strCon);
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand com = new SqlCommand("InsertUpdatePerson", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("#pr_id", Convert.ToInt32(grdPeople.DataKeys[e.RowIndex].Value.ToString()));
com.Parameters.AddWithValue("#pr_name", (grdPeople.FooterRow.FindControl("txtNameFooter") as TextBox).Text.Trim());
com.Parameters.AddWithValue("#pr_gender", (grdPeople.FooterRow.FindControl("rdoMaleFooter") as RadioButton).Checked);
com.Parameters.AddWithValue("#pr_nationality", (grdPeople.FooterRow.FindControl("txtNationalityFooter") as TextBox).Text.Trim());
com.Parameters.AddWithValue("#pr_idn", (grdPeople.FooterRow.FindControl("txtIDnoFooter") as TextBox).Text.Trim());
com.Parameters.AddWithValue("#pr_passport", (grdPeople.FooterRow.FindControl("txtPassportFooter") as TextBox).Text.Trim());
com.Parameters.AddWithValue("#pr_resident", (grdPeople.FooterRow.FindControl("rdoYesFooter") as RadioButton).Checked);
com.Parameters.AddWithValue("#pr_phone", (grdPeople.FooterRow.FindControl("txtPhoneFooter") as TextBox).Text.Trim());
com.Parameters.AddWithValue("#pr_email", (grdPeople.FooterRow.FindControl("txtEmailFooter") as TextBox).Text.Trim());
com.Parameters.Add(new SqlParameter("#ERR_MESSAGE", SqlDbType.NVarChar, 255)).Direction = ParameterDirection.Output;
com.Parameters.Add(new SqlParameter("#ERR_NUMBER", SqlDbType.Int)).Direction = ParameterDirection.Output;
com.ExecuteNonQuery();
int ErrNo = int.Parse(com.Parameters["#ERR_NUMBER"].Value.ToString());
if (ErrNo != 0)
{
lblSuccess.Text = "Data saved successfully.";
grdPeople.EditIndex = -1;
}
else
{
lblError.Text = com.Parameters["#ERR_MESSAGE"].Value.ToString();
}
}
catch (Exception ex)
{
lblSuccess.Text = "";
lblError.Text = ex.Message;
}
}
The below you will find the design of the asp page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ValidationGridView.Default" EnableEventValidation="false" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Table runat="server">
<asp:TableRow>
<asp:TableCell>
<asp:TextBox runat="server" ID="txtSearch" />
</asp:TableCell>
<asp:TableCell>
<asp:Button runat="server" ID="btnSearch" Text="Search" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
<div>
<asp:GridView ID="grdPeople" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AutoGenerateColumns="false"
OnRowCommand="grdPeople_RowCommand"
OnRowEditing="grdPeople_RowEditing"
OnRowCancelingEdit="grdPeople_RowCancelingEdit"
OnRowUpdating="grdPeople_RowUpdating"
ShowFooter="true" DataKeyNames="pr_id">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("pr_name") %>' ID="lblName" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" Text='<%# Bind("pr_name") %>' runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName" ErrorMessage="Name is required." ForeColor="Red" Text="*" ValidationGroup="Fields" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox runat="server" ID="txtNameFooter" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtNameFooter" ErrorMessage="Name is required." ForeColor="Red" Text="*" ValidationGroup="FieldsFooter" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<asp:Label Text='<%# Bind("pr_gender") %>' runat="server" ID="lblGender" />
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButton ID="rdoMale" Text ="Male" Checked='<%# Bind("pr_gender_value") %>' GroupName="Gender" runat="server" />
<asp:RadioButton ID="rdoFemale" Text="Female" runat="server" GroupName="Gender" />
</EditItemTemplate>
<FooterTemplate>
<asp:RadioButton ID="rdoMaleFooter" Text="Male" checked="true" runat="server" GroupName="Gender" />
<asp:RadioButton ID="rdoFemaleFooter" Text="Female" runat="server" GroupName="Gender" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nationality">
<ItemTemplate>
<asp:Label Text='<%# Bind("pr_nationality") %>' runat="server" ID="lblNationality" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNationality" Text='<%# Bind("pr_nationality") %>' runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ControlToValidate="txtNationality" runat="server" ErrorMessage="Nationality is required." ForeColor="Red" Text="*" ValidationGroup="Fields" />
</FooterTemplate>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNationalityFooter" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtNationalityFooter" ErrorMessage="Nationality is required." ForeColor="Red" ValidationGroup="FieldsFooter" Text="*" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID Number">
<ItemTemplate>
<asp:Label Text='<%# Bind("pr_idn") %>' runat="server" ID="lblIDNo" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtIDNo" Text='<%# Bind("pr_idn") %>' runat="server" />
<asp:CustomValidator ID="cstIDNo" runat="server" Text="*" ControlToValidate="txtIDNo" ForeColor="Red" ValidateEmptyText="True" ValidationGroup="Fields" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtIDnoFooter" runat="server" />
<asp:CustomValidator ID="cstIDNoFooter" runat="server" Text="*" ControlToValidate="txtIDnoFooter" ForeColor="Red" ValidateEmptyText="True" ValidationGroup="FieldsFooter" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Passport">
<ItemTemplate>
<asp:Label Text='<%# Bind("pr_passport") %>' runat="server" ID="lblPassport" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPassport" Text='<%# Bind("pr_passport") %>' runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtPassport" ErrorMessage="Passport is required." ForeColor="Red" ValidationGroup="Fields" Text="*" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPassportFooter" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtPassportFooter" ErrorMessage="Passport is required." ForeColor="Red" ValidationGroup="FieldsFooter" Text="*" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Resident">
<ItemTemplate>
<asp:Label Text='<%# Bind("pr_resident") %>' runat="server" ID="lblResident" />
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButton ID="rdoYes" Text="Yes" Checked='<%# Bind("pr_resident_value") %>' GroupName="Resident" runat="server" />
<asp:RadioButton ID="rdoNo" Text="No" runat="server" GroupName="Resident" />
</EditItemTemplate>
<FooterTemplate>
<asp:RadioButton ID="rdoYesFooter" Text="Yes" Checked="true" GroupName="Resident" runat="server" />
<asp:RadioButton ID="rdoNoFooter" Text="No" runat="server" GroupName="Resident" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone">
<ItemTemplate>
<asp:Label Text='<%# Bind("pr_phone") %>' runat="server" ID="lblPhone" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPhone" Text='<%# Bind("pr_phone") %>' runat="server" />
<asp:CustomValidator ID="cstPhone" runat="server" Text="*" ControlToValidate="txtPhone" ForeColor="Red" ValidateEmptyText="True" ValidationGroup="Fields" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPhoneFooter" runat="server" />
<asp:CustomValidator ID="cstPhoneFooter" runat="server" Text="*" ControlToValidate="txtPhoneFooter" ForeColor="Red" ValidateEmptyText="True" ValidationGroup="FieldsFooter" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label Text='<%# Bind("pr_email") %>' runat="server" ID="lblEmail" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmail" Text='<%# Bind("pr_email") %>' runat="server" />
<asp:CustomValidator ID="cstEmail" runat="server" Text="*" ControlToValidate="txtEmail" ForeColor="Red" ValidateEmptyText="True" ValidationGroup="Fields" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEmailFooter" runat="server" />
<asp:CustomValidator ID="cstEmailFooter" runat="server" Text="*" ControlToValidate="txtEmailFooter" ForeColor="Red" ValidateEmptyText="True" ValidationGroup="FieldsFooter" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ImageUrl="~/icons/edit.png" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px" />
<asp:ImageButton runat="server" ImageUrl="~/icons/delete.png" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px" />
<asp:ImageButton runat="server" ImageUrl="~/icons/email.png" CommandName="Mail" ToolTip="Mail" Width="20px" Height="20px" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton runat="server" ImageUrl="~/icons/save.png" CommandName="Save" ToolTip="Save" Width="20px" Height="20px" ValidationGroup="Fields"/>
<asp:ImageButton runat="server" ImageUrl="~/icons/cancel.png" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px" />
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton runat="server" ID="btnInsert" ImageUrl="~/icons/add.png" ValidationGroup="FieldsFooter" CommandName="Insert" ToolTip="Insert" Width="20px" Height="20px" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Label runat="server" ID="lblSuccess" ForeColor="Green" />
<br />
<asp:Label runat="server" ID="lblError" ForeColor="Red" />
</div>
<div>
<asp:ImageButton ID="btnImportExl" runat="server" ImageUrl="~/icons/excel.png" Width="40px" Height="40px" />
</div>
<div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" ValidationGroup="Fields" />
</div>
</form>
</body>
</html>
I added the grid design above to show you how I designed it and how I set its properties.
Thank you.
In your GridView's EditItemTemplate, you miss out a button with CommandName="Update" which this button will trigger the grdPeople_RowUpdating event.
<EditItemTemplate>
<asp:ImageButton runat="server" ImageUrl="~/icons/save.png" CommandName="Save" ToolTip="Save" Width="20px" Height="20px" ValidationGroup="Fields"/>
<asp:ImageButton runat="server" ImageUrl="~/icons/cancel.png" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px" />
</EditItemTemplate>
SOLUTION
Change the Save button CommandName's value from CommandName="Save" to CommandName="Update".
<EditItemTemplate>
<asp:ImageButton runat="server" ImageUrl="~/icons/save.png" CommandName="Update" ToolTip="Save" Width="20px" Height="20px" ValidationGroup="Fields"/>
<asp:ImageButton runat="server" ImageUrl="~/icons/cancel.png" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px" />
</EditItemTemplate>
REFERENCE
Edit and Update Record in GridView in ASP.Net
I want to bind a image (which is stored in binary format column name image(varbinary)) in list view in c#..
i am trying this two but not working..
<img src='<%# string.Format("data:image/gif;base64,{0}",Convert.ToBase64String((byte[])Eval("image")))%>'/>--%>
<%--<asp:Image ID="Image10" runat="server" Height="100px" ImageUrl='<%# "ShowImage.ashx?id=" + Eval("image") %>' GenerateEmptyAlternateText="True" Visible='<%# ShowImg(Eval("image")) %>' />--%>
here is the code:
<div>
<h3 class="calDetail">Event Details</h3>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:VeteransDirectoryConnectionString %>" SelectCommand=
"SELECT [EventID], [OrganizationName], [Title], [DescriptionShort], [DescriptionLong], [Location], [EventDate], [URL], (CASE WHEN URL IS NULL OR URL = '' THEN CAST(0 AS bit) ELSE CAST(1 AS bit) END) AS URLLink, [FileName], (CASE WHEN FileName IS NULL OR FileName = '' THEN CAST(0 AS bit) ELSE CAST(1 AS bit) END) AS FileLink, [EventTime],image FROM [t_Event] WHERE ([EventID] = #EventID)">
<SelectParameters>
<asp:QueryStringParameter Name="EventID" QueryStringField="eventid" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<div style="padding: 0px 10px 20px 10px;">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="EventID" DataSourceID="SqlDataSource1">
<EmptyDataTemplate>
<span>No data for this event was found.</span>
</EmptyDataTemplate>
<ItemTemplate>
<span class="listCalLabel">Organization:</span>
<asp:Label ID="OrganizationNameLabel" runat="server" Text='<%# Eval("OrganizationName") %>' CssClass="listCalText" />
<br /><br />
<span class="listCalLabel">Event:</span>
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' CssClass="listCalText" />
<br /><br />
<span class="listCalLabel">Short Description:</span>
<asp:Label ID="DescriptionShortLabel" runat="server" Text='<%# Eval("DescriptionShort") %>' CssClass="listCalText" />
<br /><br />
<span class="listCalLabel">Long Description:</span>
<asp:Label ID="DescriptionLongLabel" runat="server" Text='<%# Eval("DescriptionLong") %>' CssClass="listCalText" />
<br /><br />
<span class="listCalLabel">Location:</span>
<asp:Label ID="LocationLabel" runat="server" Text='<%# Eval("Location") %>' CssClass="listCalText" />
<br /><br />
<span class="listCalLabel">Event Date:</span>
<asp:Label ID="EventDateLabel" runat="server" Text='<%# Eval("EventDate", "{0:MM/dd/yyyy}") %>' CssClass="listCalText" />
<br /><br />
<span class="listCalLabel">Event Time:</span>
<asp:Label ID="EventTimeLabel" runat="server" Text='<%# Eval("EventTime") %>' CssClass="listCalText" />
<br /><br />
<span class="listCalLabel"><asp:Label ID="lblURL" runat="server" Visible='<%# Eval("URLLink") %>' Text="Additional Info (website):" ></asp:Label></span>
<br />
<asp:HyperLink ID="URLHyperLink" runat="server" Text='<%# Eval("URL") %>' NavigateUrl='<%# Eval("URL") %>' Visible='<%# Eval("URLLink") %>' Target="_blank" CssClass="listLink" />
<br /><br />
<span class="listCalLabel"><asp:Label ID="lblFile" runat="server" Visible='<%# Eval("FileLink") %>' Text="Additional Info (file):"></asp:Label></span>
<br />
<asp:HyperLink ID="URLFile" runat="server" Text='<%# Eval("FileName") %>' NavigateUrl='<%# "EventFiles/" + Eval("FileName") %>' Visible='<%# Eval("FileLink") %>' Target="_blank" CssClass="listLink" />
<br /><br />
<%-- <img src='<%# string.Format("data:image/gif;base64,{0}",Convert.ToBase64String((byte[])Eval("image")))%>'/>--%>
<%--<asp:Image ID="Image10" runat="server" Height="100px" ImageUrl='<%# "ShowImage.ashx?id=" + Eval("image") %>' GenerateEmptyAlternateText="True" Visible='<%# ShowImg(Eval("image")) %>' />--%>
<br /></span>
</ItemTemplate>
<LayoutTemplate>
<div id="itemPlaceholderContainer" runat="server" style="">
<span runat="server" id="itemPlaceholder" />
</div>
<div style="">
</div>
</LayoutTemplate>
</asp:ListView>
<asp:Button ID="btnOK" runat="server" OnClientClick="window.close(); return false;" Text="Close Event Detail" CssClass="btn btn-default" />
<br />
<br />
</div>
</div>
what should i do???
how to retrieve the image and show ina a webform ??
You can use handler to display image in gridview , your html markup look like inside
Gridview ItemTemplate set image control src as src=~/getImage.ashx?id=" + id
where getImage.ashx is your handler which return MemoryStream((byte[])img);
for more details read http://www.aspdotnet-suresh.com/2015/07/save-retrieve-display-binary-images-in-gridview-from-database-in-aspnet-csharp-vbnet.html
I have a listview that is connected to sqlserver but the columns of listview are shown without any white space in-between. I want to add a feature like cellpadding in table to my Listview.
My code :
<asp:ListView ID="ListView1" runat="server" DataKeyNames="DarkhastId"
DataSourceID="SqlDataSource1" GroupItemCount="2">
<AlternatingItemTemplate>
<td runat="server" style="">
DarkhastId:
<asp:Label ID="DarkhastIdLabel" runat="server"
Text='<%# Eval("DarkhastId") %>' />
<br />
shakhs:
<asp:Label ID="shakhsLabel" runat="server" Text='<%# Eval("shakhs") %>' />
<br />
nam:
<asp:Label ID="namLabel" runat="server" Text='<%# Eval("nam") %>' />
<br />
Idgharardad:
<asp:Label ID="IdgharardadLabel" runat="server"
Text='<%# Eval("Idgharardad") %>' />
<br />
elat:
<asp:Label ID="elatLabel" runat="server" Text='<%# Eval("elat") %>' />
<br />
hamrah:
<asp:Label ID="hamrahLabel" runat="server" Text='<%# Eval("hamrah") %>' />
<br />
sabet:
<asp:Label ID="sabetLabel" runat="server" Text='<%# Eval("sabet") %>' />
<br />
vaziat:
<asp:Label ID="vaziatLabel" runat="server" Text='<%# Eval("vaziat") %>' />
<br />
karshenas:
<asp:Label ID="karshenasLabel" runat="server" Text='<%# Eval("karshenas") %>' />
<br />
tarikh:
<asp:Label ID="tarikhLabel" runat="server" Text='<%# Eval("tarikh") %>' />
<br />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
</td>
</AlternatingItemTemplate>
<EditItemTemplate>
<td runat="server" style="">
DarkhastId:
<asp:Label ID="DarkhastIdLabel1" runat="server"
Text='<%# Eval("DarkhastId") %>' />
<br />
shakhs:
<asp:TextBox ID="shakhsTextBox" runat="server" Text='<%# Bind("shakhs") %>' />
<br />
nam:
<asp:TextBox ID="namTextBox" runat="server" Text='<%# Bind("nam") %>' />
<br />
Idgharardad:
<asp:TextBox ID="IdgharardadTextBox" runat="server"
Text='<%# Bind("Idgharardad") %>' />
<br />
elat:
<asp:TextBox ID="elatTextBox" runat="server" Text='<%# Bind("elat") %>' />
<br />
hamrah:
<asp:TextBox ID="hamrahTextBox" runat="server" Text='<%# Bind("hamrah") %>' />
<br />
sabet:
<asp:TextBox ID="sabetTextBox" runat="server" Text='<%# Bind("sabet") %>' />
<br />
vaziat:
<asp:TextBox ID="vaziatTextBox" runat="server" Text='<%# Bind("vaziat") %>' />
<br />
karshenas:
<asp:TextBox ID="karshenasTextBox" runat="server"
Text='<%# Bind("karshenas") %>' />
<br />
tarikh:
<asp:TextBox ID="tarikhTextBox" runat="server" Text='<%# Bind("tarikh") %>' />
<br />
<asp:Button ID="UpdateButton" runat="server" CommandName="Update"
Text="Update" />
<br />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Cancel" />
<br />
</td>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<EmptyItemTemplate>
<td runat="server" />
</EmptyItemTemplate>
<GroupTemplate>
<tr ID="itemPlaceholderContainer" runat="server">
<td ID="itemPlaceholder" runat="server">
</td>
</tr>
</GroupTemplate>
<InsertItemTemplate>
<td runat="server" style="">
shakhs:
<asp:TextBox ID="shakhsTextBox" runat="server" Text='<%# Bind("shakhs") %>' />
<br />
nam:
<asp:TextBox ID="namTextBox" runat="server" Text='<%# Bind("nam") %>' />
<br />
Idgharardad:
<asp:TextBox ID="IdgharardadTextBox" runat="server"
Text='<%# Bind("Idgharardad") %>' />
<br />
elat:
<asp:TextBox ID="elatTextBox" runat="server" Text='<%# Bind("elat") %>' />
<br />
hamrah:
<asp:TextBox ID="hamrahTextBox" runat="server" Text='<%# Bind("hamrah") %>' />
<br />
sabet:
<asp:TextBox ID="sabetTextBox" runat="server" Text='<%# Bind("sabet") %>' />
<br />
vaziat:
<asp:TextBox ID="vaziatTextBox" runat="server" Text='<%# Bind("vaziat") %>' />
<br />
karshenas:
<asp:TextBox ID="karshenasTextBox" runat="server"
Text='<%# Bind("karshenas") %>' />
<br />
tarikh:
<asp:TextBox ID="tarikhTextBox" runat="server" Text='<%# Bind("tarikh") %>' />
<br />
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<br />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" />
<br />
</td>
</InsertItemTemplate>
<ItemTemplate>
<td runat="server" style="">
DarkhastId:
<asp:Label ID="DarkhastIdLabel" runat="server"
Text='<%# Eval("DarkhastId") %>' />
<br />
shakhs:
<asp:Label ID="shakhsLabel" runat="server" Text='<%# Eval("shakhs") %>' />
<br />
nam:
<asp:Label ID="namLabel" runat="server" Text='<%# Eval("nam") %>' />
<br />
Idgharardad:
<asp:Label ID="IdgharardadLabel" runat="server"
Text='<%# Eval("Idgharardad") %>' />
<br />
elat:
<asp:Label ID="elatLabel" runat="server" Text='<%# Eval("elat") %>' />
<br />
hamrah:
<asp:Label ID="hamrahLabel" runat="server" Text='<%# Eval("hamrah") %>' />
<br />
sabet:
<asp:Label ID="sabetLabel" runat="server" Text='<%# Eval("sabet") %>' />
<br />
vaziat:
<asp:Label ID="vaziatLabel" runat="server" Text='<%# Eval("vaziat") %>' />
<br />
karshenas:
<asp:Label ID="karshenasLabel" runat="server" Text='<%# Eval("karshenas") %>' />
<br />
tarikh:
<asp:Label ID="tarikhLabel" runat="server" Text='<%# Eval("tarikh") %>' />
<br />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
</td>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="groupPlaceholderContainer" runat="server" border="0" style="">
<tr ID="groupPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
<asp:DataPager ID="DataPager1" runat="server" PageSize="12">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<td runat="server" style="">
DarkhastId:
<asp:Label ID="DarkhastIdLabel" runat="server"
Text='<%# Eval("DarkhastId") %>' />
<br />
shakhs:
<asp:Label ID="shakhsLabel" runat="server" Text='<%# Eval("shakhs") %>' />
<br />
nam:
<asp:Label ID="namLabel" runat="server" Text='<%# Eval("nam") %>' />
<br />
Idgharardad:
<asp:Label ID="IdgharardadLabel" runat="server"
Text='<%# Eval("Idgharardad") %>' />
<br />
elat:
<asp:Label ID="elatLabel" runat="server" Text='<%# Eval("elat") %>' />
<br />
hamrah:
<asp:Label ID="hamrahLabel" runat="server" Text='<%# Eval("hamrah") %>' />
<br />
sabet:
<asp:Label ID="sabetLabel" runat="server" Text='<%# Eval("sabet") %>' />
<br />
vaziat:
<asp:Label ID="vaziatLabel" runat="server" Text='<%# Eval("vaziat") %>' />
<br />
karshenas:
<asp:Label ID="karshenasLabel" runat="server" Text='<%# Eval("karshenas") %>' />
<br />
tarikh:
<asp:Label ID="tarikhLabel" runat="server" Text='<%# Eval("tarikh") %>' />
<br />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
</td>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:akhbarrrConnectionString %>"
DeleteCommand="DELETE FROM [darkhastezam] WHERE [DarkhastId] = #DarkhastId"
InsertCommand="INSERT INTO [darkhastezam] ([shakhs], [nam], [Idgharardad], [elat], [hamrah], [sabet], [vaziat], [karshenas], [tarikh]) VALUES (#shakhs, #nam, #Idgharardad, #elat, #hamrah, #sabet, #vaziat, #karshenas, #tarikh)"
SelectCommand="SELECT * FROM [darkhastezam]"
UpdateCommand="UPDATE [darkhastezam] SET [shakhs] = #shakhs, [nam] = #nam, [Idgharardad] = #Idgharardad, [elat] = #elat, [hamrah] = #hamrah, [sabet] = #sabet, [vaziat] = #vaziat, [karshenas] = #karshenas, [tarikh] = #tarikh WHERE [DarkhastId] = #DarkhastId">
<DeleteParameters>
<asp:Parameter Name="DarkhastId" Type="Decimal" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="shakhs" Type="String" />
<asp:Parameter Name="nam" Type="String" />
<asp:Parameter Name="Idgharardad" Type="Decimal" />
<asp:Parameter Name="elat" Type="String" />
<asp:Parameter Name="hamrah" Type="String" />
<asp:Parameter Name="sabet" Type="String" />
<asp:Parameter Name="vaziat" Type="String" />
<asp:Parameter Name="karshenas" Type="String" />
<asp:Parameter Name="tarikh" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="shakhs" Type="String" />
<asp:Parameter Name="nam" Type="String" />
<asp:Parameter Name="Idgharardad" Type="Decimal" />
<asp:Parameter Name="elat" Type="String" />
<asp:Parameter Name="hamrah" Type="String" />
<asp:Parameter Name="sabet" Type="String" />
<asp:Parameter Name="vaziat" Type="String" />
<asp:Parameter Name="karshenas" Type="String" />
<asp:Parameter Name="tarikh" Type="String" />
<asp:Parameter Name="DarkhastId" Type="Decimal" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
Can you just add the cellspacing attribute?
<LayoutTemplate>
<table runat="server" cellspacing="5">
I would suggest adding css styling:
for example this item although this is for a button:
<asp:Button ID="CancelButton" runat="server" style="padding:15px;" CommandName="Cancel" Text="Clear" />
also see http://msdn.microsoft.com/en-us/library/9wx8w7we for css classes
Add CssClass to all table tags
<table class="test" ...
Create inline style (or external). You can change the styling to margin or padding-top / padding-bottom that fits your needs.
table.test td {
padding: 3px;
}
How can i insert particular comment on particular post? (like Facebook)
i have two table in sql
wall=>wallid, username,post
post comment=>wall id,comment,comment id
when i insert wall id manually, it works fine, but i want that the wall id should be generated automatically to the post i enter comment in.
In design part
<div style="text-align: center">
<asp:TextBox ID="TextBox1" runat="server" Height="61px" TextMode="MultiLine" Width="271px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Height="37px" Text="Submit" Width="91px" OnClick="Button1_Click" />
</div>
<div>
<br />
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<div style="border: solid 2px brown; width: 400px; margin-bottom: 20px">
<span style="">Id:
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("wallId") %>' />
<br />
post:
<asp:Label ID="postLabel" runat="server" Text='<%# Eval("post") %>' />
<br />
username:
<asp:Label ID="usernameLabel" runat="server" Text='<%# Eval("username") %>' />
<br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
<asp:ListView ID="ListView2" runat="server" DataSourceID="SqlDataSource2" DataKeyNames="commentID" InsertItemPosition="LastItem">
<AlternatingItemTemplate>
<span style="">commentID:<asp:Label Text='<%# Eval("commentID") %>' runat="server" ID="commentIDLabel" />
<br />
wallID:<asp:Label Text='<%# Eval("wallID") %>' runat="server" ID="wallIDLabel" />
<br />
comment:<asp:Label Text='<%# Eval("comment") %>' runat="server" ID="commentLabel" />
<br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</AlternatingItemTemplate>
<EditItemTemplate>
<span style="">commentID:<asp:Label Text='<%# Eval("commentID") %>' runat="server" ID="commentIDLabel1" />
<br />
wallID:<asp:TextBox Text='<%# Bind("wallID") %>' runat="server" ID="wallIDTextBox" />
<br />
comment:<asp:TextBox Text='<%# Bind("comment") %>' runat="server" ID="commentTextBox" />
<br />
<asp:Button runat="server" CommandName="Update" Text="Update" ID="UpdateButton" /><asp:Button runat="server" CommandName="Cancel" Text="Cancel" ID="CancelButton" /><br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</EditItemTemplate>
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
<InsertItemTemplate>
<span style="">wallID:<asp:TextBox Text='<%# Bind("wallID") %>' runat="server" ID="wallIDTextBox" />
<br />
comment:<asp:TextBox Text='<%# Bind("comment") %>' runat="server" ID="commentTextBox" />
<br />
<asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" /><asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="CancelButton" /><br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</InsertItemTemplate>
<ItemTemplate>
<span style="">commentID:<asp:Label Text='<%# Eval("commentID") %>' runat="server" ID="commentIDLabel" />
<br />
wallID:<asp:Label Text='<%# Eval("wallID") %>' runat="server" ID="wallIDLabel" />
<br />
comment:<asp:Label Text='<%# Eval("comment") %>' runat="server" ID="commentLabel" />
<br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</ItemTemplate>
<LayoutTemplate>
<div runat="server" id="itemPlaceholderContainer" style=""><span runat="server" id="itemPlaceholder" /></div>
<div style="">
</div>
</LayoutTemplate>
<SelectedItemTemplate>
<span style="">commentID:<asp:Label Text='<%# Eval("commentID") %>' runat="server" ID="commentIDLabel" />
<br />
wallID:<asp:Label Text='<%# Eval("wallID") %>' runat="server" ID="wallIDLabel" />
<br />
comment:<asp:Label Text='<%# Eval("comment") %>' runat="server" ID="commentLabel" />
<br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="SqlDataSource2" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' SelectCommand="SELECT * FROM [postcomment]" ConflictDetection="CompareAllValues" DeleteCommand="DELETE FROM [postcomment] WHERE [commentID] = #original_commentID AND [wallID] = #original_wallID AND (([comment] = #original_comment) OR ([comment] IS NULL AND #original_comment IS NULL))" InsertCommand="INSERT INTO [postcomment] ([wallID], [comment]) VALUES (#wallID, #comment)" OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [postcomment] SET [wallID] = #wallID, [comment] = #comment WHERE [commentID] = #original_commentID AND [wallID] = #original_wallID AND (([comment] = #original_comment) OR ([comment] IS NULL AND #original_comment IS NULL))">
<DeleteParameters>
<asp:Parameter Name="original_commentID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="original_wallID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="original_comment" Type="String"></asp:Parameter>
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="wallID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="comment" Type="String"></asp:Parameter>
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="wallID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="comment" Type="String"></asp:Parameter>
<asp:Parameter Name="original_commentID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="original_wallID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="original_comment" Type="String"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' SelectCommand="SELECT * FROM [wall]"></asp:SqlDataSource>
</div>
Your schema should look like
1. Post -> PostId,WallId,PostedBy, Post,PostedDateTime
2. Comments -> CommentId,PostId,Comments,CommentedBy,CommentedDateTime
Relaationship between Post and Wall should be 1 Wall and Many Posts that is 1:M relationship, similary for the comments, 1:M.
On Save
do the following:
Generate PostId automatically.
Send WallId from querystring or session or by any means you are identifying the friends wall. Similarly find out the current user id who owns the post and save the post and its datetime.
On comments, while saving comments, get the postid, and your own user id and leave the comment id autoincremented.
make the parameter direction as InputOutput parameter.
<asp:Parameter Name="wallID" Type="Int32" Direction="InputOutput"></asp:Parameter>
then you can use on Inserted event :
protected void ListView_Inserted(object sender, SqlDataSourceSelectingEventArgs e)
{
var wallID = e.Command.Parameters["#wallID"].Value ;
}
since you want the wallid to be automatically inserted as identity value. The you have to selected so that it you can grape it. try this
DECLARE #wallID AS INT = -1
INSERT INTO [postcomment]
([comment])
VALUES (#comment)
select #wallID =SCOPE_IDENTITY();
also it is not the ListView_Inserted event, it is the SqlDataSource2_Inserted
Is there a way were I could generate a message box if a record exist in the database after the user clicked the insert link button? I want the formview to check if record exist if not make an insert.
Help would be much appreciated.
Thanks in advance :)
Here's a sample of my code:
Manage Books
Add/Remove Books
Note: For the book ID/ISBN please refer to the barcode in the ISBN, usually located
at the back of the book. A barcode reader is required.
<EditItemTemplate>
Book ID/ISBN:
<asp:Label ID="bookidLabel1" runat="server" Text='<%# Eval("bookid") %>' />
<br />
Title:
<asp:TextBox ID="booktitleTextBox" runat="server"
Text='<%# Bind("booktitle") %>' />
<br />
Author's lastname:
<asp:TextBox ID="lastnameTextBox" runat="server"
Text='<%# Bind("lastname") %>' />
<br />
Author's firstname:
<asp:TextBox ID="firstnameTextBox" runat="server"
Text='<%# Bind("firstname") %>' />
<br />
Description:
<asp:TextBox ID="descriptionTextBox" runat="server"
Text='<%# Bind("description") %>' />
<br />
Category:
<asp:TextBox ID="categoryidTextBox" runat="server"
Text='<%# Bind("categoryid") %>' />
<br />
Date added:
<asp:TextBox ID="dateaddedTextBox" runat="server"
Text='<%# Bind("dateadded") %>' />
<br />
Status:
<asp:TextBox ID="statusidTextBox" runat="server"
Text='<%# Bind("statusid") %>' />
<br />
Quantity:
<asp:TextBox ID="quantityTextBox" runat="server"
Text='<%# Bind("quantity") %>' />
<br />
name:
<asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
Book ID:
<asp:TextBox ID="bookidTextBox" runat="server" Text='<%# Bind("bookid") %>' />
<asp:RequiredFieldValidator ID="RequesFieldValidator1" runat="server" ErrorMessage="* Required" ControlToValidate="bookidTextBox" ValidationGroup="InsertBook">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator" runat="server" Display="Dynamic" ControlToValidate="bookidTextBox" ValidationExpression="^([\S\s]{13,13})$" ErrorMessage="Invalid ID/ISBN. Please try again" ValidationGroup="InsertBook">
</asp:RegularExpressionValidator>
<br />
Title:
<asp:TextBox ID="booktitleTextBox" runat="server"
Text='<%# Bind("booktitle") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="* Required" ControlToValidate="booktitleTextBox" ValidationGroup="InsertBook">
</asp:RequiredFieldValidator>
<br />
Author's lastname:
<asp:TextBox ID="lastnameTextBox" runat="server"
Text='<%# Bind("lastname") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="* Required" ControlToValidate="lastnameTextBox" ValidationGroup="InsertBook">
</asp:RequiredFieldValidator>
<br />
Author's firstname:
<asp:TextBox ID="firstnameTextBox" runat="server"
Text='<%# Bind("firstname") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="* Required" ControlToValidate="firstnameTextBox" ValidationGroup="InsertBook">
</asp:RequiredFieldValidator>
<br />
Description:
<asp:TextBox ID="descriptionTextBox" runat="server"
Text='<%# Bind("description") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="* Required" ControlToValidate="descriptionTextBox" ValidationGroup="InsertBook">
</asp:RequiredFieldValidator>
<br />
Category:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="categoryDataSource" DataTextField="name"
DataValueField="categoryid" SelectedValue='<%# Bind("categoryid", "{0}") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="categoryDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [categoryid], [name] FROM [TblCategory]">
</asp:SqlDataSource>
<br />
Date added:
<asp:TextBox ID="dateaddedTextBox" runat="server"
Text='<%# Bind("dateadded") %>'/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="* Required" ControlToValidate="dateaddedTextBox" ValidationGroup="InsertBook">
</asp:RequiredFieldValidator>
<%--<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="dateaddedTextBox" ErrorMessage="RegularExpressionValidator"
ValidationExpression="(19|20)\d\d(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])">
</asp:RegularExpressionValidator>--%>
<br />
Status:
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="statusDataSource" DataTextField="statusname"
DataValueField="statusid" SelectedValue='<%# Bind("statusid", "{0}") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="statusDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [statusid], [statusname] FROM [BookStatus]">
</asp:SqlDataSource>
<br />
Quantity:
<asp:TextBox ID="quantityTextBox" runat="server"
Text='<%# Bind("quantity") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="* Required" ControlToValidate="quantityTextBox" ValidationGroup="InsertBook">
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Add" ValidationGroup="InsertBook"/>
<asp:Button ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
<EmptyDataTemplate>
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</EmptyDataTemplate>
<HeaderTemplate>
Add a new book
</HeaderTemplate>
</asp:FormView>
You can check in the ItemInserting method.
Something like:
void FormViewName_ItemInserting(object sender, FormViewInsertEventArgs e)
{
string somevalue = e.Values["somefieldtoget"];
//make your calls to the DB to check the somevalue doesn't exist
if(exists)
e.Cancel = true;
}
You can also do this on the data sources Inserting Method (sql datasource assumed) as well....
void datasourcename_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
...
}
Another approach as mentioned by #steve-wellens is if you have a primary key that is based on one or more of the fields in the form view (not an auto generated number then below will work great for catching an attempt to insert a duplicate key/record.
void FormViewName_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
if (e.Exception != null)
{
if (((SqlException)e.Exception).Number == 2627)
{
e.ExceptionHandled = true;
e.KeepInInsertMode = true;
// Display error message.
}
}
}
It would be better to put a unique index on the table and catch exceptions that occur when an attempt is made to insert a duplicate record.
It's the simplest way to ensure integrity. Otherwise you have to start a read transaction to handle the case where between the time you check and the time you insert, some other process is doing the exact same thing.