Need a little help on ASP.NET 2.0

iDenTiTy

Expert Member
Joined
Apr 14, 2007
Messages
3,899
Reaction score
2
Location
Fourways (vicinity)
Hi Guys,

First off, I have hardly has ANY ASP.NET dev behind me in 2 years (in fact I'd say none).
But I have hit a wall with a dropdownlist custom validation..

Firstly, I don't want to use the visual pane, I would prefer to work in the code..

You see, I have a gridview with 3 columns. The first column is a dropdownlist (for when you add to the database), the rest has standard numerictextbox controls.

My dropdownlist has a VERY simple datasource select statement. The insert is as well.

However, my problem comes in with the VB.NET (*.aspx.vb) part.

How do I bind the alidation routine to the control, so that the item I select with the dropdownlist, cannot be inserted into the database when I click the 'add' button?

I am busy writing the code I think should work, but it just won't work.
As soon as I can get my files from my other pc, I will show the progress I have made.

Jeesh, this ASP.NET + VB.NET is so alien to me, it's not funny.

:(
 
If the table already contains the value contained in the dropdown (which itself get's the value from another table).

So the items need to be unique on the page? Perhaps tell me the real life problem and I may understand better. But you have two options, javascript which is client side or C#/VB which will be server side.

For server side, add one custom validator to your page. Subscribe to the server validation event, that will add an event handler to your code behind. Tip: Forget VB, go C# since you are just starting out.

protected void uniqueValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
List<string> allValues = new List<string>();

for (int i = 0; i < grdTest.Rows.Count; i++)
{
if (grdTest.Rows.RowType == DataControlRowType.DataRow)
{
DropDownList ddlWhatever = grdTest.Rows.FindControl("ddlWhatever") as DropDownList;

if (allValues.Contains(ddlWhatever.SelectedValue))
{
args.IsValid = false;
return;
}
}
}

args.IsValid = true;
}
 
Last edited:
Lol, my other pc has no network card (onboard is broken), don't have a flash drive either.

I have created a sub (method?) I will post tomorrow, maybe you could spot something 'off'

:)
 
Okay, so here is my aspx.vb sub:

Protected Sub cvalDuplicate_ServerValidate(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
Dim row = DirectCast(DirectCast(sender, Control).NamingContainer, GridViewRow)
Dim sbNo = DirectCast(row.FindControl("txtBoxSensorBoxNo"), NumericTextBox).ValueAsInteger
Dim dlNo = Integer.Parse(e.Value)

Dim sql = "select count(*) from SensorBoxDef "
sql &= " where DataloggerNo = @DataloggerNo"
sql &= " and SensorBoxNo = @SensorBoxNo"

Dim pDataloggerNo As New SqlParameter("@DataloggerNo", SqlDbType.Int)
Dim pSensorBoxNo As New SqlParameter("@SensorBoxNo", SqlDbType.Int)
'pDataloggerNo.Value = dlNo
'pSensorBoxNo.Value = sbNo

Try
Dim count = CInt(SqlHelper.ExecuteScalar(sql, pDataloggerNo, pSensorBoxNo))
e.IsValid = (count = 0)
Catch ex As Exception
Throw New DALException("Error validating rows", ex, sql, pSensorBoxNo, pDataloggerNo)
End Try

End Sub

My aspx page:

<asp:ValidationSummary runat="server" ValidationGroup="dl_add"
CssClass="valsum" />


<asp:TemplateField HeaderText="DataloggerNo" SortExpression="DataloggerNo">
<ItemTemplate>
<%# Eval("DataloggerNo") %>
</ItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="ddDataloggerNo" runat="server" DataSourceID="dsDataloggerNos"
DataTextField="DataloggerNo" DataValueField="DataloggerNo">
</asp:DropDownList>
<asp:CustomValidator ID="cvalDuplicate" runat="server" Display="None" ControlToValidate="ddDataloggerNo"
ValidationGroup="dl_add" ErrorMessage="DataloggerNo and SensorBoxNo numbers must be unique"
OnServerValidate="cvalDuplicate_ServerValidate" />
</InsertItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>

Datasource:

<asp:SqlDataSource ID="dsDataloggerNos" runat="server" ConnectionString="<%$ ConnectionStrings:ShopwareSetup %>"
SelectCommand="SELECT DataloggerNo from dataloggerdef"></asp:SqlDataSource>


As I say,

Any help would be welcome..

:(
 
Last edited:
If you're concerned about inserting duplicate records, why not just do the check on the SQL side? If you have a stored procedure implementing the INSERT into the database, just do:

Code:
INSERT INTO Table(column1, column2, column3 ...)
SELECT value1, value2, value3 ... WHERE value1 NOT IN (SELECT value1 FROM Table)
 
If you're concerned about inserting duplicate records, why not just do the check on the SQL side? If you have a stored procedure implementing the INSERT into the database, just do:

Code:
INSERT INTO Table(column1, column2, column3 ...)
SELECT value1, value2, value3 ... WHERE value1 NOT IN (SELECT value1 FROM Table)

I disagree with this method. If Table has a decent number of rows, you INSERT time will sky rocket. Isn't this what unique indexes are for?

Sounds to me like the OP has a data validation problem, surely this mustn't be done in the data layer?
 
Firstly he should validate on the client side as suggested and secondly - always try to avoid the 'IN" statement. You can achieve the same with much greater effiency by using joins.
 
Thanks for the advice everyone !!!!
:)

In the end, what I did was do the validation at both the DAL and interface side.
It feels damn good when, after hours of toil, you find the solution.

:)
 
Code:
INSERT INTO Table(column1, column2, column3 ...)
SELECT value1, value2, value3 ... WHERE value1 NOT IN (SELECT value1 FROM Table)

I'm no SQL expert but wouldn't this maybe be faster
Code:
INSERT INTO Table(column1, column2, column3 ...)
SELECT value1, value2, value3 ... WHERE NOT EXISTS (SELECT value1 FROM Table WHERE value1 = theActualValue)
 
It would yes, seeing as no database reads would actually be performed when using NOT EXISTS, but I was merely mentioning the principle... :)

(I actually use WHERE NOT EXISTS in many cases...)
 
I figured it out. It's all the smileys that are messing with your code, dude! :D

Seriously though, glad you came right. No go sit in the corner and learn C#. ;)
 
Another tip: you shouldn't have your database logic in your code-behind file for your page. You should move your database access routines into a separate class and separate your data access layer from your presentation layer. Even better you should build a business object layer between your data access layer and your presentation layer with classes that represent your data entities. Best practice is to not work directly with data objects in the presentation layer, but to only work with your business objects in the presentation layer, which in turn have access to the data access layer.

One of the most compelling reasons for this is that it makes it a hell of a lot easier to reuse your code.
 
Top
Sign up to the MyBroadband newsletter
X