Monday, March 12, 2012

How to handle multiple UserControls that manipulate the same underlying data?

How should I handle the sitaution where two or more UserControls on the same page are using ObjectDataSources that leverage the same business object?

For example, UserControlA and UserControlB are both using BusinessObjectA via their respective ObjectDataSource. However, there does not appear to be a guarentee that UserControlA's events (i.e., DELETing data) fire before UserControlB's events (i.e., SELECTing data.) The result is that UserControlB can (and does) display incorrect data for the page, as it performed its SELECT before UserControlA performed its DELETE.

The situation that I find myself in at the moment is a page made up of GenericWebParts via dragging these UserControls into a WebPartZone. The controls are designed to be independant of one another, hence the reason why they each have their own ObjectDataSource. The problem is arising just as described above ... when I delete data via one UserControl, another UserControl on the page that makes decisions based upon the same data does not see the changes until a forced page refresh.

I did a little digging into this tonight and sure enough, UserControlB is being loaded before the events for UserControlA are firing, resulting in UserControlB read data before UserControlA has a chance to delete it.

I'm able to hack the solution by performing the delete opeartion from another page. So instead of enabling deletes via UserControlA's GridView, I've added a Hyperlink Field to ControlA's GridView which calls a Delete.aspx and then redirects back to the default page. However, this does not feel very clean at all.

Labels: , , , , , , , , , , ,

How to handle exception?

Hi,

I have a asp.net 2.0 page which uses objdatasource of which the business object is a TableAdapter object. In the page, all database related operation is done by GridView. Issues come out that when user perform an illegal database insert or update (such as inserting a null value), the exception is thrown out to the page, which is unfriendly.

How can program to catch exception and show friendly in the page?

try

'your code

catch objExeption as Exception (or as )

'the code in case of an exception

'for example response.redirect("friendly page")

end try

hope it helps

Labels: , , , , , , , , , , ,

How to Handle Error Message: "Login failed for user (null). Reason: Not associated with a

I'm running WinXP and using "Visual Studio 2005". To make a long story short, my "Default.aspx" page is using a GridView to display data which is being read from a Windows 2000 DB server. When I run the app under the IDE all works fine.

I copied the 1 file and put it into my folder as, "C:\Inetpub\wwwroot\default.aspx". When it loads I receive the error message, "Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection."

I'm frustrated with Studio Express 05 and will not bother with it again until Microsoft fixes this deployment issue. Can any one PLEASE help me???As you surely understand, this is a question of Access Premissions. When you write "Windows 2000 DB server", do you actually mean SQL 2000 Server? Assigning permissions correctly to the '<machinename>\ASPNET' account can be a bit difficult to grasp sometimes. It would help if you described your exact setup (what is installed on which machine). Example:
- Asp.Net 2.0 version 2.xxxxxxxxxx installed on machine YYYY running OS Windows XP Pro with 'IIS X.X'
- SQL Server version XXXX running on machine ZZZZ running OS QQQQ.

swep wrote:

I'm frustrated with Studio Express 05 and will not bother with it again until Microsoft fixes this deployment issue. Can any one PLEASE help me???


Your error SCREAMS "bad/improper connection string", so i wouldn't be so quick to blame a "deployment issue"
Application created on (my machine):
- Asp.Net 2.0 version 2.0.50215.44 running OS Windows XP Pro SP 2 with 'IIS 5.1'

Accessing database on:
- SQL Server 2000 Enterprise Edition running OS Windows Server 2000.

I'm really stuck...Any help you can offer would be greatly appreciated.
You must put in a valid connection string, because that is what the error is telling you
http://www.connectionstrings.com

I'm really lost right now and need some expect advice. You mentioned in the ASP.NET forum,

"You must put in a valid connection string, because that is what the error is telling you"

If my connection string is wrong then why can I connect to the db server within Visual Web 2005 just fine? My web.config file contains my connection as,

add name="WebConnectionString" connectionString="Data Source=NAME OF SQL SERVER;Initial Catalog=NAME OF TABLE;"

Could it be that I created this in .net framework 2.0 and it is querying a database in the .net framework 1.1?

hello.

maybe the problem is related with the fact that the asp.net user hasn't enough permissions to access the database (you might not have faced this problem because when you use the internal server it'll use the credentials of the active user to process the request - this does not happen when you use IIS: in this case, i't use a specific account for accessing the db).

Labels: , , , , , , , , , , , , , , , , , , ,

How to handle Checkbox in itemtemplate and with the button in gridview

Hi All

I m having a gridview,button control on a form. In thatPatientId is a templatefiled along with a check box.Per page i m goingto display 10 records of a same patient.

My requirement is

1.user has to select only one check box if user selects more than one check box alert or message should be displayed.

2. If user checks only one checkbox i have to pick the particual row. I want to store all the values.

How to handle it.

I m able to get the values with checkbox, but i m unable with the button .

Here is my aspx page code

<asp:GridView ID="Prescgrid" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None"
BorderWidth="1px" CellPadding="4" Height="49px">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="Refill"
Text="Refill"></asp:LinkButton>

</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Patient_id" SortExpression="patient_id">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Bind("patient_id") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("patient_id") %>'></asp:Label>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack=true OnCheckedChanged="Hai" />
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="prescription_date" DataFormatString="{0:d}" HeaderText="Prescription Date"
HtmlEncode="False" SortExpression="prescription_date">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="home_phone" HeaderText="Patient Home Phone" SortExpression="home_phone">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="pharmacy_phone" HeaderText="Pharmacy Phone" SortExpression="pharmacy_phone">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="medication1" HeaderText="Medication" SortExpression="medication1">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="dosage1" HeaderText="Dosage" SortExpression="dosage1">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="frequency1" HeaderText="Frequency" SortExpression="frequency1">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="refills" HeaderText="Number Of Refills" SortExpression="refills">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="amt_tablets" HeaderText="Number Of Tablets" SortExpression="amt_tablets">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="staff_comments" HeaderText="Staff Comments" SortExpression="staff_comments">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
nbsp;

<asp:Button ID="Button1" runat="server" Text="Button" />

Waiting for valuable replies

Thank u

Baba

Hi Baba,

Now you want to capture the GridViewRow on which the CheckBox is selected on ButtonClick, right?

Try this.

<asp:GridViewID="Prescgrid"runat="server"AllowPaging="True"AllowSorting="True"

AutoGenerateColumns="False"BackColor="White"BorderColor="#3366CC"BorderStyle="None"

BorderWidth="1px"CellPadding="4"Height="49px">

<FooterStyleBackColor="#99CCCC"ForeColor="#003399"/>

<Columns>

<asp:TemplateFieldShowHeader="False">

<ItemTemplate>

<asp:LinkButtonID="LinkButton1"runat="server"CausesValidation="false"CommandName="Refill"

Text="Refill"></asp:LinkButton>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateFieldHeaderText="Patient_id"SortExpression="patient_id">

<EditItemTemplate>

<asp:TextBoxID="TextBox1"runat="server"Text='<%# Bind("patient_id") %>'></asp:TextBox>

</EditItemTemplate>

<HeaderStyleHorizontalAlign="Left"Wrap="False"/>

<ItemTemplate>

<asp:LabelID="Label1"runat="server"Text='<%# Bind("patient_id") %>'></asp:Label>

<asp:CheckBoxID="CheckBox2"runat="server"AutoPostBack="true"OnCheckedChanged="CheckBox2_CheckedChanged"/>

</ItemTemplate>

</asp:TemplateField>

<asp:BoundFieldDataField="prescription_date"DataFormatString="{0:d}"HeaderText="Prescription Date"

HtmlEncode="False"SortExpression="prescription_date">

<HeaderStyleHorizontalAlign="Left"Wrap="False"/>

</asp:BoundField>

<asp:BoundFieldDataField="home_phone"HeaderText="Patient Home Phone"SortExpression="home_phone">

<HeaderStyleHorizontalAlign="Left"Wrap="False"/>

</asp:BoundField>

<asp:BoundFieldDataField="pharmacy_phone"HeaderText="Pharmacy Phone"SortExpression="pharmacy_phone">

<HeaderStyleHorizontalAlign="Left"Wrap="False"/>

</asp:BoundField>

<asp:BoundFieldDataField="medication1"HeaderText="Medication"SortExpression="medication1">

<HeaderStyleHorizontalAlign="Left"Wrap="False"/>

</asp:BoundField>

<asp:BoundFieldDataField="dosage1"HeaderText="Dosage"SortExpression="dosage1">

<HeaderStyleHorizontalAlign="Left"Wrap="False"/>

</asp:BoundField>

<asp:BoundFieldDataField="frequency1"HeaderText="Frequency"SortExpression="frequency1">

<HeaderStyleHorizontalAlign="Left"Wrap="False"/>

</asp:BoundField>

<asp:BoundFieldDataField="refills"HeaderText="Number Of Refills"SortExpression="refills">

<HeaderStyleHorizontalAlign="Left"/>

</asp:BoundField>

<asp:BoundFieldDataField="amt_tablets"HeaderText="Number Of Tablets"SortExpression="amt_tablets">

<HeaderStyleHorizontalAlign="Left"/>

</asp:BoundField>

<asp:BoundFieldDataField="staff_comments"HeaderText="Staff Comments"SortExpression="staff_comments">

<HeaderStyleHorizontalAlign="Left"/>

</asp:BoundField>

</Columns>

<RowStyleBackColor="White"ForeColor="#003399"/>

<SelectedRowStyleBackColor="#009999"Font-Bold="True"ForeColor="#CCFF99"/>

<PagerStyleBackColor="#99CCCC"ForeColor="#003399"HorizontalAlign="Left"/>

<HeaderStyleBackColor="#003399"Font-Bold="True"ForeColor="#CCCCFF"/>

</asp:GridView>

<asp:ButtonID="Button1"runat="server"Text="Button"OnClick="Button1_Click"/>

protectedvoid CheckBox2_CheckedChanged(object sender,EventArgs e)

{

string cScript ="<script>alert('Another CheckBox already checked');</script>";

CheckBox chkCheckBox2 = (CheckBox)sender;

GridViewRow mySelectedRow = ((CheckBox)sender).Parent.ParentasGridViewRow;if(chkCheckBox2.Checked)

{

foreach (GridViewRow gvrin Prescgrid.Rows)

{

if(gvr!=mySelectedRow)

{

if(((CheckBox)gvr.FindControl("CheckBox2")).Checked)

{

ClientScript.RegisterStartupScript(typeof(Page),"clientscript",cScript);

chkCheckBox2.Checked =false;

}

}

}

}

}

protectedvoid Button1_Click(object sender,EventArgs e)

{

string cScript ="<script>alert('First Select a Row');</script>";

bool blnFlag =false;

foreach (GridViewRow gvrin Prescgrid.Rows)

{

if (((CheckBox)gvr.FindControl("CheckBox2")).Checked)

{

//access all elements using myRow.FindControl blah blah

string strPatientID = ((Label)gvr.FindControl("Label1")).Text;blnFlag =true;

}

}

if (!blnFlag)

{

ClientScript.RegisterStartupScript(typeof(Page),"clientscript", cScript);

}

}

Regards,

Naveen

Labels: , , , , , , , , , , , , , , , ,

How to Handle Check box with Button Control

Hi All

I m having a gridview,button control on a form. In that PatientId is a templatefiled along with a check box.Per page i m going to display 10 records of a same patient.

My requirement is

1.user has to select only one check box if user selects more than one check box alert or message should be displayed.

2. If user checks only one checkbox i have to pick the particual row. I want to store all the values.

How to handle it.

I m able to get the values with checkbox, but i m unable with the button .

Here is my aspx page code

<asp:GridView ID="Prescgrid" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None"
BorderWidth="1px" CellPadding="4" Height="49px">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="Refill"
Text="Refill"></asp:LinkButton>

</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Patient_id" SortExpression="patient_id">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("patient_id") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("patient_id") %>'></asp:Label>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack=true OnCheckedChanged="Hai" />
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="prescription_date" DataFormatString="{0:d}" HeaderText="Prescription Date"
HtmlEncode="False" SortExpression="prescription_date">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="home_phone" HeaderText="Patient Home Phone" SortExpression="home_phone">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="pharmacy_phone" HeaderText="Pharmacy Phone" SortExpression="pharmacy_phone">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="medication1" HeaderText="Medication" SortExpression="medication1">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="dosage1" HeaderText="Dosage" SortExpression="dosage1">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="frequency1" HeaderText="Frequency" SortExpression="frequency1">
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="refills" HeaderText="Number Of Refills" SortExpression="refills">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="amt_tablets" HeaderText="Number Of Tablets" SortExpression="amt_tablets">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="staff_comments" HeaderText="Staff Comments" SortExpression="staff_comments">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
nbsp;

<asp:Button ID="Button1" runat="server" Text="Button" />

Waiting for valuable replies

Thank u

Baba

You may do better with a DataGrid if you are allowing multiple selections.

http://www.dotnetjohn.com/articles.aspx?articleid=83

Alternately, set up an array as a session variable and populate it with the DataKey for each row on the checkbox_checked event. You should be able to get that out of the event arguments. Then when you do whatever you do, iterate throught the array and pass those datakeys to the SQL statement or whatever you are using as a criteria class.


Hi Baba,

Try this.

http://forums.asp.net/t/1185169.aspx

Regards,

Naveen


Hisrijaya_ramaraju ,

srijaya_ramaraju@.yahoo.com:

1.user has to select only one check box if user selects more than one check box alert or message should be displayed.

You can use javascript to allow user to select only one check box.

srijaya_ramaraju@.yahoo.com:

2. If user checks only one checkbox i have to pick the particual row. I want to store all the values

You can use findcontrol method to get any controls in the row then retrieve it's value.

Have a look at this sample ,

1<div>2 <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">3 <Columns>4 <asp:TemplateField>5 <ItemTemplate>6 <asp:CheckBox ID="CheckBox1" runat="server" />7 </ItemTemplate>8 </asp:TemplateField>9 </Columns>10 </asp:GridView>1112 </div>13 <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

1function check(self)2 {34 var items = document.getElementsByTagName('input');56for(var i =0;i7 {8if(items(i).type =='checkbox' && items(i).id != self.id && items(i).checked ==true)9 items(i).checked =false;10 }11121314 }

1protected void Page_Load(object sender, EventArgs e)2 {3if (!IsPostBack)4 {5 DataTable table =new DataTable();6 table.Columns.Add("age");7 table.Columns.Add("name");8 DataRow dr = table.NewRow();9 dr["age"] = 1;10 dr["name"] ="zz";11 table.Rows.Add(dr);1213 DataRow dr2 = table.NewRow();14 dr2["age"] = 2;15 dr2["name"] ="vv";16 table.Rows.Add(dr2);1718this.GridView1.DataSource = table;19 GridView1.DataBind();20 }212223//this.Button1.Attributes.Add("onclick","check(); return false;");24 }2526protected void Button1_Click(object sender, EventArgs e)27 {28for (int i = 0; i < GridView1.Rows.Count; i++)29 {30 CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");31if (chk.Checked ==true)32 {33string name = GridView1.Rows[i].Cells[2].Text;//get value here34 }35 }36 }3738protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)39 {40if (e.Row.RowType == DataControlRowType.DataRow)41 {42 CheckBox chk = (CheckBox)e.Row.FindControl("CheckBox1");43 chk.Attributes.Add("onclick","check(this)");44 }45 }

Labels: , , , , , , , , , , , , ,