how to import visual basic function into asp.net - asp.net

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.VisualBasic;
public partial class ReportGeneration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet query = (DataSet)Session["myDataset"];
GridView1.DataSource = query;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DateTime dt;
long age;
Datefunctions3.DateClass d1 = new Datefunctions3.DateClass();
//DateFunctions2.DateClass d1 = new DateFunctions2.DateClass();
if (e.Row.RowType == DataControlRowType.DataRow)
{
string s1 = e.Row.Cells[8].Text;
dt = Convert.ToDateTime(e.Row.Cells[8].Text);
// age = d1.DateDifference(da.Year, dt, DateTime.Now);
age = d1.DateDifference(DateInterval.year, d1, DateTime.Now);
e.Row.Cells[8].Text = age.ToString();
}
}
}
This is my code it shows error under the dateinterval not able to solve the problem have added a dll file of my visual basic function into the BIN folder as well

Here's an extension function that will give you the age w/o needing to use a VB function:
<Extension()> Function Age(dStart As DateTime) As Integer
Dim dNow As DateTime = DateTime.Now
Return dNow.Year - dStart.Year - If(dNow.Month > dStart.Month OrElse dNow.Month = dStart.Month AndAlso dNow.Day > dStart.Day,0,1)
End Function
usage:
age = d1.Age
or, if you don't want to use an extension, use it directly:
Dim dNow As DateTime = DateTime.Now
age = dNow.Year - d1.Year - If(dNow.Month > d1.Month OrElse dNow.Month = d1.Month AndAlso dNow.Day > d1.Day,0,1)
I just noticed that your code is C#... you'll need to convert my vb to C#

Related

Search bar by using LinQ

I want to show the results only that match the user input, but this code will show the whole table, do you know what is the problem? For example, if the user enters "233442" id it will only show him the users that their id "233442". The user could search by id, name, or username.
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.Configuration;
using System.Data.SqlClient;
namespace SearchBarLinQ
{
public partial class SearchBarLinQ : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
DataClasses1DataContext db = new DataClasses1DataContext();
protected void btnsearch_Click(object sender, EventArgs e)
{
var st = (from s in db.SearchTable2s where s.ID == int.Parse(TxtID.Text) select s).First();
TxtName.Text = st.Name;
TxtUsername.Text = st.Username;
LoadData();
}
void LoadData()
{
var st = from s in db.SearchTable2s select s;
GridView1.DataSource = st;
GridView1.DataBind();
}
}
}
Function btnsearch_Click is processing the search, but the value you found is not displayed. You need to assign the value found to the data source of girdview.
Refer to the sample code(Customize the param object of the LoadData function to be compatible with the view):
protected void btnsearch_Click(object sender, EventArgs e)
{
var st = (from s in db.SearchTable2s where s.ID == int.Parse(TxtID.Text) select s).First();
TxtName.Text = st.Name;
TxtUsername.Text = st.Username;
LoadData(st);
}
void LoadData(object param)
{
GridView1.DataSource = param;
GridView1.DataBind();
}

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.

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

System.FormatException: Input string was not in a correct format

In the following code, when I try to add an Item to an ASP DropDownList, System.FormatException: Input string was not in a correct format, is thrown.
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;
public partial class ScheduleExam : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String connection = System.Configuration.ConfigurationManager.ConnectionStrings["TYCConnection"].ConnectionString;
String branch = Request.Form["ctl00$ctl00$MainContent$AdminMainContent$BranchDropDownList"];
if (!String.IsNullOrWhiteSpace(branch))
{
if (!branch.Equals("00"))
{
SqlConnection sqlConn = new SqlConnection(connection);
String semQuery = "select totalSem from branchTable where branchId='" + branch + "'";
SqlCommand semCommand = new SqlCommand(semQuery, sqlConn);
sqlConn.Open();
SqlDataReader semReader = semCommand.ExecuteReader();
semReader.Read();
int totalSem = Int32.Parse(semReader["totalSem"].ToString());
SemesterDropDownList.Items.Clear();
SemesterDropDownList.Enabled = true;
//ListItem list = new ListItem("Select");
SemesterDropDownList.Items.Add(new ListItem("select"));
for (int sem = 1; sem <= totalSem; sem++)
{
//SemesterDropDownList.Items.Add(sem.ToString());
}
sqlConn.Close();
}
else
{
SemesterDropDownList.Items.Clear();
SemesterDropDownList.Enabled = false;
//SemesterDropDownList.Items.Add("First Select Branch");
}
}
else
{
SemesterDropDownList.Enabled = false;
//SemesterDropDownList.Items.Add("First Select Branch");
}
}
protected void RegisterButton_Click(object sender, EventArgs e)
{
}
}
However, when I comment all such lines there is no exception thrown.
What could be the problem and its possible solution?
Instead of
int totalSem = Int32.Parse(semReader["totalSem"].ToString());
try
int totalSem;
Int32.TryParse(semReader["totalSem"].ToString(),totalSem);
and if that takes care of the exception then look into addressing the issues with that field.

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)

Resources