Show text entered from textbox in a label asp net - c#

so I have this code in asp .net
Default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 248px;
}
.auto-style2 {
width: 253px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<table style="width:100%;">
<tr>
<td class="auto-style2">
</td>
<td>
<asp:SqlDataSource ID="Artikujt" runat="server" ConnectionString="<%$ ConnectionStrings:ChipString %>" SelectCommand="SELECT * FROM [Artikujt]"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style2">
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" ></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style2">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style2">
<asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style2">
<asp:PlaceHolder ID="PlaceHolder3" runat="server"></asp:PlaceHolder>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style2">
<asp:PlaceHolder ID="PlaceHolder4" runat="server"></asp:PlaceHolder>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style2">
<asp:PlaceHolder ID="PlaceHolder5" runat="server"></asp:PlaceHolder>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style2">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
<td> </td>
</tr>
</table>
<br />
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
AddItems();
}
}
DropDownList artikulli;
TextBox cmimi;
Label tregoCmimi;
TextBox sasia;
Label cmimiGjithsej;
protected void AddItems()
{
if (!string.IsNullOrEmpty(TextBox1.Text))
{
int a = int.Parse(TextBox1.Text);
for (int j = 1; j <= a; j++)
{
artikulli = new DropDownList();
cmimi = new TextBox();
tregoCmimi = new Label();
sasia = new TextBox();
cmimiGjithsej = new Label();
//artikulli.ID = j.ToString(IDUnik.ToString("N").Substring(31));
artikulli.ID = "artikulli_" + j.ToString();
artikulli.AutoPostBack = true;
cmimi.ID = "cmimi_" + j.ToString();
cmimi.AutoPostBack = true;
tregoCmimi.ID = "tregoCmimi_" + j.ToString();
sasia.ID = "sasia_" + j.ToString();
sasia.AutoPostBack = true;
cmimiGjithsej.ID = "cmimiGjithsej_" + j.ToString();
PlaceHolder1.Controls.Add(artikulli);
PlaceHolder2.Controls.Add(cmimi);
PlaceHolder3.Controls.Add(tregoCmimi);
PlaceHolder4.Controls.Add(sasia);
PlaceHolder5.Controls.Add(cmimiGjithsej);
DataTable listaArtikujt = new DataTable();
using (SqlConnection lidhje = new SqlConnection(ConfigurationManager.ConnectionStrings["ChipString"].ConnectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT [Artikulli] FROM [Artikujt]", lidhje);
adapter.Fill(listaArtikujt);
artikulli.DataSource = listaArtikujt;
artikulli.DataTextField = "Artikulli";
artikulli.DataBind();
}
catch (Exception ex)
{
Response.Write("Gabim:" + ex.ToString());
}
}
}
}
}
}
What I want to do is, after PostBack, after I have added number 2 it will show 2 DropDownlist, 2 Textboxes, 2 Labels, 2 Textboxes again and 2 other Labels.
I am trying to do after Post back, after I enter a number in TextBox cmimi = new TextBox(); it will automatically be added in the label below. I am trying to do but no luck.
I hope you understood my explanation.
Thank you. :)

Related

BC30456: 'btnSubmit_Click' is not a member of 'webform2_aspx'

I am new to C#. Can not understand whats this error means:
I am just following simple examples from
http://csharp-video-tutorials.blogspot.com/2012/10/calling-stored-procedure-with-output.html
this is my .aspx file
<!DOCTYPE html>
<table style="border: 1px solid black; font-family:Arial">
<tr>
<td>
Employee Name
</td>
<td>
<asp:TextBox ID="txtEmployeeName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Gender
</td>
<td>
<asp:DropDownList ID="ddlGender" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
Salary
</td>
<td>
<asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
</tr>
</table>
This is aspx.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
namespace adoDemo
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void btnSubmit_Click(object sender, EventArgs e)
{
//Read the connection string from Web.Config file
string ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConnectionString))
{
//Create the SqlCommand object
SqlCommand cmd = new SqlCommand("spAddEmployee", con);
//Specify that the SqlCommand is a stored procedure
cmd.CommandType = System.Data.CommandType.StoredProcedure;
//Add the input parameters to the command object
cmd.Parameters.AddWithValue("#Name", txtEmployeeName.Text);
cmd.Parameters.AddWithValue("#Gender", ddlGender.SelectedValue);
cmd.Parameters.AddWithValue("#Salary", txtSalary.Text);
//Add the output parameter to the command object
SqlParameter outPutParameter = new SqlParameter();
outPutParameter.ParameterName = "#EmployeeId";
outPutParameter.SqlDbType = System.Data.SqlDbType.Int;
outPutParameter.Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add(outPutParameter);
//Open the connection and execute the query
con.Open();
cmd.ExecuteNonQuery();
//Retrieve the value of the output parameter
string EmployeeId = outPutParameter.Value.ToString();
lblMessage.Text = "Employee Id = " + EmployeeId;
}
}
}
}
If i place this directive (I guess that is what it calls) on a top of .aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="adoDemo.WebForm2" %>
then I get another error:
System.Web.HttpException: Control 'txtEmployeeName' of type 'TextBox' must be placed inside a form tag with runat=server.
What is it that I am missing?
Your two pages are not communicating. You have not told the front-end who the backend is.
the # Page directive in the code-behind model contains attributes that
reference an external file and a class. These attributes link the .aspx page to its code.
https://msdn.microsoft.com/en-us/library/015103yb.aspx
Once you have added the attributres, you need to surround your markup with a <form></form> that has a runat="server" attribute.
User George Stocker gives an explanation on the runat="server" attribute.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="WebForm2.aspx.cs" Inherits="WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table style="border: 1px solid black; font-family:Arial">
<tr>
<td>
Employee Name
</td>
<td>
<asp:TextBox ID="txtEmployeeName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Gender
</td>
<td>
<asp:DropDownList ID="ddlGender" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
Salary
</td>
<td>
<asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>

Need Help in ASP.Net C# login page

What is wrong with this code? I have tried a lot of methods. But it always show login failed. No Build Errors though. I have a database named honeypot and a table called register in it,with username row and password row as varchars. I'm using built in login control. Can anyone help? I'm using Visual studio 2013.
home.aspx.cs
enter code here
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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 System.Xml.Linq;
using System.Data.SqlClient;
namespace CodeInjection4
{
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
private static int count = 0;
protected void log1_Authenticate(object sender, AuthenticateEventArgs e)
{
if (log1.UserName == "Admin" && log1.Password == "Admin")
{
Response.Redirect("Adminhome.aspx");
}
else if (YourValidationFunction(log1.UserName, log1.Password))
{
Session["User"] = log1.UserName;
e.Authenticated = true;
Response.Redirect("userhome.aspx");
log1.TitleText = "Successfully Logged In";
}
else
{
e.Authenticated = false;
count++;
if (count >= 3)
{
count = 0;
Session["User"] = log1.UserName;
Server.Transfer("MainPage.aspx");
}
}
}
private SqlConnection strConnection = new
SqlConnection("server=.\\SQLEXPRESS;database=honeypot;integrated security=true;");
private bool YourValidationFunction(string UserName, string Password)
{
bool boolReturnValue = false;
String SQLQuery = "SELECT UserName, Password FROM Register";
SqlCommand command = new SqlCommand(SQLQuery, strConnection);
SqlDataReader Dr;
try
{
strConnection.Open();
Dr = command.ExecuteReader();
while (Dr.Read())
{
if ((UserName == Dr["UserName"].ToString()) & (Password == Dr["Password"].ToString()))
{
boolReturnValue = true;
}
}
Dr.Close();
}
catch
{
}
return boolReturnValue;
}
protected void lnkRegis_Click(object sender, EventArgs e)
{
Response.Redirect("AdUserAcc.aspx");
}
}
}
Home.aspx
enter code here
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="CodeInjection4.Home" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
</script>
<style type="text/css">
#form1 {
text-align: center;
}
.auto-style1 {
width: 981px;
text-align: left;
}
.auto-style2 {
width: 961px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
Forestalling Code Injection</div>
<asp:Login ID="log1" OnAuthenticate="log1_Authenticate" runat="server" Width="1062px">
<LayoutTemplate>
<table cellpadding="1" cellspacing="0" style="border-collapse:collapse;">
<tr>
<td>
<table cellpadding="0">
<tr>
<td align="center" colspan="2">Log In</td>
</tr>
<tr>
<td align="right" class="auto-style2">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
</td>
<td class="auto-style1">
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="log1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" class="auto-style2">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
</td>
<td class="auto-style1">
<asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="log1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />
</td>
</tr>
<tr>
<td align="center" colspan="2" style="color:Red;">
<asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
</td>
</tr>
<tr>
<td align="right" colspan="2" style="text-align: center">
<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="log1" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:Login>
<br />
<asp:Button ID="Button1" runat="server" Text="Register" PostBackUrl="~/AdUserAcc.aspx" />
</form>
</body>
</html>
You are selecting all the users and looping through them. You have break out of the loop if you find a matching username and password such as
if ((UserName == Dr["UserName"].ToString()) & (Password == Dr["Password"].ToString()))
{
boolReturnValue = true;
break;
}
Othwerwise the next user will set it back to false.
A couple of notes:
Selecting all users and iterating through them is not scalable and wouldn't perform well. Instead you can pass in the username and password in WHERE clause. If you get a match then the login info is correct.
I'd recommend using logical-AND operator (&&) instead of bitwise-AND (&). Here's a SO thread with related discussion: Usage & versus &&
Consider using salted password hashes as opposed to plaintext passwords.

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();

aspx page save on pdf

Here is my ASPX Code:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" EnableEventValidation="false" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<table align="center">
<tr>
<td colspan="3">
<asp:Label ID="Label6" runat="server" Text="Header Text" style="color:Black;"></asp:Label>
</td>
<td colspan="2" align="right">
<asp:Label ID="Label2" runat="server" Text="Invoice Number : " style="color:Black;"></asp:Label>
<asp:Label ID="Label3" runat="server" Text="" style="color:Black;"></asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="Label7" runat="server" Text="Address :" style="color:Black;"></asp:Label><br />
</td>
</tr>
<tr>
<td height="30px" colspan="5">
</td>
</tr>
<tr>
<td align="center" width="150px">
<asp:Label ID="Label22" runat="server" Text="Item" style="color:Black;"></asp:Label>
</td>
<td align="center" width="150px">
<asp:Label ID="Label23" runat="server" Text="Quantity" style="color:Black;"></asp:Label>
</td>
<td align="center" width="150px">
<asp:Label ID="Label24" runat="server" Text="Amount" style="color:Black;"></asp:Label>
</td>
<td align="center" width="150px">
<asp:Label ID="Label25" runat="server" Text="Paid Amount" style="color:Black;"></asp:Label>
</td>
<td align="center" width="150px">
<asp:Label ID="Label26" runat="server" Text="Balance Amount" style="color:Black;"></asp:Label>
</td>
</tr>
<tr>
<td colspan="5" height="5px">
</td>
</tr>
<tr>
<td colspan="5" height="30px">
</td>
</tr>
<tr>
<td colspan="3" valign="top" align="left">
</td>
<td colspan="2" align="right" valign="top">
<table>
<tr>
<td>
<asp:Label ID="Label37" runat="server" Text="Total Amount" style="color:Black;"></asp:Label>
</td>
<td>
<asp:Label ID="Label38" runat="server" Text="" style="color:Black;"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label39" runat="server" Text="Paid Amount" style="color:Black;"></asp:Label>
</td>
<td>
<asp:Label ID="Label40" runat="server" Text="" style="color:Black;"></asp:Label>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="5" height="20px">
</td>
</tr>
<tr>
<td colspan="5">
<asp:Label ID="Label41" runat="server" Text="Terms & Condition" style="color:Black;"></asp:Label><br />
</td>
</tr>
<tr>
<td colspan="5" align="right">
<asp:Button ID="Button1" runat="server" Text="Send" onclick="Button1_Click" />
</td>
</tr>
</table>
</asp:Content>
and my C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Text;
using System.Drawing;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string attachment = "attachment; filename=" + "abc" + ".pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter s_tw = new StringWriter();
HtmlTextWriter h_textw = new HtmlTextWriter(s_tw);
h_textw.AddStyleAttribute("font-size", "7pt");
h_textw.AddStyleAttribute("color", "Black");
Document doc = new Document();
doc = new Document(PageSize.A4, 5, 5, 15, 5);
FontFactory.GetFont("Verdana", 80, iTextSharp.text.Color.RED);
PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();
StringReader s_tr = new StringReader(s_tw.ToString());
HTMLWorker html_worker = new HTMLWorker(doc);
html_worker.Parse(s_tr);
doc.Close();
Response.Write(doc);
}
}
I want that if i click on Send Button my ASPX Page save on PDF
My ASPX Page is that:
The problem is that when i click on Send button it gives the following error
The document has no pages
I am using Visual Studio 2010 and ASP.Net C#
I solved my issue using this Code
string attachment = "attachment; filename=" + Session["pdf_name"] + ".pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter s_tw = new StringWriter();
HtmlTextWriter h_textw = new HtmlTextWriter(s_tw);
h_textw.AddStyleAttribute("font-size", "8pt");
h_textw.AddStyleAttribute("color", "Black");
Panel1.RenderControl(h_textw);//Name of the Panel
Document doc = new Document();
PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();
StringReader s_tr = new StringReader(s_tw.ToString());
HTMLWorker html_worker = new HTMLWorker(doc);
html_worker.Parse(s_tr);
doc.Close();
Response.Write(doc);
Thanks for helping me.

Sum values in dynamic control asp net

I have created a dynamic control script. What I want to do is to Sum all "LabelVlera" that is in "Details.ascx" in a single Label that is "LabelTotal" in ".aspx.cs file".
I hope you understood me. thank you.
On the UserControl Folder I have this script...
Details.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="ContactDetails.ascx.cs" Inherits="UserControl_ContactDetails" EnableViewState="false" %>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 150px;
}
.auto-style3 {
width: 294px;
}
.auto-style4 {
width: 150px;
text-align: right;
}
</style>
<table class="auto-style1">
<tr>
<td class="auto-style4">
<asp:Label ID="Label1" runat="server" Text="Artikulli:"></asp:Label>
</td>
<td class="auto-style3">
<asp:DropDownList ID="DropDownListArtikulli" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListArtikulli_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="DropDownListArtikulli" ErrorMessage="Zgjidhni artikullin"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="Label2" runat="server" Text="Cmimi:"></asp:Label>
</td>
<td class="auto-style3">
<asp:Label ID="LabelCmimi" runat="server"></asp:Label>
<asp:Label ID="Label3" runat="server" Text="lek"></asp:Label>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="Label4" runat="server" Text="Sasia:"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="TextBoxSasia" runat="server" AutoPostBack="True"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBoxSasia" ErrorMessage="Vendosni sasine"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="Label5" runat="server" Text="Vlera:"></asp:Label>
</td>
<td class="auto-style3">
<asp:Label ID="LabelVlera" runat="server" ></asp:Label>
<asp:Label ID="Label6" runat="server" Text="lek"></asp:Label>
</td>
<td> </td>
</tr>
</table>
<hr />
Details.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class UserControl_ContactDetails : System.Web.UI.UserControl
{
#region Public Properties
public Label VlTotal
{
get
{
return LabelVlera;
}
set
{
LabelVlera = value;
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
DropDownListArtikulli.Text = Request.Form[DropDownListArtikulli.UniqueID];
LabelCmimi.Text = Request.Form[LabelCmimi.UniqueID];
TextBoxSasia.Text = Request.Form[TextBoxSasia.UniqueID];
LabelVlera.Text = Request.Form[LabelVlera.UniqueID];
}
Cmimi();
Vlera();
}
private void Cmimi()
{
DataTable listaArtikujt = new DataTable();
using (SqlConnection lidhje = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM [Artikujt]", lidhje);
adapter.Fill(listaArtikujt);
DropDownListArtikulli.DataSource = listaArtikujt;
DropDownListArtikulli.DataTextField = "Artikulli";
DropDownListArtikulli.DataValueField = "Cmimi";
DropDownListArtikulli.DataBind();
LabelCmimi.Text = DropDownListArtikulli.SelectedValue.ToString();
}
catch (Exception ex)
{
Response.Write("Gabim:" + ex.ToString());
}
}
}
protected void DropDownListArtikulli_SelectedIndexChanged(object sender, EventArgs e)
{
Cmimi();
}
private void Vlera()
{
if(!string.IsNullOrEmpty(TextBoxSasia.Text))
{
LabelVlera.Text = TextBoxSasia.Text.ToString();
int a = Convert.ToInt32(LabelCmimi.Text);
int b = Convert.ToInt32(LabelVlera.Text);
int c = a * b;
LabelVlera.Text = c.ToString();
}
}
protected void TextBoxSasia_TextChanged(object sender, EventArgs e)
{
Vlera();
}
}

Categories