DataBind Problem... Help!!!

T

Tai

I'm new at this so please bear with me. I have two webforms, for
example purposes I'll say a.aspx and b.aspx... a.aspx has a datagrid
populated from MS Access with all the records, including contactID. On
b.aspx, I want to populate a datagrid with only one record, based on
the selected row from a.aspx... I can see the contactID in the
HTTP://dadadadada?cid=115 (or whatever row is chosen) but when b.aspx
is loaded, I see nothing. I know I have to assign a parameter for my
BindData method, but how? My code is below. Any help would be great.
Thanks...

Tai

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.OleDb;
using System.Reflection;

namespace Contact
{
/// <summary>
/// Summary description for contact_details.
/// </summary>
public class contact_details : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Image Image2;
protected System.Web.UI.WebControls.Image Image1;
protected System.Web.UI.WebControls.Label Lbl_header;
protected System.Web.UI.WebControls.Literal ContactIDLtl;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label1;


public string ContactID
{
get
{
return ContactIDLtl.Text;
}
}

private void SetContactID()
{
string cid = Request.QueryString["cid"];
ContactIDLtl.Text = cid;
Label1.Text = cid;
}



private void Page_Load(object sender, System.EventArgs e)

{
// Put user code to initialize the page here
if (!IsPostBack)
{
SetContactID();
BindData();
}
}

private void BindData()
{
string sConnString = ConfigurationSettings.AppSettings["Contact"];
OleDbConnection conn = new OleDbConnection(sConnString);
string sSql = "SELECT Contacts.ContactID, Contacts.FirstName,
Contacts.LastName, Contacts.DateApplied, Contacts.DateAv FROM Contacts
WHERE (((Contacts.ContactID)=[?]))";

Cmd.Parameters.Add("ContactID",
System.Data.SqlDbType.VarChar).Value = ContactID;
OleDbCommand ContactsCmd = new OleDbCommand(sSql, conn);
conn.Open();
try
{
DataGrid1.DataSource = ContactsCmd.ExecuteReader();
DataGrid1.DataBind();

}
finally
{
conn.Close();

}

}
 
I

Ignacio Machin

Hi Tai,


If you are going to show only a row in b.aspx then there is no need to use a
datagrid or databinding , use simples labels or texboxes if you are going to
make changes.

basically what you do is :

string ContactName;
page_load( ... )
{
if ( !IsPostBack )
{
// First check if the param exist
if ( Request["cID"] !="" )
{
LoadData();
label1.Text = ContactName;
.....
}
}
}

LoadData()
{

// the same code you have to load the data
OleDbDataReader reader = command.ExecuteReader()
reader.Read();
ContactName = reader[0].ToString();
..
}


Beware!! the code above was not tested !!!

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Tai said:
I'm new at this so please bear with me. I have two webforms, for
example purposes I'll say a.aspx and b.aspx... a.aspx has a datagrid
populated from MS Access with all the records, including contactID. On
b.aspx, I want to populate a datagrid with only one record, based on
the selected row from a.aspx... I can see the contactID in the
HTTP://dadadadada?cid=115 (or whatever row is chosen) but when b.aspx
is loaded, I see nothing. I know I have to assign a parameter for my
BindData method, but how? My code is below. Any help would be great.
Thanks...

Tai

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.OleDb;
using System.Reflection;

namespace Contact
{
/// <summary>
/// Summary description for contact_details.
/// </summary>
public class contact_details : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Image Image2;
protected System.Web.UI.WebControls.Image Image1;
protected System.Web.UI.WebControls.Label Lbl_header;
protected System.Web.UI.WebControls.Literal ContactIDLtl;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label1;


public string ContactID
{
get
{
return ContactIDLtl.Text;
}
}

private void SetContactID()
{
string cid = Request.QueryString["cid"];
ContactIDLtl.Text = cid;
Label1.Text = cid;
}



private void Page_Load(object sender, System.EventArgs e)

{
// Put user code to initialize the page here
if (!IsPostBack)
{
SetContactID();
BindData();
}
}

private void BindData()
{
string sConnString = ConfigurationSettings.AppSettings["Contact"];
OleDbConnection conn = new OleDbConnection(sConnString);
string sSql = "SELECT Contacts.ContactID, Contacts.FirstName,
Contacts.LastName, Contacts.DateApplied, Contacts.DateAv FROM Contacts
WHERE (((Contacts.ContactID)=[?]))";

Cmd.Parameters.Add("ContactID",
System.Data.SqlDbType.VarChar).Value = ContactID;
OleDbCommand ContactsCmd = new OleDbCommand(sSql, conn);
conn.Open();
try
{
DataGrid1.DataSource = ContactsCmd.ExecuteReader();
DataGrid1.DataBind();

}
finally
{
conn.Close();

}

}
 
T

Tai Ferguson

Thanks...

It's still not loading... I have been trying to get the data to bind to
a label. My current error is "An OleDbParameter with ParameterName
'FirstName' is not contained by this OleDbParameterCollection." is it
possible to send me a working sample? heres my code for page a and b...

PAGE A (Works Fine...)

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.OleDb;

namespace BSE
{
/// <summary>
/// Summary description for commview.
/// </summary>
public class commview : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DG_contactinfo;

private void Page_Load(object sender, System.EventArgs e)
{

if (!IsPostBack)
{
BindData();
}
}

private void BindData()
{
string sConnString = ConfigurationSettings.AppSettings["BSE"];
OleDbConnection conn = new OleDbConnection(sConnString);
string sSql = "SELECT Contacts.ContactID, Contacts.LastName,
Contacts.FirstName, Contacts.City, Contacts.State, Contacts.HomePhone,
Contacts.DateApplied, Contacts.DateAv FROM Contacts ORDER BY LastName";


OleDbCommand ContactsCmd = new OleDbCommand(sSql, conn);
conn.Open();
try
{
DG_contactinfo.DataSource = ContactsCmd.ExecuteReader();
DG_contactinfo.DataBind();

}
finally
{
conn.Close();

}

}


// Put user code to initialize the page here


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>


private void InitializeComponent()
{
this.DG_contactinfo.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DG_contactinf
o_ItemCommand);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion



private void DG_contactinfo_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string ContactID = e.Item.Cells[1].Text;

if (e.CommandName == "Select")
{

// Add code here to use the ContactID item as the criteria for the
next page posted.
Server.Transfer("commdetails.aspx?ContactID="+ ContactID);
}
}
}
}

PAGE B (ERROR BEFORE LOADS)

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.OleDb;
using System.Reflection;
using System.Data.OleDb.OleDbParameterCollection;



namespace BSE
{
/// <summary>
/// Summary description for commdetails.
/// </summary>
public class commdetails : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Literal ContactIDLtl;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.Label Label1;


public string ContactID
{
get
{
return ContactIDLtl.Text;
}
}

private void SetContactID()
{
string ContactID = Request.QueryString["ContactID"];
ContactIDLtl.Text = ContactID;
Label1.Text = ContactID;
}

string FirstName;
string LastName;
string DateApplied;
string DateAv;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack)
{
if ( Request["ContactID"] !="" )
{
BindData();
Label2.Text = FirstName;
Label3.Text = LastName;
Label4.Text = DateApplied;
Label5.Text = DateAv;
}
SetContactID();
BindData();
}
}
private void BindData()
{
string sConnString = ConfigurationSettings.AppSettings["BSE"];
OleDbConnection conn = new OleDbConnection(sConnString);
string sSql = "SELECT Contacts.FirstName, Contacts.LastName,
Contacts.DateApplied, Contacts.DateAv FROM Contacts WHERE
(((Contacts.ContactID)=[ContactID]))";

OleDbCommand Cmd = new OleDbCommand(sSql, conn);

OleDbCommand ContactsCmd = new OleDbCommand(sSql, conn);
ContactsCmd.Parameters["@Contacts.FirstName"].Value = FirstName;
ContactsCmd.Parameters["@Contacts.LastName"].Value = LastName;
ContactsCmd.Parameters["@Contacts.DateApplied"].Value = DateApplied;
ContactsCmd.Parameters["@Contacts.DateAv"].Value = DateAv;



conn.Open();
try
{
OleDbDataReader reader = ContactsCmd.ExecuteReader();
reader.Read();
DataBind();
FirstName = reader[0].ToString();
LastName = reader[1].ToString();
DateApplied = reader[2].ToString();
DateAv = reader[3].ToString();

}
finally
{
conn.Close();

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top