I have a Detailsview with multiple fields, always set to be in insert mode. Of those fields I wanted to check before the user inserted a new record to see if that user has already entered a record for that date.
I really needed to figure a way to check multiple requirements, like if the date requested is a holiday, or if there are already two different requests made from different users for the same date (because there cannot be more than two people requesting the same date.)
I could not get the custom validator to work because the only time I have postback is when insert is pressed.
I was wondering if there was any way to check different validations when insert is pressed but only follow through with the insert if it all passed?
I would need to check more than one column in the table. The userid and the date.
I hope that made sense.
asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="ObjectDataSource1" DataKeyNames="bwrequestid" Height="29px" Width="928px" AutoGenerateRows="False" CellPadding="4" ForeColor="#333333" GridLines="None" Style="margin-right: 0px; text-align: left; margin-top: 0px;" ondatabound="DetailsView1_DataBound" oniteminserting="EntValid_ItemInserting"
protected void EntValid_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
for (int i = 0; i < e.Values.Count; i++)
{
if (e.Values[i] != GetData())
{
e.Cancel = true;
return;
}
}
}
-------the get data looks like this
private DataSet GetData() { ConnectionStringSettingsCollection cssc = ConfigurationManager.ConnectionStrings;
var sql = "SELECT LEAVETYPE, LEAVECODE FROM TEST.LVTYPE ORDER BY LEAVECODE";
using (iDB2Connection conn = new iDB2Connection(GetConnectionString()))
{
conn.Open();
using (iDB2Command cmd = new iDB2Command(sql, conn))
{
cmd.DeriveParameters();
using (iDB2DataAdapter da = new iDB2DataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}
}
protected void EntValid_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
for (int i = 0; i < e.Values.Count; i++)
{
if (e.Values[i] != GetData())
{
e.Cancel = true;
return;
}
}
}
-------the get data looks like this
private DataSet GetData() { ConnectionStringSettingsCollection cssc = ConfigurationManager.ConnectionStrings;
var sql = "SELECT LEAVETYPE, LEAVECODE FROM TEST.LVTYPE ORDER BY LEAVECODE";
using (iDB2Connection conn = new iDB2Connection(GetConnectionString()))
{
conn.Open();
using (iDB2Command cmd = new iDB2Command(sql, conn))
{
cmd.DeriveParameters();
using (iDB2DataAdapter da = new iDB2DataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}
bool
. So in your submit event you could call something likeif(myValidatefunction(theData)) { submitTheFormHere } else { showMessageAndStartOver }
– aserwin