Asp.net how to solve the error - asp.net

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)

Related

How to print datagidview using Crystal Reports in ASP.NET using C#?

I would like to know if exists the way to print data table using Crystal Reports. I would like to know it work.
Please try to do like this and for printing, crystal report has option to print
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
using System.IO;
namespace CrystalReportWithOracle
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
my_rpt objRpt;
// Creating object of our report.
objRpt = new my_rpt();
String ConnStr = "SERVER=mydb;USER ID=user1;PWD=user1";
OracleConnection myConnection = new OracleConnection(ConnStr);
String Query1 = "select * from TableName";
OracleDataAdapter adapter = new OracleDataAdapter(Query1, ConnStr);
DataSet Ds = new DataSet();
// here my_dt is the name of the DataTable which we
// created in the designer view.
adapter.Fill(Ds, "my_dt");
if ( Ds.Tables[0].Rows.Count == 0 )
{
MessageBox.Show("No data Found", "CrystalReportWithOracle");
return;
}
// Setting data source of our report object
objRpt.SetDataSource(Ds);
CrystalDecisions.CrystalReports.Engine.TextObject root;
root = (CrystalDecisions.CrystalReports.Engine.TextObject)
objRpt.ReportDefinition.ReportObjects["txt_header"];
root.Text = "Sample Report By Using Data Table!!";
// Binding the crystalReportViewer with our report object.
crystalReportViewer1.ReportSource = objRpt;
}
}
}

Retrieve name and image from database where image is stored as upload folder and reference is stored in database?

Trying the following code but could't get the desired result.
I have stored the image in the upload folder and its reference is saved in database while storing the data in the database from the admin side.
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;
namespace Admin
{
public partial class fetch : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string cs =
ConfigurationManager.ConnectionStrings["DBSC"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spUploadImage", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter ParamId = new SqlParameter()
{
ParameterName = "#Id",
Value = Request.QueryString["Id"]
};
cmd.Parameters.Add(ParamId);
con.Open();
byte[] Bytes = (byte[])cmd.ExecuteScalar();
string strBase64 = Convert.ToBase64String(bytes);
FileUpload1.ImageUrl = "data:image/png;base64, +strBase64";
}
}
}
}

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

Resources