Fileupload in CreateUserWizard - createuserwizard

I have a createuserwizard (cuw) control and I want to add more fields (customizing). I need to store this info in a database called 'UserProfiles'. I´ve added a fileupload control to store an image (avatar) and the problem is here. When I refresh my database (after I filled the cuw control) I can see the new data for all the columns, EXCEPT for the image column. I supose the problem is in the code behind. This is my code:
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.Web.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
CreateUserProfile(CreateUserWizard1.UserName, txtFirstName.Text, txtLastName.Text, CreateUserWizard1.Email, CreateUserWizard1.Password, imgUpload.FileName);
}
private void CreateUserProfile(string UserName, string Gender, string LookingFor, string Email, string Password, string Image)
{
string conString = WebConfigurationManager.ConnectionStrings["UserProfiles"].ConnectionString;
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("INSERT UserProfiles (UserName,Gender,LookingFor,EMail,Password,Image) VALUES (#UserName,#Gender,#LookingFor,#EMail,#Password,#Image)", con);
cmd.Parameters.AddWithValue("#UserName", UserName);
cmd.Parameters.AddWithValue("#Gender", Gender);
cmd.Parameters.AddWithValue("#LookingFor", LookingFor);
cmd.Parameters.AddWithValue("#EMail", Email);
cmd.Parameters.AddWithValue("#Password", Password);
cmd.Parameters.AddWithValue("#Image", Image);
using (con)
{
con.Open();
cmd.ExecuteNonQuery();
}
}
}
I´m a beginner. Thanks in advance for your help.

Related

Absolute expiration and sliding expiration are not working(caching)

// guys please help the absolute caching and sliding caching are not working i am unable to find where is the problem
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Web.Caching;
namespace WebApplication154
{
public partial class WebForm1 : System.Web.UI.Page
{
string cs =
WebConfigurationManager.ConnectionStrings["cs"].ConnectionString;
code to bind grid view when the page is loading
protected void Page_Load(object sender, EventArgs e)
{
lblserver.Text = DateTime.Now.ToString();
DataSet ds = getdata();
Cache["products"] = ds;
grid1.DataSource = ds;
grid1.DataBind();
}
i am passing data from database to dataset
public DataSet getdata()
{
SqlConnection conn = new SqlConnection(cs);
SqlDataAdapter da = new SqlDataAdapter("spfrag", conn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
the data is not caching even i mentioned the below code
protected void btnsubmit_Click(object sender, EventArgs e)
{
if (Cache["products"] != null)
{
DataSet ds = (DataSet)Cache["products"];
grid1.DataSource = ds;
grid1.DataBind();
}
else
{
DataSet ds = getdata();
Cache.Add("products", ds, null, Cache.NoAbsoluteExpiration,
TimeSpan.FromSeconds(20), CacheItemPriority.Default, null);
grid1.DataSource = ds;
grid1.DataBind();
}
}
}
You probably want to try pulling from the cache in getdata() and only pull from the database if the cache is empty there. Check out the Cache Repository Pattern for an example
The way this coded now you would only ever pull from the cache on the second btnsubmit_Click event fire. You would end up going to the database on every Page_Load too, which fires on btnsubmit_Click too.

Crsytall Report is not showing in web page asp.net

I want to create crystal report in asp.net but when i click the report is not shown in the web page. Only a blank web page is shown and the button and the text field is shown through which i give parameter to my report.
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 CrystalDecisions.CrystalReports;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Reporting;
using CrystalDecisions.CrystalReports.Engine;
using System.Data;
public partial class Report_Viewer : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Regist"].ConnectionString);
ReportDocument rdoc = new ReportDocument();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = new SqlCommand("Regist", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
//cmd.Parameters.AddWithValue("#AccountNumber",AccNumTB.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable ds = new DataTable();
da.Fill(ds);
rdoc.Load(Server.MapPath("CrystalReport.rpt"));
rdoc.SetDataSource(ds);
rdoc.SetParameterValue("Frequency", AccNumTB.Text);
CrystalReportViewer1.ReportSource = rdoc;
CrystalReportViewer1.DataBind();
conn.Close();
}
}

how to show a record values in text box in asp.net

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.Data;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
binddata();
}
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
void binddata()
{
string pid = Request.QueryString["Prod_Id"];
con = new SqlConnection("Data Source=.\\sqlexpress; initial catalog=PracticeDb; user id=sa; pwd=manager;");
con.Open();
da = new SqlDataAdapter("Select * from Products where Prod_Id='" + pid + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
listview.DataSource = ds;
listview.DataBind();
}
}
}
the user is directed to the record updation page when he click the edit link in the record list page. what should i do write in datasource and databind
You have to extract the values from DataSet like
string name = ds.Tables[0].Rows[0]["name"].ToString();
Here i am assuming that you have a name as a field in your select query, you have to use your fields.
After getting that field assign it to TextBox like
TextBox1.Text = name ;
After that run your update query based on Primary Key.
One Important Suggestion :-
Try to use Page.IsPostBack property of your Page on Page_Load.Need to change your Page_Load like
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
binddata();
}
}
Hope you understand and works for you.
Try to use this.
TextBox1.Text = ds.Tables[0].Rows[0]["name"].Tostring();

how to remove error Incorrect syntax near 'nvarchar'. Must declare the scalar variable "#"

I am getting this error Incorrect syntax near 'nvarchar'. Must declare the scalar variable "#". I am using the code as mentioned below . Here SACALOGIN.MDF is Database name admin_login is table name. User-Name and Password are table columns and admin1.aspx is another web page ...please help as it is giving me a great headache .....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.Configuration.Common;
using System.Web.Configuration.Internal;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection cn=new SqlConnection();
cn.ConnectionString =
WebConfigurationManager.ConnectionStrings["SACALOGIN.MDF"].ConnectionString;
cn.Open();
string sql="Select * from admin_login where ID=#[ID]";
SqlCommand cmd=new SqlCommand(sql,cn);
cmd.Parameters.AddWithValue("#[User-Name]",txtUserName.Text);
cmd.Parameters.AddWithValue("#Password",txtPWD.Text);
SqlDataReader dr=cmd.ExecuteReader();
bool found=false;
if(dr.Read())
{
found=true;
cn.Close();
if(found)
{
Response.Redirect("admin1.aspx");
}
else
lblMessage.Text="Sorry! Invalid User Id.";
}
}
}
You're using a 'ID' parameter , but the only parameters you're adding to your command are '#user-name' and '#Password'. Try to replace :
string sql="Select * from admin_login where ID=#[ID]";
SqlCommand cmd=new SqlCommand(sql,cn);
cmd.Parameters.AddWithValue("#[User-Name]",txtUserName.Text);
cmd.Parameters.AddWithValue("#Password",txtPWD.Text);
by :
string sql="Select * from admin_login where [ID]=#ID";
SqlCommand cmd=new SqlCommand(sql,cn);
cmd.Parameters.Add("#ID", SqlDbType.Int);
cmd.Parameters["#ID"].Value = userID;
of course, replace this last 'userID' by whatever your userid variable is named...
I dont think your code will ever work.
It should be something like this
SqlConnection cn=new SqlConnection();
cn.ConnectionString = W WebConfigurationManager.ConnectionStrings["SACALOGIN.MDF"].ConnectionString;
cn.Open();
string sql="Select * from admin_login where [User-Name] = #Username and Password= #Password";
SqlCommand cmd=new SqlCommand(sql,cn);
cmd.Parameters.AddWithValue("#Username",txtUserName.Text);
cmd.Parameters.AddWithValue("#Password",txtPWD.Text);
SqlDataReader dr=cmd.ExecuteReader();
bool found=false;

Convert Datagrid or Gridview to excel with Page break

I need to convert either an ASP.NET Datagrid or GridView to excel with a page-break that can be part of the target excel.
The first part to convert the grid control to excel is not that complex and fairly easy to do, but my problem is how to insert a page break to the excel file during the conversion process.
Thanks for the help.
Here is the complete code to Export GridView to Excel:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class ExportGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = BindData();
GridView1.DataBind();
}
}
private string ConnectionString
{
get { return #"Server=localhost;Database=Northwind;
Trusted_Connection=true"; }
}
private DataSet BindData()
{
// make the query
string query = "SELECT * FROM Categories";
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
DataSet ds = new DataSet();
ad.Fill(ds, "Categories");
return ds;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;
filename=FileName.xls");
Response.Charset = "";
// If you want the option to open the Excel file without saving than
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for the
specified ASP.NET server control at run time.
}
}

Resources