PetaPoco orm tool with Asp.Net GridView - asp.net

I am trying PetaPoco with my Asp.Net Project. How can I use PetaPoco with Asp.Net GridView. I am new at web programming.
I tried all code samples in the blog. They all are working with console app. But in Asp.Net I could not bind the datasource to GridView.
Thank you
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 PetaPoco;
using northwind;
using System.Dynamic;
namespace Mng
{
public partial class Default : System.Web.UI.Page
{
public northwindDB db;
protected void Page_Load(object sender, EventArgs e)
{
db = new northwindDB();
if (!Page.IsPostBack)
{
var q = db.Query<Product>("SELECT top 10 * FROM Products");
grdMng.DataSource = q;
}
else
{
Response.Write("Postback occurs");
}
}
}
}

Call DataBind method to build the grid
grdMng.DataSource = q;
grdMng.DataBind();

Related

How to add connection string dynamically in web.config

I have to create one application which will add connection string in web.config when user clicks on some control.
actually it runs fine,
But now together with it i have to run one SQL script which will create new database.
for that i have write following code..
now it gives me error as
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.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Web.Configuration;
using System.Collections.Generic;
public partial class connectionString : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnadd_Click(object sender, EventArgs e)
{
try
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
// System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
var section = config.GetSection("connectionStrings") as ConnectionStringsSection;
section.ConnectionStrings.Add(new ConnectionStringSettings(txtNAme.Text, txtNAme.Text, "System.Data.SqlClient"));
config.Save();
string sqlConnectionString = #"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ccwebgrity;Data Source=SURAJIT\SQLEXPRESS";
FileInfo file = new FileInfo(#"G:\AAA\SCRIPT.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
file.OpenText().Close();
}
catch (Exception ex)
{
}
}
}
Define the connection string in web.config
<ConnectionStrings>
<add name="student" connectionString="Server=student;Max Pool Size=300;Initial Catalog=studentDB;User ID=student;Password=st123dent;" providerName="System.Data.SqlClient"/>
</Connectionstrings>
Configuration is read only so you can not do it in obvious way like
ConfigurationManager.ConnectionStrings["student"].ConnectionString = "new value";

Label not displaying the dropdownlist selected value

VS is giving error at this line:
Label1.Text = DropDownList1.SelectedItem.Text;
Error message says that object reference not set to an instance of the object. what could be the solution?
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;
namespace Login
{
public partial class ViewCourses : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string loginID = Convert.ToString(Session[0]);
DropDownList1.SelectedValue = loginID;
Label1.Text = DropDownList1.SelectedItem.Text;
}
}
}
I am guessing that DropDownList1.SelectedItem is null.
Put a breakpoint on that line and see what DropDownList1.SelectedItem's value is.
Are you familiar with the VS debugger and breakpoints?

C# Calendar Tool ASP.NET

So I'm trying to create a calendar tool that pulls in dates from an SQL Database.
I've created a TableAdapter which is here: http://bit.ly/KRaTvr
This is the small bit of ASP I have to create my calendar:
<asp:Calendar ID="calEvents" runat="server"> </asp:Calendar>
Then I have the C# in my project used to pull the information through.
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 BreakyBottomTableAdapters;
public partial class Events : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Data Table
BreakyBottom table;
//Table Adapter
BreakyBottomTableAdapters.ScheduleDate ta;
ta = new BreakyBottomTableAdapters.ScheduleDate();
//Populate the table using the Table Adapter passing current date
table = ta.GetData(e.Day.Date);
//Check the number of rows in the data table
if (table.Rows.Count > 0)
{
//Change the background colour to gray
e.Cell.BackColor = System.Drawing.Color.Gray;
//Allow the Day to be selected
e.Day.IsSelectable = true;
}
}
}
Now I know I'm missing something but I can't for the life of my figure out what it is.
Here is a screenshot of the compile error I get: http://bit.ly/ISuVsT - I know I'm probably missing something insanely obvious but any assistance would be appreciated.
Best Regards
The compiler is telling you that EventArgs does not have any member called Day which you are apparently using in your code incorrectly:
protected void Page_Load(object sender, EventArgs e)
{
....
table = ta.GetData(e.Day.Date); //WRONG: Day is not a member of EventArgs
}
If the idea is to use the Current date, as you mentioned, then do this:
table = ta.GetData(DateTime.Now);

Can't override preload because not function?

Another .net newbie question
I created a class that inherits System.Web.UI.Page with the intention of overriding the preload and Unload functions. But Visual Studios complains I can't override because "System.Web.UI.Page.PreLoad" is not a function. What's wrong with my code? Here it is:
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 BTPage : System.Web.UI.Page
{
protected SqlConnection cnx;
public override void PreLoad(object sender, EventArgs e)
{
cnx = new SqlConnection(ConfigurationManager.AppSettings["btdatabase"]);
cnx.Open();
}
protected void Unload(object sender, EventArgs e)
{
cnx.Close();
}
}
If there's anything else alarming about my code, please tell me. I'm new to .Net and I'm not sure if I'm doing things in the ".NET way".
What I also want to do is have every web page inherit from BTPage so that they all open a connection to the database on preload.
PreLoad is not a method of the Page type. You need to override OnPreLoad

Dropdownlist is not allowing new items added dynamically

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;
public partial class Expt : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Bttnadd_Click(object sender, EventArgs e)
{
DropDownList11.Items.Add(ListBox1.SelectedItem);
ListBox1.Items.Remove(ListBox1.SelectedItem);
}
}
In this when i add the item in dropdown list it shows an error:"Dropdown list does not allow multiple seelection"
But when i used to print the selected item It shows null exception error.
Try to add new list items like that :
DropDownList11.Items.Add(
new ListItem(ListBox1.SelectedItem.Text, ListBox1.SelectedItem.Value)
);
Because you're adding the ListItem that selected by the ListBox, it's Selected property is true. Then you get this exception.

Resources