Crsytall Report is not showing in web page asp.net - 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();
}
}

Related

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

ArgumentException when trying to open a SqlConnection

This code is for the button:
But every time i run it say's ArgumentException was unhandled by user code.
It's a site made to look up in at Database to see if there is enough ink.
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;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into Toner Values('"+txtFname.Text+"','"+txtLname.Text+"','"+TxtCity.Text+"')",con);
cmd.ExecuteNonQuery();
con.Close();
Label1.Visible = true;
Label1.Text = "Indsætning succesfuld!!!";
TxtCity.Text = "";
txtFname.Text = "";
txtLname.Text = "";
}
protected void TextBox3_TextChanged(object sender, EventArgs e)
{
}
}
My best guess is that TxtCity should be txtCity.
Try this:
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;
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string query = "insert into Toner Values (#Fname, #Lname, #City)";
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString);
SqlCommand cmd = new SqlCommand(query, con);
//Use parameterized query to prevent SQL injection
cmd.Parameters.Add("Fname", SqlDbType.VarChar, 50).Value = txtFname.Text;
cmd.Parameters.Add("Lname", SqlDbType.VarChar, 50).Value = txtLname.Text;
//C# is case-sensitive... is it txtCity or TxtCity?
cmd.Parameters.Add("City", SqlDbType.VarChar, 50).Value = TxtCity.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Label1.Visible = true;
Label1.Text = "Indsætning succesfuld!!!";
TxtCity.Text = "";
txtFname.Text = "";
txtLname.Text = "";
}
}

Fileupload in 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.

Asp.net how to solve the error

Im designing my web page, in that i have typed in the book id and typed Nameofthebook. and then click the button the stack of book are available is go to another page and then not available is display the error message is book is not available My project (Library management system)
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;
public partial class Bookcheck : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string constr = null;
constr = ConfigurationManager.ConnectionStrings["librarymanagementconnetionstring"].ConnectionString;
SqlConnection cnn = new SqlConnection(constr);
cnn.Open();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("SELECT*FROM BOOKREGISTRATIONDETAILS WHERE bookId='" + txtid.Text.Trim() + "'AND Nameofbook='" + txtnb.Text.Trim() + "'", constr);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
Response.Redirect("Issueofbook.aspx");
}
else
{
Msg.Text = "NO book available";
Msg.Visible = true;
}
}
}
Error:
Object reference not set to an instance of an object.
I noticed that in librarymanagementconnetionstring, connection is spelled incorrectly. Is this the correct entry in your web.config for the connection string?
I'd say that it's the line
if (ds.Tables[0].Rows.Count > 0)
that giving the error.
If your DataSet has no tables then ds.Tables could be null.
If that's a possibility then you need to code for that.
If there are no rows ds.Tables[0].Rows won't be null and your test should work.
Change
if (ds.Tables[0].Rows.Count > 0)
to
if(ds.HasRows)

Why The Gridview "AutoGenerateEditButton = true "property does not work in run time?

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;
public partial class ExptGridviewEdit : System.Web.UI.Page
{
SqlCommand com;
SqlDataAdapter da;
DataTable dtb;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["gj"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
//if (!Page.IsPostBack)
//{
com = new SqlCommand("Select * from tblExpt", con);
da = new SqlDataAdapter(com);
dtb = new DataTable();
da.Fill(dtb);
if (dtb.Rows[0] != null)
{
BindData();
}
GridView1.AutoGenerateEditButton = true;
GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
GridView1.DataKeyNames = new string[] { "id" };
GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
// }
}
protected void BindData()
{
GridView1.DataSource = dtb;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = dtb;
GridView1.DataBind();
}
}
When a data source control that supports updating is bound to a GridView control, the GridView control can take advantage of the data source control's capabilities and provide automatic updating functionality.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogenerateeditbutton.aspx

Resources