Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody i have to make a program where i can Edit user, delete user, search user, create user, but i dont know how to make the Edit and Search code .. This is Where i got so far ..

C#
//create user
 private void button3_Click(object sender, EventArgs e)
 {
     if (!string.IsNullOrWhiteSpace(textBox4.Text))
     {
         Datahandler.dbOpen();


         string query = "SELECT * FROM Usertable WHERE (name = '" + textBox4.Text + "')";
         SqlDataReader reader = Datahandler.SqlReader(query);

         if (!reader.Read())
         {
             var md5 = new MD5CryptoServiceProvider();
             byte[] origninalBytes = ASCIIEncoding.Default.GetBytes("Lav Mig Om!!");
             byte[] md5bytes = md5.ComputeHash(origninalBytes);
             string md5string = BitConverter.ToString(md5bytes).Replace("-", "");

             reader.Close();
             Datahandler.query("INSERT INTO usertable (name, password) Values ('" + textBox4.Text + "', '" + md5string + "')");
             MessageBox.Show("Bruger Oprettet");
         }
         else
         {
             MessageBox.Show("Brugerern eksisterer allerede!");
             reader.Close();
         }
         Datahandler.dbClose();
         textBox4.Clear();
     }
     else
         MessageBox.Show("Skriv noget i tekstboksen, \nfør du prøver at lave en bruger.");
 }

 //delete user
 private void button4_Click(object sender, EventArgs e)
 {
     if (!string.IsNullOrWhiteSpace(textBox4.Text))
     {
         Datahandler.dbOpen();

         string query = "DELETE Usertable WHERE (name = '" + textBox4.Text + "')";
         Datahandler.query(query);
         Datahandler.dbClose();
     }
     else
         MessageBox.Show("Indtast et navn før du \nprøver på at slette en bruger");
 }
Posted
Updated 6-Dec-11 21:48pm
v2
Comments
[no name] 7-Dec-11 3:49am    
EDIT: removed "pre" tag

First things first.

Two changes I strongly reccommend you make:
1) Stop using the VS default names for cointrols - you make your life a lot harder. Sure, you can remember today that textBox4 is the username, and the button4 is delete user, but will you remember that next week? Change the names of the controls, and change the names of the event handlers to something that tells you what they do:
C#
textBox4      -> tbUsername
button4       -> butDeleteUser
button4_Click -> butDeleteUser_Click


2) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.


Then, Edit is a simple matter of using an SQL UPDATE[^] command instead of INSERT, and Search is what you are doing at the top of your button3_Click method: The select statement does a primitive search for the username. You may want to look at using the LIKE clause[^] though.
 
Share this answer
 
Comments
Mehdi Gholam 7-Dec-11 3:56am    
Sound advise, 5!
HI,

Define primary key column on your table. then though this unique value edit,update and search your records.

just i am giving you a example:

create table test using TESTDB database.

SQL
USE [TESTDB]
GO
/****** Object:  Table [dbo].[test]    Script Date: 12/07/2011 13:43:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[test](
	[slno] [int] NOT NULL,
	[name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[password] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED 
(
	[slno] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF 


here slno is unique just i am writing sql for update database
SQL
---Update test name='value1', password='value2' where slno= 1
same as search record from database table write this
---Select * from test where slno =1

if you understand this example you implement edit and search easily.
make to edit user only change the update query from your create user code.
 
Share this answer
 
v3

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