Asked by:
How to insert current email into database?

Question
-
User481677910 posted
Hi,
I am trying to insert email address of the current user who is filling the form. I have created column in table with name Email
place the Email in insert query with value @email
and
cmd.Parameters.AddWithValue("@Email", Membership.GetUser(User.Identity.Name).Email);
But still it's not working.
Thursday, October 10, 2019 6:18 PM
All replies
-
User475983607 posted
Have you tried running your code through the debugger? Are you receiving an error and if so what is the error? Does the current user have an email address in the system? Is there anyway you can post all the relevant code?
Thursday, October 10, 2019 6:21 PM -
User481677910 posted
Guid newGUID = Guid.NewGuid(); String strConnString = System.Configuration.ConfigurationManager .ConnectionStrings["userConnectionString"].ConnectionString; Session["UserName"] = HttpContext.Current.User.Identity.Name; SqlConnection con = new SqlConnection(strConnString); string strQuery = "insert into DB_110006_Order (Guid, Id, ,Email, UserName, Order_Name)" + " values ( @Guid, @Id, @Email, @UserName, @Order_Name)"; SqlCommand cmd = new SqlCommand(strQuery); cmd.Parameters.AddWithValue("@Guid", newGUID.ToString()); cmd.Parameters.AddWithValue("@Id", Membership.GetUser().ProviderUserKey); cmd.Parameters.AddWithValue("@Email", Membership.GetUser(User.Identity.Name).Email); cmd.Parameters.AddWithValue("@Username", Session["UserName"]);
cmd.Parameters.AddWithValue("@Order_Name", OrderName.Text)Yes user email is in database already as I have user membership login system. There is no error coming but after I click submit it's not submitting anything. If I remove Email completely then it is working fine.
Thursday, October 10, 2019 6:26 PM -
User475983607 posted
What is the result of the following code?
Membership.GetUser(User.Identity.Name).Email
new2world2015
here is no error coming but after I click submit it's not submitting anything. If I remove Email completely then it is working fine.Are you hiding exceptions? Have you tried simply debugging your code by placing a break point and single stepping?
IMHO, it's easier to get the user once rather than twice. Also why do you have the user name in Session when the user name is...
User.Identity.Name
Thursday, October 10, 2019 6:35 PM -
User481677910 posted
Membership.GetUser(User.Identity.Name).Email
It does nothing means I am trying on web server directly it just doesn't insert into table anything and take me to next page once I click on submit as this is what I have coded to redirect to another page as I click on submit.
Thursday, October 10, 2019 6:39 PM -
User481677910 posted
That is super secret lol.
Thank anyways. It's working now as I had a comma mistake in insert.
Thursday, October 10, 2019 6:50 PM -
User475983607 posted
It does nothing means I am trying on web server directly it just doesn't insert into table anything and take me to next page once I click on submit as this is what I have coded to redirect to another page as I click on submit.The design does not make sense. If the email address exists then why do you need to copy the email to another table? You have the user's Id so you can just do a join to get the email address.
Anyway, I suspect there is an error if removing the email address allow the record to be added. Do you have an empty catch block that is hiding errors?
Thursday, October 10, 2019 6:50 PM -
User475983607 posted
That is super secret lol.
Thank anyways. It's working now as I had a comma mistake in insert.
So you are hiding exceptions from yourself?
Thursday, October 10, 2019 6:51 PM -
User481677910 posted
new2world2015
It does nothing means I am trying on web server directly it just doesn't insert into table anything and take me to next page once I click on submit as this is what I have coded to redirect to another page as I click on submit.The design does not make sense. If the email address exists then why do you need to copy the email to another table? You have the user's Id so you can just do a join to get the email address.
Anyway, I suspect there is an error if removing the email address allow the record to be added. Do you have an empty catch block that is hiding errors?
Id, ,Email, was the mistake
Thursday, October 10, 2019 6:52 PM -
User481677910 posted
mgebhard
new2world2015
That is super secret lol.
Thank anyways. It's working now as I had a comma mistake in insert.
So you are hiding exceptions from yourself?
No I have exception to give me error but in this case it didnt.
catch (Exception ex) { Response.Write(ex.Message); } finally { con.Close(); con.Dispose();
Thank you for helping me out.
Thursday, October 10, 2019 6:53 PM