|
oh my. Lots of comments:
1.
please use PRE tags to improve code readability.
2.
the problem is you dispose of your reader on the first match, and then you continue the while loop which depends on the reader. The dispose should be outside the while loop!
3.
the easier way to get the dispose stuff right is by using a using statement, like so (note the result of PRE tags):
using (SqlDataReader reader = cmd.ExecuteReader()) {
while (reader.Read()) {
if (txtUsername.Text == (reader["UserName"].ToString()) && txtPassword.Text = reader["Password"].ToString())) {
break;
}
}
}
4.
So you are storing usernames and passwords in a table. Do you consider that a good practice?
Normally one would:
- [rephrased] concatenate the username, the password, and a little constant string ("salt"); [/rephrased]
- then call a hashing function;
- and lookup the table, which does not contain the password, all it holds is the hash value.
The reason is this: if your database gets compromised, all users loose the privacy of their password (and most of them would have been foolish enough to use the same username and password on other applications and/or web sites).
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
modified on Thursday, May 26, 2011 8:09 AM
|
|
|
|
|
Yeah, what Luc said. Plus:
SqlCommand cmd = new SqlCommand("SELECT * FROM [TBLUsers]", conn);
cmd.Connection = conn;
SqlDataReader reader = null;
reader = cmd.ExecuteReader();
txtPassword.Text == reader
And you don't break the loop.
Of course, you should probably use a parameterized query rather than querying the whole table.
|
|
|
|
|
try this:
try
{
SqlConnection conn = new SqlConnection("");
conn = DataHandler.conn;
String username = txtUsername.Text;
String password = txtPassword.Text;
SqlCommand cmd = new SqlCommand("SELECT * FROM [TBLUsers]", conn);
conn.Open();
DataTable table = new DataTable();
SqlDataReader reader = null;
reader = cmd.ExecuteReader();
while (reader.Read())
{
if (txtUsername.Text == (reader["UserName"].ToString()) && txtPassword.Text = reader["Password"].ToString()))
{
table.Load(reader);
POS2 p = new POS2();
MainPage page = (MainPage)this.MdiParent;
p.MdiParent = page;
p.WindowState = FormWindowState.Maximized;
p.Show();
page.WelcomeUser = reader["UserName"].ToString();
}
}
}
catch
{
reader.Dispose();
}
finally
{
conn.close();
}
suchita
|
|
|
|
|
That is hardly any better than the original. I suggest you read the thread again and learn from it.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
|
|
|
|
|
I am not suggesting any database table design here.. just gave a simple view on the error mentioned on the original problem.
Invalid attempt to call MetaData when reader is closed.
suchita
|
|
|
|
|
so now:
1. you rely on some exception to dispose of the reader; if no exception gets thrown, the reader isn't disposed.
2. you catch and basically ignore any exception you are getting, a very bad practice.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
|
|
|
|
|
thanks, this worked perfectly
|
|
|
|
|
Its my first time to deal with Web Services and I am having errors on consuming Web Methods with parameters. Below is my code.
[WebMethod]
public string HelloWorld()
{
System.Diagnostics.EventLog.WriteEntry("HELLO WORLD INVOKED", "Congratulations, you have just invoked Hello World method of your local Web Service");
return "Hello World";
}
[WebMethod]
public string PostMethod(string test)
{
System.Diagnostics.EventLog.WriteEntry("POST METHOD INVOKED", "Congratulations, you have just invoked Post Method of your local Web Service. Data - " + test.ToUpper());
return test.ToUpper();
}
The way I am calling these web methods are by creating a HTTP POST to http://localhost/Service1.asmx/HelloWorld and http://localhost/Service1.asmx/PostMethod
I am able to invoke HelloWorld method successfully(I can verify it by looking at the Event Log) but when calling PostMethod I always get Internal Server Error. I also tried doing http://localhost/Service1.asmx/PostMethod?test=value but still no luck.
I know this can be solved by searching Google but since I am a beginner, I am not familiar on which direction to search. I tried though but no luck.
Appreciate your help. .
|
|
|
|
|
Try running in the debugger and navigating to http://localhost/Service1.asmx (or right-click the asmx file and choose view in browser).
You should see your methods listed - click one to see sample requests/responses for the method.
(if you run in debugger you can set breakpoints in your methods so you don't have to use log entries...)
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Under the PostMethod method, I can see this section under the HTTP Post
POST /xmlservice1dot1/service1.asmx/PostMethod HTTP/1.1
Host: ***.***.**.**
Content-Type: application/x-www-form-urlencoded
Content-Length: length
test=string
However, its not yet clear to me on how to consume the PostMethod. I was thinking you could consume it by creating an HTTP post to http://localhost/Service1.asmx/PostMethod?test=string but I am not sure if it can be done. I was doing a post to http://localhost/Service1.asmx/HelloWorld and it works fine but I cant get it to work for PostMethod. Can you please suggest a way for me to do it? Thanks in advance.
|
|
|
|
|
samir.abda wrote: I was doing a post to http://localhost/Service1.asmx/HelloWorld and it works fine but I cant get it to work for PostMethod.
From the browser address bar you'll need to be able to call using GET.
Add this to your web.config in the system.web section:
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
That seemed to do the trick. Thank you.
Another question, given that http://localhost/Service1.asmx/PostMethod?test=string already works, is there a way to make the value of test dynamic? Like putting it in an XML for example? I tried posting something like the XML below to the web method but it does not work. Hope you can help me with a solution.
<test>This is a test string</test>
|
|
|
|
|
What do you mean by "make the value of test dynamic"?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
For example, if I post to this URL, http://localhost/Service1.asmx/PostMethod?test=string, the value of the parameter test would be string . Is there a way to do this dynamically, like call http://localhost/Service1.asmx/PostMethod and just post the XML containing the value for the test parameter?
|
|
|
|
|
Are you doing this from code? I have no idea how you're consuming your web service
If you're calling your web service from another application, Visual Studio can create a servicereference for you which will give you a nice proxy class you can use to call the web service methods just like they're local...
Mark Salsbery
Microsoft MVP - Visual C++
modified on Thursday, May 26, 2011 4:40 AM
|
|
|
|
|
Oh sorry for that. This is how I consume the web service. Basically, I post an XML to the web method
XmlDocument _xml = new XmlDocument();
_xml.Load(filePath);
ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy;
WebRequest _request = WebRequest.Create((WebServiceURL));
_request.Credentials = new NetworkCredential(UserName, Password);
_request.Method = "POST";
_request.ContentType = "text/xml";
XmlTextWriter xw = new XmlTextWriter(_request.GetRequestStream(), Encoding.UTF8);
_xml.WriteTo(xw);
xw.Close();
WebResponse response = _request.GetResponse();
response.Close();
|
|
|
|
|
Why not use Visual Studio to make a proxy class for you so you don't have to do that all yourself?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Sorry I haven't mentioned that the real web service is created in Java. What I provided was just a test webservice to verify my solution. I have no control on the Web Service since it is owned by the client. I am not sure if creating a proxy class is still applicable even if the Web Service is not creating in .Net. Will it work even in this case? Sprry, I know I could verify this on my own but right now, there are no Java test web services so I have created my own, in .Net. I really appreciate your help on this. Thank you in advance.
|
|
|
|
|
I don't know about Java....you could ask on the Java board though!
Without .NET I don't believe the proxy class would apply...
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Basically, as Mark suggested, you need to navigate to your webservice through your browser. From there, you can see the format of the XML messages that you will send/receive. You will also see the headers that you need to assign to your _request object before firing it up. I can see in your code that you havent assigned the SOAPAction header in your request object. You also need to set that if you are going to use SOAP, like this.
_request.Headers["SOAPAction"] = "[Value of SOAPAction, you can see this by navigating on the web method of the web service, through browser]";
After setting this, I believe your'e good to go. Just make sure that you are using the right format of XML that was provided on the web service.
Ignorance of the ability brings disability.
|
|
|
|
|
Hello,
I am using SOAP method to get a list of tours from a web service in the internet. The pictures in the tourlist do not show up because of LOCALHOST definition in their source. How can I set LOCALHOST in web config to use web services root address.
Thank you!
What a curious mind needs to discover knowledge is noting else than a pin-hole.
|
|
|
|
|
You can't. There is no setting in the web.config file that will convert the address in the data you receive. Do you know any developers who can help you with this?
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
I was expecting a solution rather than a judgment.
What a curious mind needs to discover knowledge is noting else than a pin-hole.
|
|
|
|
|
Once you get the data back, why not just do a string replace LOCALHOST -> real host name? The issue isn't on your side, the web service isn't implemented properly. A web service should *NEVER* be returning LOCALHOST in URLs. You just need to work around the web service bug with the string replace.
|
|
|
|
|
Thank you very much that is what I needed.
What a curious mind needs to discover knowledge is noting else than a pin-hole.
|
|
|
|