Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
i am send password via email..butt when enter the email address and sed the..show the error
how to resolve this error,,
C#
Server Error in '/' Application.

The ConnectionString property has not been initialized.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.

Source Error: 


Line 118:                SqlDataAdapter dAdapter = new SqlDataAdapter(command);
Line 119:                command.Parameters.AddWithValue("@Email", Email);
Line 120:                connection.Open();
Line 121:                dAdapter.Fill(dsPwd);
Line 122:


What I have tried:

C#
<pre lang="c#">public ActionResult ForgotPassword(User s)
        {
            using (userEntities2 db = new userEntities2())
            {

                string Email = Request.Form["Email"];
                string Msg = Request.Form["Message"];


                string strConnection = ConfigurationSettings.AppSettings["userEntities2"];
                string strSelect = "SELECT UserName,Password FROM user WHERE Email = @Email";

                SqlConnection connection = new SqlConnection(strConnection);
                SqlCommand command = new SqlCommand();
                command.Connection = connection;
                command.CommandType = CommandType.Text;
                command.CommandText = strSelect;

                DataSet dsPwd = new DataSet();
                SqlDataAdapter dAdapter = new SqlDataAdapter(command);
                command.Parameters.AddWithValue("@Email", Email);
                connection.Open();
                dAdapter.Fill(dsPwd);

                connection.Close();

                if (dsPwd.Tables[0].Rows.Count > 0)
                {
                    string MailingAddress = ConfigurationManager.AppSettings["MailingAddress"].ToString();
                    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(MailingAddress, Email);
                    SmtpClient client = new SmtpClient();
                    message.Subject = "Forget Password";
                    message.IsBodyHtml = true;
                    message.Body = "Username: " + dsPwd.Tables[0].Rows[0]["UserName"] + "<br><br>Password: " + dsPwd.Tables[0].Rows[0]["Password"] + "<br><br>";

                    client.Host = "smtp.mail.yahoo.com";
                    client.Port = 587;
                    client.Credentials = new System.Net.NetworkCredential
                    ("email", "password");
                    client.EnableSsl = true;
                    client.Send(message);
                    Response.Write("&lt;script LANGUAGE='JavaScript'>alert('Email has Sent')&lt;/script>");

                }
                else
                {
                    Response.Write("&lt;script LANGUAGE='JavaScript'>alert('Email Not Register')&lt;/script>");
                }


                return View(s);
            }
        }</pre>
Posted
Updated 24-Mar-16 6:34am
v2
Comments
ZurdoDev 24-Mar-16 10:42am    
The error says that you did not set your connection string on your SqlConnection.
Mehak Naaz 24-Mar-16 10:55am    
how to resolve
ZurdoDev 24-Mar-16 10:57am    
Set the connection string.
Mehak Naaz 24-Mar-16 11:00am    
set the connection string..but same error
ZurdoDev 24-Mar-16 11:08am    
You cannot get the error "ConnectionString property has not been initialized" if you actually did set the connection string.

You may think you have, but you aren't. Debug the code and see what is happening.

Your connection string is meant for Entity Framework. SqlConnection would require a different one. But as your project is an Entity Framework-project I see no reason why you would want to bypass Entity Framework here. You actually do initialize an EF-DBContext with that using-Directive at the top of your method - which you then don't use at all.

To stick with your question I would suggest you remove all that Sql****-stuff and replace it with an EF-query. BUT: You should neither store passwords as plain text in your database nor send them via email. Please take a look at these articles:

Secure Password Authentication Explained Simply[^]

Troy Hunt: Everything you ever wanted to know about building a secure password reset feature[^]
 
Share this answer
 
v2
Comments
Mehak Naaz 24-Mar-16 11:26am    
please write the ef query for forget paswword
Sascha Lefèvre 24-Mar-16 11:36am    
No. For two reasons:

1) As I wrote in my answer above, you should never store passwords as plain text or send them via email. Passwords are supposed to be secure. Email is not secure. I will not support this practice.

2) That query would be very, very basic. If you started an EF-project you should be able to do that yourself. Otherwise you will ask for every other query that someone else write it for you as well. Take a look at the various online EF-tutorials if you need to learn about EF-queries.
Mehak Naaz 24-Mar-16 11:40am    
write qurey for forget password.
thanks in advance
Sascha Lefèvre 24-Mar-16 11:43am    
Take a look at the articles I linked for you. Do it. Just do it. Please.
Looking at you question history you have really asked this same question multiple times. You asked about converting aspx code to ado.net and now you trying to throw Eintity Framework on top of it and you are not actually using any EF feature in the code you have displayed.

You seem to ask questions and then post very short replies to advice and then finally ask someone to write the code for you. You even did that to a very simple question I answered a few weeks ago where you were missing a column in a stored procedure.

Is any of this code even your? I suggest you slow down and learn the basics before you get much further.
 
Share this answer
 
Comments
Sascha Lefèvre 24-Mar-16 12:38pm    
My 5. Maybe even too polite..
You have to assign the connection to the command;

C#
command.Connection = connection;
SqlDataAdapter dAdapter = new SqlDataAdapter(command);


You're not using Entity Framework though, and as stated above what you're doing is bad for many reasons.
 
Share this answer
 
Comments
Mehak Naaz 24-Mar-16 10:56am    
i am use this code in entity framwork...how to modifty this code..run properly

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900