|
I have a window form name Login and a database with a Table name Login it has two fleids usernm, pwd.
usernm is to hold user names and pwd is to hold password.
Ok on the form I have two text box name user_name and user_password. And two buttons. I want this to check the user name and pasword.
I have made a sqlConnection name sqlConn.
I have made a sqlCommand name sqlcommand1;
I have made a sqlDataReader name dataReader.
here is my part of my code. What am I doing wrong with my string?
void check_password()
{
string query = string.Format(S"SELECT * FROM BankLogin where usernm= '{0}' and pwd= '{1}'", user_name.Text, user_password);
sqlConn.Open();
sqlcommand(query, sqlConn);
slqcommand1.ExecuteReader();
}
Harrison Brock
|
|
|
|
|
How can we even know what's wrong if you don't tell us what exception you're getting?
I will say this, though: don't create SQL statements using string formatting anymore! That's an old hack that is riddled with encoding problems. What if the username and/or password contains a double or single quote? This forces you to have to encode the strings yourself.
Instead, use parameterized queries, which are much more elegant, powerful, and allow for easy batch updates. Use the appropriate DbParameter derivative class. So, if you're using System.Data.SqlClient.SqlCommand , then use the System.Data.SqlClient.SqlParameter . You declare the parameters using "@name" and then you can set their values at any time before executing the statement and can even receive output parameters (if the OLE DB driver supports it).
So, you're statement becomes this:
SqlConnection conn = new SqlConnection(
"Data Source=COMPUTERNAME; Initial Catalog=DBName; Integrated Security=SSPI");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM BankLogin WHERE usernm=@usernm AND pwd=@pwd";
SqlParameter usernm = cmd.Parameters.Add("@usernm", SqlDbType.NVarChar, 40);
SqlParameter pwd = cmd.Parameters.Add("@pwd", SqlDbType.NVarChar, 40);
SqlDataReader reader = null;
try
{
usernm.Value = "username";
pwd.Value = "password";
reader = cmd.ExecuteReader();
}
catch (Exception ex)
{
Console.Error.WriteLine("Error: {0}", ex.Message);
}
finally
{
if (reader != null) reader.Close();
conn.Close();
} You don't have to worry about encoding the text and you can simply change the values and run the query again if needs be.
See the documentation for the SqlDataReader in the .NET Framework SDK for more information and examples.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
G'Day Harrison
I cant help you with the question that you asked but I can tell you something that will help you down the track: I recently tried to do the same thing and store a users login information in a database. This is bad practice because the users information can be easily read from the database! You should use encrytion and decryption.
Jon
|
|
|
|
|
Hello.
Given an array of binary data stored in an ArrayList container, do you associate the container as a datasource in a Data Grid control?
private ArrayList xList = new ArrayList();
// Arbitrarily add 64 integers into container
for (int i = 0; i < 64; ++i)
this.xList.Add(i);
// Assuming Data Grid was created via wizard
thisTheDataGrid.DataSource = this.xList;
thisTheDataGrid.Refresh();
The example above compiles okay. However, nothing shows up in the data grid. All you would see is 64 empty rows and no columns.
Do you need to set a specific property to show arbitrary data in the grid?
Thanks,
Kuphryn
|
|
|
|
|
You can use any IList or IListSource implementation (ArrayList implements IList ). If you have any DataGridTableStyle s configured in the DataGrid.TableStyles property, you must set the DataGridTableStyle.MappingName to the type, i.e. "ArrayList", so that it is mapped correctly.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Okay. Thanks.
I tried inserting "ArrayList" as the mapping name for the tablestyle. That did not make a difference.
The problem is still that nothing shows up in the Data Grid. In fact, there are no grid line. All I see are row borders.
Kuphryn
|
|
|
|
|
If you are using table styles and they don't apply to the current data binding, that what you should expect to see. Instead of an ArrayList , use an int[] array and use "int[]" as the DataGridTableStyle.MappingName . See the documentation for DataGridTableStyle.MappingName in the .NET Framework SDK for more information.
Are you even using table styles? If so, maybe try not adding them and you should see something. Again, a data source can be anything that implements IList or IListSource . Some controls' DataSource properties will even except IEnumerable implementations as a last resort.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I develop a control that derived from TextBox control. I want to give a Bitmap to it when it displayed on the VS.Net toolbox. and I also want if the bitmap file is not exist, the default bitmap should be the standard TextBox control's bitmap. how to write the "[ToolBitmap()]".
Thanks a lot!
Alan
|
|
|
|
|
The best way is to make sure it always exists. Embed the image as an embedded resource in your project (change the Build Action to "Embedded Resource" in the project after adding the image to your project if you haven't already).
Then, in the ToolboxBitmapAttribute , you have a couple options. If it uses the same namespace and name as a class and has the .bmp extension (so a Windows bitmap), then you can simply specify the type of the class that's named like it using [ToolboxBitmap(typeof(MyClass))] . If it simply shares the same namespace, you can use [ToolboxBitmap(typeof(MyClass), "SomeImage.bmp")] . If you want to worry about having to the deploy the image as well, you can just specify the name like [ToolboxBitmap("SomeImage.bmp")] . The former two ways are much better and will ensure that your image always exists.
BTW, the namespace of the image is obtained using the root namespace of your project (defaults to the project name) plus any folders it might be in within your project. These folders will also cause VS.NET to use the same scheme to create a namespace for your classes that you add to your project/folder, but moving that source file will not automatically change its namespace. It will for embedded resources, though.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
In Visual C++ 6.0 you used to be able to go to the next error or find in files tag by pressing F4. This no longer works in Visual Studio.Net 2003. Does anyone know the keyboard shortcut for "Go To Error/Tag" in Visual Studio.Net 2003?
<RANT>
Does anyone else find it frustrating that Microsoft changed most of the keyboard shortcuts between Visual C++ 6.0 and Visual Studio.Net 2003?
</RANT>
Sincerely,
-Ron
|
|
|
|
|
Try pressing F8, I think it work for errors as well as the mulit-file find.
Gary
|
|
|
|
|
Thanks. F8 works.
Sincerely,
-Ron
|
|
|
|
|
No, but you had a choice the first time you started it to use different layouts / defaults, one of which was compatibility with Visual Studio 6. You can still change this from the Start page in VS.NET to default to a Visual Studio 6-like configuration.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks. I didn't know you could do that.
Sincerely,
-Ron
|
|
|
|
|
here's a complete list of all available shortcut keys
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxoriKeyboardShortcuts.asp
|
|
|
|
|
I am new to c#. I have created an class where an sql connection is opened in the constructor and is closed and disposed in the destructor.
However, it appears that the destructor is not being called when the object goes out of scope. It is not called until I close the form, at which time I get a "Handle is not initialized" error.
Is this the way this is supposed to work? If so, is it possible to have a class that opens a connection upon construction and automatically disposes of it?
|
|
|
|
|
Yes, this is the way it's supposed to work. The garbage collector "lazily" cleans up objects that are no longer in scope and have no references.
It is a much better practice to open your SQL connection only when you need it and close it when your done. Do not hold the connection open during the lifetime of your object.
RageInTheMachine9532
|
|
|
|
|
That's too bad. Since I tend to write wrappers around database connections, I guess my wrappers will have to include dispose functions as well.
I'm still not clear as to why the connection handle (which is private to the class and created in the constructor) would be lost before the destructor is called.
|
|
|
|
|
You're wrappers can simply open and close the connections as needed in your method implementations as well. We do that in our N-tier application quit often.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
An idle connection can timeout and be closed by the TCP/IP stack. Plus, it's much better practice to have your conneciton open only as long as your performing a transaction because each connection is using resources on the SQL Server as well as a connection license.
RageInTheMachine9532
|
|
|
|
|
This is by design. You'll need to implement the IDisposable interface. This makes it possible to perform deterministic cleanup by calling the Dispose method. Be sure to read "Implementing a Dispose Method"[^] in the documentation first. I've seen a lot of people do this incorrectly.
|
|
|
|
|
I get that too and it is really irritating me - i have just surrounded it with a try catch block and ignore it - I couldn't solve it and I got to the point where I just didn't care - I am not happy with ADO.NET at all - it's very poor.
Rugby League: The Greatest Game Of All.
|
|
|
|
|
Can anyone tell me how to get the system string size in C#? I know in C++ it's listed as a constanct in a header file.
Thanks
|
|
|
|
|
What do you mean the "system string"? You can use String.Length on a string instance to get how many characters there are. Internally strings are stored as Unicode, so there'll be 2 bytes per character. You can use the Encoding class to convert the encoding of the string, and can marshal these to unmanaged code by using the appropriate UnmanagedType enumeration member with the MarshalAsAttribute on parameters and fields. The TextInfo class may also be useful to enumerate code points within the string.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
About 2 gigabytes... (in bytes that is, so that's 1 Gb of DBCS (i.e. Unicode-16 (i.e. 2 bytes per character)))
I don't think you'll reach that limit quick, and if you do it might be a good time to look into some streaming classes.
Regards,
Jeroen Landheer.
|
|
|
|