// Add product data to DB.
AddProducts products = new AddProducts();
bool addSuccess = products.AddProduct(AddProductName.Text, AddProductDescription.Text, AddSubHeading1.Text, AddSubInfo1.Text, AddSubHeading2.Text, AddSubInfo2.Text, AddSubHeading3.Text, AddSubInfo3.Text, AddSubHeading4.Text, AddSubInfo4.Text, AddSubHeading5.Text, AddSubInfo5.Text, DropDownAddCategory.SelectedValue, ProductImage.FileName);
if (addSuccess)
{
// Reload the page.
string pageUrl = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count());
Response.Redirect(pageUrl + "?ProductAction=add");
}
else
{
LabelAddStatus.Text = "Unable to add new product to database.";
}
}
else
{
LabelAddStatus.Text = "Unable to accept file type.";
}
I am creating a website for my employer that has an admin page that allows the user to add products, I have tried a few methods now but I have not been able to enter empty fields into the database through this page. Can anyone help?
Can check whether the string is null and give a ' ' value using
AddProductName?.Text?? " " //in C#6.0
or you can use
if(string.IsNullOrEmpty(AddproductName.Text))
{
AddproductName.Text=" ";
}
and then try inserting into the database
if it is null, also check if the database is allowed to take empty or null values.
Related
I am trying to figure out a way for my program not to update the sql database if some fields are blank when I hit the submit form. Right now when I submit it to the sql database, if the fields are blank, it updates it as blank. Is there a way for my code not to behave like this?
Thanks
//field names in the table
string update = #"UPDATE Master_List
SET Date_Complete1 = #Date_Complete1, Pass_Fail = #Pass_Fail, CRC_Number = #CRC_Number, QN_Number = #QN_Number, Notes = #Notes WHERE Job_Number = #Job_Number"; //parameter names
using (SqlConnection conn = new SqlConnection(connString)) //using allows disposing of low level resources
{
try
{
conn.Open();//open new connection
command = new SqlCommand(update, conn); // create the new sql command object
// Read value from form and save to the table
command.Parameters.AddWithValue(#"Job_Number", jobTxt.Text);
command.Parameters.AddWithValue(#"Pass_Fail", comboBox1.Text);
command.Parameters.AddWithValue(#"Date_Complete1", opBox1.Text);
command.Parameters.AddWithValue(#"CRC_Number", crcTxt.Text);
command.Parameters.AddWithValue(#"QN_Number", qnTxt.Text);
command.Parameters.AddWithValue(#"Notes", notesTxt.Text);
command.ExecuteNonQuery(); // Push form into the table
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); // If there is something wrong, show the user message
}
}
Assuming that you want to update some of the fields if one or more fields are blank, then you can do this:
UPDATE Master_List
SET Date_Complete1 = ISNULL(NULLIF(#Date_Complete1,''),Date_Complete1),
Pass_Fail = ISNULL(NULLIF(#Pass_Fail,''),Pass_Fail),
CRC_Number = ISNULL(NULLIF(#CRC_Number,''),CRC_Number),
QN_Number = ISNULL(NULLIF(#QN_Number,''),QN_Number),
Notes = ISNULL(NULLIF(#Notes,''),Notes)
WHERE Job_Number = #Job_Number
If you don't want any fields to update if any fields are blank, then just check them in an if statement.
Perform checks on your input data before your sql code is executed.
simply return out of the function if any checks you want to do fail e.g.
If(string.IsNullOrEmpty(variable to check))
... Return or configure error message for user ...
Or perform some logic to show the user you dont want blank fields.
If you are going to validate that you should change your database field properties to do that task.
if you want to do this with some code you should add something like this:
bool val = true;
if (jobTxt.Text.Trim() == string.Empty) {
val = false;
}
if(val==true){
command.ExecuteNonQuery();
}
else{
MessageBox.Show("Some field is empty")
}
and repite the sentence if for each textbox you want to do a validation.
i hope this help you.
You can say which textbox is empty on the else sentences on the textbox.
I am making a windows application that sync the source data to Active Directory.
This application works like this.
Choose Source Data(Department, User)
Mapping user or department attributes from source data
When Application service is run, it create groups and users in Active Directory
And also it sets the attributes to users and groups.
When I try to set group or user attributes(properties), it throws exception message like this.
in DirectoryEntry.CommitChanges(); block
The directory
service cannot perform the requested operation on the RDN attribute of an object.
I tried to solve it, but it's really hard to me because I`m not good at Active directory...
Code is below, Please share your knowledge.
//ppk: department key column, pk:user key column, row : Source DataTable's row
void CreateADUser(string ppk,string pk,DataRow row)
{
//password
string pass = GetPass(pk,row,LogSections.AD);
//OU
DirectoryEntry addept = adm.FindOU(ppk);
//principal path
string sOU = adm.GetPrincipalPath(addept);
var aduser = adm.CreateNewUser(sOU, pk, pass, pk, null, null, adm.sDomain);
SetAdUserProperties(pk, pass, row);
MoveUser(ppk,pk);
}
void SetAdUserProperties(string pk,string pass,DataRow row)
{
if (row == null) return;
//list of mapped column(AD User attributes)
List<ADMapping> MappingPatterns = GetAdMappings(Words.User,false);
//Columns name of Source Data table's row
var colnames = Tool.GetColNames(row);
//get user proterties
var aduser = adm.GetUser(pk);
//directory entry of users
var de=aduser.GetUnderlyingObject() as DirectoryEntry;
//looping mapped column of user attributes
foreach (var ADMap in MappingPatterns)
{
string val = ADMap.Mapping;
//mapped columns value
val=Util.ReplaceColPattern(val, row);
SetProperty(de, ADMap.CN, val);
}
if (!string.IsNullOrWhiteSpace(pass))
{
var UserPkColumn = AppConfigHelper.GetAppString(Words.SourceUserPKColumn);
UserPkColumn = Util.GetActualColName(UserPkColumn);
aduser.SetPassword(pass);
QueryHelper.Update(QueryHelper.ConnectionString, Words.ShadowUserTable
,new SqlParameter[] { new SqlParameter("#passwd", pass) }
, new SqlParameter("#"+UserPkColumn,pk));
}
aduser.Save();
}
public void SetProperty(DirectoryEntry oDE, string sPropertyName, object sPropertyValue)
{
if (sPropertyValue != null && !string.IsNullOrWhiteSpace(sPropertyValue.ToString()))
{
if (oDE.Properties.Contains(sPropertyName))
{
oDE.Properties[sPropertyName].Value = sPropertyValue;
}
else
{
oDE.Properties[sPropertyName].Add(sPropertyValue);
}
try
{
oDE.CommitChanges(); //exception here.
oDE.Close();
}
catch (Exception)
{
}
}
}
I also asked this question to other forums, and finally got it.
Before DirectoryEntry.CommitChanges(); set UserPropertyCache property to true
and call the RefreshCache method.
It's hard to see what's the cause of the issue here as we're not seeing what attributes you are trying to set.
That said, you can't just add an attribute if it doesn't exist on your AD object so this part of your code definitely has an issue :
if (oDE.Properties.Contains(sPropertyName))
{
oDE.Properties[sPropertyName].Value = sPropertyValue;
}
else
{
//The following line will never work in this context
oDE.Properties[sPropertyName].Add(sPropertyValue);
}
If I had to make an educated guess, I'd say you're either trying to set an attribute that can't be set, or the User you're adding doesn't have all it's mandatory attributes set.
I have a C# application where I need the list to "remember" what has been previously entered in it.
http://oi40.tinypic.com/2ivhcuw.jpg
So once the attendee has been added, the previous attendee will stay in the list if a new one is added. Currently it just displays the most recent attendee, I want them to save, until I decide to clear them (maybe in a session?)
List<Information> infoList = new List<Information>();
Information data = new Information();
firstName = resultEntry.Properties["givenname"].Value.ToString();
lastName = resultEntry.Properties["sn"].Value.ToString();
fullName = firstName + " " + lastName;
//data.CWID = resultEntry.Properties["username"].Value.ToString();
data.FullName = fullName;
// data.Email = resultEntry.Properties["email"].Value.ToString();
infoList.Add(data);
For storing items into a session:
Session["MyInformation"] = data;
receiving data out of a session
List<information> data = (List<information>)Session["MyInformation"];
But beware: Sessions ought to be forgotten.
One might consider viewstate but beware here to: viewstates do make a page slow. (easy testings in how big a html page gets when viewstate is added)
one last option might be to include cookies for getting a longer hold on the items.
Write a cookie: http://msdn.microsoft.com/en-us/library/78c837bd(v=vs.100).aspx
Read a cookie : http://msdn.microsoft.com/en-us/library/bd70eh18(v=vs.100).aspx
another option is to hold the data in database => this however means more traffic.
(also a small hint on the sessions, place them in a seperate static class so you can get them through the whole project instead of one page
public static class MySessions
{
public static List<Information> MyData
{
get{
//EDIT in the GET
if(HttpContext.Current.Session["MyInformation"] != null)
return (List<information>)HttpContext.Current.Session["MyInformation"];
else
{
HttpContext.Current.Session["MyInformation"] = new List<Information>();
return new List<Information>();
}
}
set{HttpContext.Current.Session["MyInformation"] = value;}
}
}
EDIT:
use the class as follows: (it's a static class, by entering the classname followed by the property, you can call for it instead of first instantiating the class.
//Set the value from Somewhere
MySessions.MyData = new List<Information>();
//get the values from somewhere
var myInfo = MySessions.MyData;
Depending on the length of the duration of "saving":
Use ViewState - if you want the list to be empty on a page load (not postback). (i.e. ViewState["data"] = infoList)
Use Session - if you want to manually empty the list based on some condition.
Trying to unit test functions which access a entity framework. So i tried to put all the entity code into the test function below? However it stops at the Linq statement; obviously trying to access the database is too much drama for it. Maybe a work around would be too to create a replica database within the unit test function based on sql lite or compact;(Its not a big database anyways) then execution would not have to leave the test function? Is this possible and how would i implement it?
public void RetreiveKeyFnTest()
{
StegApp target = new StegApp(); // TODO: Initialize to an appropriate value
string username = "david"; // TODO: Initialize to an appropriate value
string password = "david1"; // TODO: Initialize to an appropriate value
string ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseEntities"].ToString();
var dataContext = new DatabaseEntities(ConnectionString);
var user = dataContext.Users.FirstOrDefault(u => u.Username.Equals(username) && u.Password.Equals(password));
Assert.IsNotNull(user);
//target.RetreiveKeyFn(username, password);
//Assert.IsInstanceOfType(target.RetreiveLogs,typeof(DataAccess));
//Assert.IsInstanceOfType(target.p);
//Assert.IsNotNull(target.RetreiveLogs.AuthenitcateCredentials(username,password));
//Assert.Inconclusive("A method that does not return a value cannot be verified.");
}
Below is the code i am trying to test:
public void RetreiveKeyFn(string username, string password)
{
BusinessObjects.User p = RetreiveLogs.AuthenitcateCredentials(username,password);
if (p != null)
{
if (RetreiveLogs.RetreiveMessages(p.UserId) == null)
{
DisplayLogs.Text = "Sorry No messages for you recorded in Database, your correspondant might have chose not to record the entry";
}
else
{
MessageBox.Show("LogId = " + RetreiveLogs.RetreiveMessages(p.UserId).LogId + "\n" +
"UserId = " + RetreiveLogs.RetreiveMessages(p.UserId).UserId + "\n" +
"Message Key = " + RetreiveLogs.RetreiveMessages(p.UserId).MessageKey + "\n" + "PictureId = " + RetreiveLogs.RetreiveMessages(p.UserId).PictureId +
" Date & time = " + RetreiveLogs.RetreiveMessages(p.UserId).SentDateTime);
DisplayLogs.Visible = true;
}
}
else
{
MessageBox.Show("Please enter your correct username and password in order to retreive either key, image or both from Databse");
}
}
First, you should be able to access the same database in your test application as the one you're using in your main/actual application. You just need to make sure that your Test project contains your connection string in its own App.config.
The initialization of the context should be done either inside your StegApp(), or you should be able to pass a context to your StegApp() from a different scope. From what I read of your code, your StegApp() will not be able to access the dataContext variable you created.
Your test for null user already happens inside the RetrieveKeyFn() under the AuthenticateCredentials() method so there's no need for the first "Assert.IsNotNull(user)". I would recommend separating your business logic for RetrieveKeyFn from your UI behaviors so that you can easily do unit tests. You can bind the "Messagebox" operations to say a button click event handler which calls just RetrieveKeyFn(). I would suggest maybe something like this:
public class StegApp
{
public DatabaseEntities context;
//other properties
public StegApp()
{
//assuming your DatabaseEntities class inherits from DbContext.
//You should create other constructors that allow you to set options
//like lazy loading and mappings
this.context = new DatabaseEntities();
}
//ASSUMING YOUR RetrieveLogs.RetrieveMessages() function returns
//a Message object. replace this type with whatever type the
//RetrieveLogs.RetrieveMessages() method returns.
public Message RetrieveKeyFn (string username, string password)
{
BusinessObjects.User p = RetreiveLogs.AuthenitcateCredentials(username,password);
if (p != null)
{
var message = RetrieveLogs.RetrieveMessages(p.UserId);
if (message == null)
// handle behavior for no messages. In this case
// I will just create a new Message object with a -1 LogId
return new Message {LogId =-1};
else
return message;
}
else
//handle behavior when the user is not authenticated.
//In this case I throw an exception
throw new Exception();
}
//on your button click handler, do something like:
// try
// {
// var message = RetrieveKeyFn(txtUsername.Text.Trim(), txtPassword.Text.Trim());
// if (message.LogId == -1)
// DisplayLogs.Text = "Sorry No messages for you recorded in Database, your correspondant might have chose not to record the entry";
// else
// {
// MessageBox.Show("Log Id = " + message.LogId)
// etc. etc. etc.
// }
// }
// catch
// {
// MessageBox.Show ("user is not authenticated");
// }
}
When you do your unit test, remember to have the appropriate configuration strings in your test project's App.Config If the app.config does not yet exist, go ahead and create one. You should create tests for all possibilities (i.e. 1) user is valid, you get the message, 2) user is valid, there are no messages, 3) user is invalid).
Here's an example for case 2
[TestMethod]
public void RetrieveKeyFnTest1()
{
StegApp target = new StegApp(); // this creates your context. I'm assuming it also creates your RetrieveLogs object, etc
var username = "UserWithNotMessages"; //this user should exist in your database but should not have any messages. You could insert this user as part of your TestInitialize method
var password = "UserWithNotMessagesPassword"; //this should be the proper password
var message = target.RetrieveKeyFn(username, password);
Assert.AreEqual (-1, message.LogId);
}
I got my unit tests to work fine. The mistake i had was not to copy the app.config file into the test project! Although to be honest i expected Visual studio would have done that anyways.
Im made a webform that insert data to the database. When the insert button was clicked, the codes for inserting data is triggered and after successfully inserting data, It redirects to other page that says "Data Inserted Successfully".
It was like this...
INSERT PAGE.....
if (CodeClass.InsertData(txtFirstName.Text, txtLastName.Text, Gender) == true)
{
String A = "InsertSuccess";
Response.Redirect("OtherPage.aspx?&lnk=" + A);
}
OTHER PAGE....
String link = null;
link = Request.QueryString["lnk"];
if (link == "InsertSuccess")
{
txtLabel.Text = "Record inserted succesfully!";
}
My problem is when I access the OTHER PAGE even I did not use the insert page..I am getting same results. Lets say I typed ..http://localhost:8672/OtherPage.aspx?&lnk=InsertSuccess in the address bar..I am getting "Record inserted succesfully!" label. I want the OTHER PAGE to never show "Record inserted succesfully!" message when I actually did not insert something but rather just access it through the browser's address bar.
you may use Session variable to store updated status and then check if it is available in otherpage.aspx,update textbox and reset it.
Insert Page
if (CodeClass.InsertData(txtFirstName.Text, txtLastName.Text, Gender) == true)
{
Session["status"]="InsertSuccess";
Response.Redirect("OtherPage.aspx");
}
}
Other Page
if (Session["status"]!=null)
{
txtLabel.Text = "Record inserted succesfully!";
Session["status"]=null;
}
Well for what I see you dont want to get the message just by typing "http://localhost:8672/OtherPage.aspx&lnk=InsertSuccess" in the address bar, then I would recommend you that instead of passing a string with a flag you should pass the record ID, then on your other page you should check that your record ID really exist on your table.
For that I hope that you are working with ID as a GUID type.
You'll have something like this:
"http://localhost:8672/OtherPage.aspx&lnk=BBB5259E-F5A3-4271-ABC8-D95A00BE9770"
Otherwise it would be too easy to remember an int ID.
if (CodeClass.InsertData(txtFirstName.Text, txtLastName.Text, Gender) == true)
{
HttpContext.Current.Items["A"]= "Inserted Successfully";
Server.Transfer("OtherPage.aspx);
}
string ContextData =(string) HttpContext.Current.Items["A"];
if(!string.Empty(ContextData))
{
Label1.Text = ContextData;
}
Assuming you fix your URL and remove the "&" after the "?" .. You have to remove the "&"
Have you thought about the browser cache? sometimes cache can do this and I have struggled with an issue similar to yours because of the cache settings.
You should change your querystring.
Response.Redirect("OtherPage.aspx?&lnk=" + A);
Should be:
Response.Redirect("OtherPage.aspx?lnk=" + A);
The '&' sign when not encoded are treated as a parameter separator.
Response.Redirect("OtherPage.aspx?lnk=" + A + "&secondParam=someValue");
As requested by comment:See this SO post
MSDN HttpServerUtility.UrlEncode
.NET Slave - Working with query strings
and the String.IsNullOrWhiteSpace() might be handy sometimes.
Just add this script after succeed inserted code:
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "setInterval(function(){location.href='Default.aspx';},3000);", true);