Click here to Skip to main content
16,019,349 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
am trying to get my username n password saved in SQL Database but it doesnt work.

What I have tried:

i have tried using the following codes
<pre> private void btnLogin_Click(object sender, EventArgs e)
        {

            SqlConnection cs = new SqlConnection("Data Source =Tyiselani-HP, Initial Catalog =Customers, Intergrated Security = TRUE");
            SqlDataAdapter dc = new SqlDataAdapter();
            dc.InsertCommand = new SqlCommand("INSERT INTO Login1 VALUES (@Username,@Password)", cs);
            dc.InsertCommand.Parameters.Add("@Username", SqlDbType.VarChar).Value = Txtusername.Text;
            dc.InsertCommand.Parameters.Add("@Password", SqlDbType.VarChar).Value = Txtpassword.Text;
            
            cs.Open();
            dc.InsertCommand.ExecuteNonQuery();
            cs.Close();
Posted
Comments
vivvicks 20-Feb-18 6:50am    
whats is the exception u r getting?

1 solution

Loads of problems here, none of them directly related to your problem...
1) Never hard code connection strings: use a config file (or at the very least a const string so it only needs to be entered in one place).
2) Connections, Commands, and so forth as scarce resources - you need to Dispose them properly when complete. A using block is the simplest way to do that.
3) Don't use a DataAdapter for a simple one line insert: it's very wasteful. Generate a stand-alone SqlCommand object and use that instead.
4) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

Fix those, and your problem will probably vanish at the same time...
 
Share this answer
 

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