Selectedindexchanged does not show in the Web - c#

I have DropDownList
Whenever Data selected from the DDL, It autofills the textboxes specified.
It does get the values but does not show in the web.
Here is asp.net
<table class="col-lg-12" align="center" width="900">
<tr>
<td align="center" class="style2" valign="middle">
Name
</td>
<td align="center" class="style4" valign="middle">
ProductID
</td>
<td align="center" class="style4" valign="middle">
Price
</td>
<td align="center" class="style4" valign="middle">
Quantity
</td>
</tr>
<ItemTemplate>
<tr>
<td class="style3">
<asp:DropDownList ID="ddlName" runat="server" class="form-control"
AutoPostBack="True"
onselectedindexchanged="ddlName_SelectedIndexChanged" Width="200px"> </asp:DropDownList>
</td>
<td class="style5">
<asp:Label ID="lbID" runat="server" Width="200px"></asp:Label>
</td>
<td class="style5">
<asp:Label ID="lbPrice" runat="server" Width="200px"></asp:Label>
</td>
<td class="style5">
<asp:TextBox ID="Quantity" runat="server" Width="200px"></asp:TextBox>
</td>
<td>
<asp:Button ID="AddProduct" runat="server" style="color:White"
Text="Add Product" Width="200px" onclick="AddProduct_Click" /></td>
</tr>
</ItemTemplate>
ddlSelectedIndexChanged codes
protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
{
string Name = ddlName.SelectedItem.Value;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT Name, ProductID, Price FROM Products WHERE Name=#Name";
cmd.Parameters.AddWithValue("#Name", ddlName.SelectedValue);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
Int32 lbID = dr.GetInt32(1);
decimal lbPrice = dr.GetDecimal(2);
}
}
it doesn't fill the textboxes. Sorry for low profile question. huhuhu Help

I believe you should assign the Labels like this instead:
lbID.text = dr.GetInt32(1).ToString();
lbPrice.text = dr.GetDecimal(2).ToString();
Alternatively I would recommend looking into the DetailsView control for a more elegant, and easier in some cases, solution: DetailsVew Web Server Control

Related

C# = The ListView raised event which wasn't handled.

There is a ListView which shows the data that were retrieved from the database.
I want to edit a single record on a ListView by clicking the edit button beside it but if I press the edit button, I get the error:
The ListView 'lvItemView' raised event ItemEditing which wasn't handled.
Here is the ListView:
<asp:ListView ID="lvItemView" runat="server" ItemPlaceholderID="itemHolder">
<LayoutTemplate>
<table style="color: Black;" width="100%" border="0" cellpadding="5">
<tr>
<th style="text-align: center;">Customer ID</th>
<th style="text-align: center;">Contact Name</th>
<th style="text-align: center;">Company</th>
<th style="text-align: center;">Created By</th>
<th style="text-align: center;">Created Date</th>
<th style="text-align: center;">Modified By</th>
<th style="text-align: center;">Modified Date</th>
</tr>
<asp:PlaceHolder ID="itemHolder" runat="server"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td align="left">
<asp:Label ID="lblCustomerID" runat="Server" Text='<%#Eval("_CustomerID") %>' />
</td>
<td align="left">
<asp:Label ID="lblContactName" runat="Server" Text='<%#Eval("_ContactName") %>' />
</td>
<td align="left">
<asp:Label ID="lblCompany" runat="Server" Text='<%#Eval("_CompanyID") %>' />
</td>
<td align="left">
<asp:Label ID="lblCreatedBy" runat="Server" Text='<%#Eval("_CreatedBy") %>' />
</td>
<td align="left">
<asp:Label ID="lblCreatedDate" runat="Server" Text='<%#Eval("_CreatedDate") %>' />
</td>
<td align="left">
<asp:Label ID="lblModifiedBy" runat="Server" Text='<%#Eval("_ModifiedBy") %>' />
</td>
<td align="left">
<asp:Label ID="lblModifiedDate" runat="Server" Text='<%#Eval("_ModifiedDate") %>' />
</td>
<td align="left">
<asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit" CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>' />
</td>
<td align="left">
<asp:LinkButton ID="Delete" runat="Server" Text="Delete" CommandName="Delete" CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
And the code behind
protected void lvItemView_ItemEditing(object sender, ListViewEditEventArgs e)
{
System.Web.UI.WebControls.Label index_id = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCustomerID");
int customerID = int.Parse(index_id.Text);
Item item = new Item();
item._CustomerID = customerID;
System.Web.UI.WebControls.Label cmp_id = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCompany");
int companyID = int.Parse(cmp_id.Text);
item._CompanyID = companyID;
System.Web.UI.WebControls.Label samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblContactName");
item._ContactName = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCreatedBy");
item._CreatedBy = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCreatedDate");
item._CreatedDate = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblModifiedBy");
item._ModifiedBy = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblModifiedDate");
item._ModifiedDate = samp.Text;
CustomerID = item._CustomerID.ToString();
ContactName = item._ContactName.ToString();
CompanyID = item._CompanyID.ToString();
CreatedBy = item._CreatedBy.ToString();
CreatedDate = item._CreatedDate.ToString();
ModifiedBy = item._ModifiedBy.ToString();
ModifiedDate = item._ModifiedDate.ToString();
modAdd.Show();
}
I am new to asp.net and c# so I have no idea what to do with this kind of error.
set the event lvItemView_ItemEditing in OnItemEditing which will be triggered when click on edit button
<asp:ListView ID="lvItemView" runat="server" ItemPlaceholderID="itemHolder" OnItemEditing="lvItemView_ItemEditing">
and also set data source if you are binding on pageload
lvItemView.DataSource = SomeData;
lvItemView.DataBind();

How to Upload Video in database using C#.net in Visual studio 2008

I have below code to upload video files into database but when we upload video and click on upload button then my webpage go offline and data is not store into data base what happening I don't know.
my database table is
CREATE TABLE [dbo].[tblVideos](
[id] [int] IDENTITY(1,1) NOT NULL,
[Video_Name] [varbinary](50) NULL,
[Name] [varchar](50) NULL,
[ContentType] [varbinary](50) NULL,
[Data] [varbinary](max) NULL
) ON [PRIMARY]
my aspx code
<table style="margin-left:150px; height:546px">
<tr class="tr">
<td colspan="3">
<asp:Label ID="Label1" runat="server"
Font-Size="15pt" ForeColor="#0099FF" Text="Add New Video"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Font-Names="MS Reference Sans Serif"
Font-Size="7pt" Text="Make Sure Video in MP4 format"></asp:Label>
</td>
</tr>
<tr class="tr">
<td class="style1"> Video Name :</td>
<td><asp:TextBox ID="txtideoname" Height="24px" Width="270px" runat="server"></asp:TextBox></td>
<td>
</td>
</tr>
<tr class="tr">
<td class="style1">Video type :</td>
<td><asp:TextBox ID="txtvideotype" Height="24px" Width="270px" runat="server"></asp:TextBox></td>
<td>
</td>
</tr>
<tr class="tr">
<td class="style1">video:</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" /></td>
<td>
</td>
</tr>
<tr class="tr">
<td> </td>
<td><asp:Button ID="btnupload" runat="server" Text="Upload" Width="132px" Height="34px" BackColor="Red" BorderStyle="None" Font-Bold="True" ForeColor="White" onclick="btnupload_Click"/><br />
<asp:Label ID="lblMsg" runat="server"/></td>
<td>
</td>
</tr>
<tr class="tr">
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
my C# Code
using (BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream))
{
byte[] bytes = br.ReadBytes((int)FileUpload1.PostedFile.InputStream.Length);
string strConnString = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "insert into tblVideos(Video_Name,Name, ContentType, Data) values (#Video_Name,#Name, #ContentType, #Data)";
cmd.Parameters.AddWithValue("#Video_Name", txtideoname.Text);
cmd.Parameters.AddWithValue("#Name", Path.GetFileName(FileUpload1.PostedFile.FileName));
cmd.Parameters.AddWithValue("#ContentType", "video/mp4");
cmd.Parameters.AddWithValue("#Data", bytes);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
One potential problem is that both Video_Name and ContentType are also being stored as varbinary, and don't seem to be cast in your SQL. Have you tried changed them to varchar?

Use viewstate for Retaining the password in asp.net

I have a form in which i am using input field with name Password1 . after postback i need to maintain the value in password using viewstate . How can i do it in ASP.NET .My code is for a registration form in which i have given a password field .i have given auto postback for the dropdown list . when the auto postback occurs the password field is getting empty. instead of that i need to maintain the value of the password as it is.
<div>
<%--<asp:ScriptManager runat="server"></asp:ScriptManager>--%>
<ajaxToolkit:ToolkitScriptManager runat="server">
</ajaxToolkit:ToolkitScriptManager>
<h1 style="color:bisque;text-align:center;">USER FORM REGISTRATION</h1>
<table class="auto-style1" style="color:crimson">
<tr>
<td class="auto-style2">First Name</td>
<td class="auto-style3">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Last Name</td>
<td class="auto-style3">
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Password</td>
<td class="auto-style3">
<input id="Password1" type="password" runat="server"/></td>
</tr>
<tr>
<td class="auto-style2">Date of Birth</td>
<td class="auto-style3">
<asp:TextBox ID="TextBox2" runat="server"
ClientIDMode="Static" ReadOnly="true"></asp:TextBox>
<asp:Image ID="imgCal" DescriptionUrl="#.jpg"
AlternateText="" runat="server" />
<ajaxToolkit:CalendarExtender ID="Calend" runat="server"
Animated="true" PopupButtonID="imgCal" TargetControlID="TextBox2">
</ajaxToolkit:CalendarExtender>
</td>
</tr>
<tr>
<td class="auto-style2">Age</td>
<td class="auto-style3">
<asp:TextBox ID="TextBox7" runat="server"
ClientIDMode="Static" ReadOnly="true"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Gender</td>
<td class="auto-style3">
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
RepeatDirection="Horizontal">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList></td>
</tr>
<tr>
<td class="auto-style2">Nationality</td>
<td class="auto-style3">
<asp:DropDownList ID="DropDownList1" runat="server"
Width="145px" AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="0">n1</asp:ListItem>
<asp:ListItem Value="1">loc2</asp:ListItem>
<asp:ListItem Value="2">loc3</asp:ListItem>
<asp:ListItem Value="3">other</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox9" runat="server" Visible="false">
</asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Mobile Number</td>
<td class="auto-style3">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<asp:UpdatePanel ID="Update1" runat="server">
<ContentTemplate>
<tr>
<td class="auto-style2">Residential Adress</td>
<td class="auto-style3">
<textarea id="TextArea1" cols="20" name="TextArea1"
runat="server"></textarea></td>
</tr>
</ContentTemplate>
</asp:UpdatePanel>
</table>
</div>
by default password textbox will not retail value on postback.so you need following ways to preserve value based on this link
protected void Page_Load(object sender, EventArgs e)
{
string pwd = txtPwd.Text;
txtPwd.Attributes.Add("value", pwd);
}

Retrieve data from database and show it in textboxes

I am currently working on a web application in which I have to fetch data from database and show it in text boxes. I have these fields:
Name,Comp_Name,User_Name,Email_Id,Password,User_Image,Category,Status
and on top of it I have 1 text box where user can enter the id to see the data. For example if I write 1 in the textbox. Then the text boxes will show me the data of the record with id=1 which is stored in database.
ASPX Code:
<head runat="server">
<title>Untitled Page</title>
<link href="User.css" type="text/css" rel="Stylesheet" />
<script type="text/javascript" src="imgpreview.js"></script>
<script type="text/javascript">
function showimagepreview(input) {
if (input.files && input.files[0]) {
var filerdr = new FileReader();
filerdr.onload = function(e) {
$('#imgprvw').attr('src', e.target.result);
}
filerdr.readAsDataURL(input.files[0]);
}
}
</script>
<style type="text/css">
#Header
{
height: 65px;
}
.style1
{
height: 37px;
}
.style2
{
width: 217px;
height: 37px;
}
.style3
{
height: 37px;
width: 155px;
}
.style4
{
height: 129px;
width: 155px;
}
.style5
{
width: 217px;
height: 129px;
}
.style6
{
height: 129px;
}
</style>
<script>
window.onload=function()
{
document.getElementById("button").style.display='none';
}
function showButton(){
document.getElementById("button").style.display='block';
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="Full">
<br />
<br />
<br />
<div id="Main">
<div id="MainHead">
<asp:Panel ID="Panel1" runat="server" Height="66px">
<br />
<asp:Label ID="ID" runat="server" Text="ID"></asp:Label>
& nbsp;
<input type="text" id="userText" value="Change the text" runat="server" /> <asp:Button
ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</asp:Panel>
</div>
<br />
<table style="height: 871px; width: 504px;">
<tr>
<td class="style3">
<asp:Label ID="Name" runat="server" Font-Bold="True" ForeColor="Black"
Text="Name :"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate="txtName"
Text="field required!"
runat="server" />
</td>
</tr><tr>
<td class="style3">
</td>
<td class="style2">
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="CompName" runat="server" Font-Bold="True" ForeColor="Black"
Text="Comp Name :"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtcomp" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td class="style2">
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="UserName" runat="server" Font-Bold="True" ForeColor="Black"
Text="User Name :"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtuser" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
ControlToValidate="txtUser"
Text="field required!"
runat="server" />
</td>
</tr>
<tr>
<td class="style3">
</td>
<td class="style2">
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="Password" runat="server" Font-Bold="True" ForeColor="Black"
Text="Email-ID :"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3"
ControlToValidate="txtemail"
Text="field required!"
runat="server" />
</td>
</tr>
<tr>
<td class="style3">
</td>
<td class="style2">
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="Label5" runat="server" Font-Bold="True" ForeColor="Black"
Text="Password :"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtpass" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td class="style2">
</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="Label6" runat="server" Font-Bold="True" ForeColor="Maroon"
Text="User Image :"></asp:Label>
</td>
<td class="style5">
<input type="file" name="filUpload" id="filUpload" onchange="showimagepreview(this)" runat="server"/>
</td>
<td class="style6">
<asp:Panel ID="Panel2" runat="server" Height="122px">
<img id="imgprvw" alt="uploaded image preview"/>
</asp:Panel>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td class="style2">
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="Label7" runat="server" Font-Bold="True" ForeColor="Black"
Text="Category :"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtcat" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td class="style2">
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="Label8" runat="server" Font-Bold="True" ForeColor="Black"
Text="Status :"></asp:Label>
</td>
<td class="style2">
<asp:DropDownList ID="txtddl" runat="server" Height="28px" Width="151px"
AutoPostBack="True">
<asp:ListItem>Store Manager</asp:ListItem>
<asp:ListItem>Pro Manager</asp:ListItem>
<asp:ListItem>Engg</asp:ListItem>
<asp:ListItem>Admin</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td class="style2">
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="Label9" runat="server" Font-Bold="True" ForeColor="Black"
Text="Category No :"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtcatno" runat="server" Height="21px" Width="151px"> </asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td class="style2">
</td>
</tr>
<tr>
<td class="style3">
</td>
<td class="style2">
</td>
</tr>
<tr>
<td class="style1" colspan="2">
& nbsp; &n bsp;
<asp:Button id="Submit" runat="server" Text="Submit" onclick="Submit_Click" />
<input type="button" id="btnupd" value="New Button" runat="server" onclick="return btnupd_onclick()" />
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
C# Code:
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
public partial class _Default : System.Web.UI.Page
{
MySqlConnection con = new MySqlConnection("DataSource=localhost;database=mrp;userid=**;password=**;port=**");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Submit_Click(object sender, EventArgs e)
{
try
{
//This is my connection string i have assigned the database file address path
string MyConnection2 = "datasource=localhost;port=**;username=**;password=**;database=mrp";
//This is my insert query in which i am taking input from the user through windows forms
string Query = "insert into user_tbl(Name,Comp_Name,User_Name,Email_Id,Password,User_Image,Category,Status) values('" + this.txtName.Text + "','" + this.txtcomp.Text + "','" + this.txtuser.Text + "','" + this.txtemail.Text + "','" + this.txtpass.Text + "','" + this.filUpload.Value + "','" + this.txtcat.Text + "','" + this.txtddl.Text + "');";
//This is MySqlConnection here i have created the object and pass my connection string.
MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
//This is command class which will handle the query and connection object.
MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
MySqlDataReader MyReader2;
MyConn2.Open();
MyReader2 = MyCommand2.ExecuteReader(); // Here our query will be executed and data saved into the database.
MessageBox.Show("Save Data");
while (MyReader2.Read())
{
}
MyConn2.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
MySqlConnection con1 = new MySqlConnection("Data Source=localhost;database=mrp;userid=**;password=**;port=**");
var str = "select * from user_tbl,con1";
DataTable dt = new DataTable();
con1.Open();
MySqlDataReader myReader = null;
MySqlCommand myCommand = new MySqlCommand("Select * from user_tbl where Id=" + Id1, con1);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
txtName.Text = (myReader["Name"].ToString());
txtemail.Text = (myReader["Email_Id"].ToString());
txtddl.Text = (myReader["Status"].ToString());
txtpass.Text = (myReader["Password"].ToString());
txtcomp.Text = (myReader["Comp_Name"].ToString());
txtuser.Text = (myReader["User_Name"].ToString());
filUpload.Value = (myReader["User_Image"].ToString());
//and whatever you have to retrieve
}
con1.Close();
}
}
Use this code snippet:
SqlConnection con1 = new SqlConnection("your connection string");
DataTable dt = new DataTable();
con1.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("your query", con1);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
txtName.Text = (myReader["name"].ToString());
txtEmail.Text = (myReader["email"].ToString());
//and whatever you have to retrieve
}
con1.Close();

RequiredFieldValidator not working if I put a code in the button's Click property ASP.NET

Q: All the RequiredFieldValidators are working when I make the btnSubmitCheckup_Click property empty. But if I put a simple code inside it, the validators are not working anymore and more, it will execute the simple code I placed inside the Click property of the button.
How will prevent the program from going forward if the required fields are blank? I've set the ControlToValidate and it works flawlessly when there is no code inside the btnSubmitCheckup_Click but if I try to put a code inside it, it will go forward and disregard the RequiredFieldValidator.
protected void btnSubmitCheckup_Click(object sender, EventArgs e)
{
Response.Write("asdasda"); ////will work even all other textboxes are empty but validators won't appear
//_con.Open();
//SqlCommand NewCheckup = new SqlCommand("dbo.NewCheckup", _con);
//NewCheckup.CommandType = CommandType.StoredProcedure;
//NewCheckup.Parameters.Add("#PatientID", SqlDbType.Int).Value = RadioButtonList_Patient.SelectedValue;
//NewCheckup.Parameters.Add("#DeptCode", SqlDbType.VarChar).Value = RadioButtonList_Department.SelectedValue;
//NewCheckup.Parameters.Add("#EmpID", SqlDbType.Int).Value = RadioButtonList_Employee.SelectedValue;
//NewCheckup.Parameters.Add("#CheckupDate", SqlDbType.Date).Value = BasicDatePicker1.SelectedDate;
//NewCheckup.Parameters.Add("#Diagnosis", SqlDbType.VarChar).Value = txtDiagnosis.Text;
//NewCheckup.ExecuteNonQuery();
//_con.Close();
//Response.Write("<script>Alert('Checkup Successfully recorded!')</script>");
}
This is my source
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="NewCheckup.aspx.cs" Inherits="Clinic_WebDev.NewCheckup" %>
<%# Register assembly="BasicFrame.WebControls.BasicDatePicker" namespace="BasicFrame.WebControls" tagprefix="BDP" %>
<!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>Bargamed Clinic - New Checkup</title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 136px;
}
.style3
{
width: 250px;
}
#TextArea1
{
height: 104px;
width: 272px;
}
</style>
</head>
<body>
<table class="auto-style1">
<tr>
<td>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/InsertProcedure.aspx">Insert Procedure</asp:HyperLink>
</td>
<td>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/InsertPatient.aspx">Insert Patient</asp:HyperLink>
</td>
<td>
<asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/InsertEmployee.aspx">Insert Employee</asp:HyperLink>
</td>
<td>
<asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="~/InsertDepartment.aspx">Insert Department</asp:HyperLink>
</td>
<td>
<asp:HyperLink ID="HyperLink5" runat="server" NavigateUrl="~/NewCheckup.aspx">New Checkup</asp:HyperLink>
</td>
</tr>
</table>
<form id="form1" runat="server">
<table class="style1">
<tr>
<td align="right" bgcolor="#66FF66" class="style2">
Name of Patient</td>
<td bgcolor="#CCFFCC" class="style3">
<asp:TextBox ID="txtPatientName" runat="server" AutoPostBack="True"
TextMode="Search" Width="170px"></asp:TextBox>
</td>
<td bgcolor="#66FF66" style="margin-left: 80px">
Press Enter to search a patient
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
ControlToValidate="txtPatientName" ErrorMessage="Please search for a patient"
Font-Italic="True" Font-Names="Segoe UI" ForeColor="#666633"
EnableClientScript="False"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" bgcolor="#66FF66" class="style2">
Patient</td>
<td bgcolor="#CCFFCC" class="style3">
<asp:RadioButtonList ID="RadioButtonList_Patient" runat="server"
AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="PatientName"
DataValueField="PatientID">
</asp:RadioButtonList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:CLINICConnectionString %>"
SelectCommand="SELECT PatientID, FirstName + ' ' + MiddleName + ' ' + LastName AS PatientName FROM PATIENT WHERE (FirstName + ' ' + MiddleName + ' ' + LastName LIKE '%' + #PatientName + '%')">
<SelectParameters>
<asp:ControlParameter ControlID="txtPatientName" Name="PatientName"
PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
</td>
<td bgcolor="#66FF66">
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="RadioButtonList_Patient"
ErrorMessage="Please select the patient" Font-Italic="True" Font-Names="Segoe UI"
ForeColor="#666633"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" bgcolor="#66FF66" class="style2">
Department
</td>
<td bgcolor="#CCFFCC" class="style3">
<asp:RadioButtonList ID="RadioButtonList_Department" runat="server"
AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="DeptName"
DataValueField="DeptCode"
onselectedindexchanged="RadioButtonList_Department_SelectedIndexChanged">
</asp:RadioButtonList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CLINICConnectionString %>"
SelectCommand="SELECT DeptCode, DeptName, Status FROM DEPARTMENT">
</asp:SqlDataSource>
</td>
<td bgcolor="#66FF66">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="RadioButtonList_Department"
ErrorMessage="Please select the department" Font-Italic="True"
Font-Names="Segoe UI" ForeColor="#666633"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" bgcolor="#66FF66" class="style2">
Employee</td>
<td bgcolor="#CCFFCC" class="style3">
<asp:RadioButtonList ID="RadioButtonList_Employee" runat="server"
DataSourceID="SqlDataSource2" DataTextField="EmpName" DataValueField="EmpID">
</asp:RadioButtonList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:CLINICConnectionString %>"
SelectCommand="SELECT EmpID, EmpName, EmpStatus, Contact FROM EMPLOYEE WHERE (EmpID IN (SELECT EmpID FROM EMPLOYEE_SPECIALIZATION WHERE (DeptCode LIKE #DeptCode)))">
<SelectParameters>
<asp:ControlParameter ControlID="RadioButtonList_Department"
DefaultValue="None" Name="DeptCode" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</td>
<td bgcolor="#66FF66">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="RadioButtonList_Employee"
ErrorMessage="Please select the employee" Font-Italic="True"
Font-Names="Segoe UI" ForeColor="#666633"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" bgcolor="#66FF66" class="style2">
<asp:Label ID="Label1" runat="server" Text="Date of Checkup"></asp:Label>
</td>
<td bgcolor="#CCFFCC" class="style3">
<BDP:BasicDatePicker ID="BasicDatePicker1" runat="server" />
</td>
<td bgcolor="#66FF66">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="BasicDatePicker1"
ErrorMessage="Please select the date of checkup" Font-Italic="True"
Font-Names="Segoe UI" ForeColor="#666633"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" bgcolor="#66FF66" class="style2">
<asp:Label ID="Label2" runat="server" Text="Diagnosis"></asp:Label>
</td>
<td bgcolor="#CCFFCC" class="style3">
<asp:TextBox ID="txtDiagnosis" runat="server" Height="67px"
TextMode="MultiLine" Width="247px"></asp:TextBox>
</td>
<td bgcolor="#66FF66">
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="txtDiagnosis" ErrorMessage="Please put the diagnosis"
Font-Italic="True" Font-Names="Segoe UI" ForeColor="#666633"
ValidationGroup="ValidationGroupDaw"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td bgcolor="#66FF66" class="style2">
</td>
<td align="center" bgcolor="#CCFFCC" class="style3">
<asp:Button ID="btnSubmitCheckup" runat="server"
onclick="btnSubmitCheckup_Click" PostBackUrl="~/NewCheckup.aspx"
Text="Submit Checkup" CausesValidation="False"
ValidationGroup="ValidationGroupDaw" />
</td>
<td bgcolor="#66FF66">
</td>
</tr>
<tr>
<td bgcolor="#66FF66" class="style2">
</td>
<td bgcolor="#CCFFCC" class="style3">
</td>
<td bgcolor="#66FF66">
</td>
</tr>
</table>
</form>
</body>
</html>
are you talking your button is not working with the validation? try change your CausesValidation to true, cheer :)
<td align="center" bgcolor="#CCFFCC" class="style3">
<asp:Button ID="btnSubmitCheckup" runat="server"
onclick="btnSubmitCheckup_Click" PostBackUrl="~/NewCheckup.aspx"
Text="Submit Checkup" CausesValidation="true"
ValidationGroup="ValidationGroupDaw" />
</td>
First of all, your button contains CausesValidation="False" which will make it not call the client side validation.
The error occuring really is that it DOES validate if you don't fill in any code, but this might be because you can't tell that the button event was called regardless.
As for ensuring validation is called, you should add validation on your button click event as such:
protected void btnSubmitCheckup_Click(object sender, EventArgs e)
{
validate();
if(page.IsValid)
{
//your code here
}
else
{
//if you want to do something special if validation fails
}
}

Categories