Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Pure VPN - Up to 27 Months
vgp
Updated:
Today at 8:10 AM
එක පැකේජ් එකයි මාසෙටම Unlimited Internet. තාමත් DATA CARD දාන්න සල්ලි වියදම් කරනවද? අඩුම මිලට අපෙන්.
sayuru bandara
Updated:
Tuesday at 12:30 PM
Ad icon
ඉන්ටර්නෙට් එකෙන් හරියටම සල්ලි හොයන්න සහ Success වෙන්න කැමතිද? 🚀 (E-Money & Success Stories)
siri sumana
Updated:
Saturday at 11:44 PM
Gemini AI PRO 18 months Offer
Hawaka
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Education
C# and SQL
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="Core" data-source="post: 10769205" data-attributes="member: 263471"><p>[CODE]</p><p>.CS File</p><p></p><p>using System;</p><p>using System.Collections.Generic;</p><p>using System.ComponentModel;</p><p>using System.Data;</p><p>using System.Drawing;</p><p>using System.Text;</p><p>using System.Windows.Forms;</p><p>using System.IO;</p><p>using System.Data.SqlClient;</p><p></p><p>namespace DataBase_UserDetails</p><p>{</p><p> public partial class Form1 : Form</p><p> {</p><p> string connectionString;</p><p></p><p> public Form1()</p><p> {</p><p> InitializeComponent();</p><p> connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=Database1 - Copy.mdf;Integrated Security=True;User Instance=True";</p><p> }</p><p></p><p> //Create a New Table</p><p> private void btnCreate_Click(object sender, EventArgs e)</p><p> {</p><p> SqlConnection SqlCon = new SqlConnection(connectionString);</p><p> SqlCon.Open(); //open the connection</p><p> string commandStr = "CREATE TABLE users("+</p><p> "user_id INT NOT NULL," +</p><p> "user_name VARCHAR(40) NOT NULL," +</p><p> "user_email VARCHAR(40) NOT NULL," +</p><p> "user_pass VARCHAR(40) NOT NULL," +</p><p> "user_registration_date DATETIME NOT NULL," +</p><p> "user_state VARCHAR(10) NOT NULL," +</p><p> "PRIMARY KEY(user_id))";</p><p> SqlCommand cmd = new SqlCommand(commandStr, SqlCon);</p><p></p><p> try</p><p> {</p><p> cmd.ExecuteNonQuery();</p><p> SqlCon.Close();</p><p> </p><p> }</p><p> catch (Exception error)</p><p> {</p><p> MessageBox.Show(error.Message, "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);</p><p> }</p><p></p><p></p><p> }</p><p></p><p> //Create a DataBase</p><p> private void btn_newDB_Click(object sender, EventArgs e)</p><p> {</p><p> string commandStr = "CREATE DATABASE Core";</p><p> SqlCommand cmd = new SqlCommand(commandStr);</p><p> try</p><p> {</p><p> cmd.ExecuteNonQuery();</p><p> }</p><p> catch (Exception error)</p><p> {</p><p> MessageBox.Show(error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);</p><p> }</p><p> }</p><p></p><p> //create new field</p><p> private void btnInsert_Click(object sender, EventArgs e)</p><p> {</p><p> SqlConnection con = new SqlConnection(connectionString);</p><p> con.Open(); //open the connection</p><p> string commandStr = "INSERT INTO users(user_id, user_name, user_email, user_pass, user_registration_date, user_state)" +</p><p> " VALUES(@userID,@userName,@email,@userPass,@date,@userState)";</p><p></p><p> SqlCommand cmd = new SqlCommand(commandStr, con);//create an object from the sqlcommand</p><p> cmd.Parameters.AddWithValue("@userID", txtID.Text);</p><p> cmd.Parameters.AddWithValue("@userName", txtName.Text);</p><p> cmd.Parameters.AddWithValue("@email", txtEmail.Text);</p><p> cmd.Parameters.AddWithValue("@userPass", txtPass.Text);</p><p> cmd.Parameters.AddWithValue("@date", DateTime.Now);</p><p> cmd.Parameters.AddWithValue("@userState", txtState.Text);</p><p></p><p> try</p><p> {</p><p> cmd.ExecuteNonQuery();</p><p> con.Close();</p><p> }</p><p> catch (Exception error)</p><p> {</p><p> MessageBox.Show(error.Message);</p><p> }</p><p> }</p><p></p><p> //UPDATE</p><p> private void btnUpdate_Click(object sender, EventArgs e)</p><p> {</p><p> SqlConnection con = new SqlConnection(connectionString);</p><p> con.Open();//open the connection between the database and the application</p><p> string commandString = "UPDATE users SET "+</p><p> "user_name=@userName,user_email=@email,user_pass=@userPass,"+</p><p> "user_registration_date=@date,user_state=@userState WHERE user_id=@selectedID";</p><p></p><p> string selectedID = treeView1.SelectedNode.Text;//get the selected index value</p><p> SqlCommand cmd = new SqlCommand(commandString, con);//make the SQL command object</p><p> cmd.Parameters.AddWithValue("@selectedID", selectedID);//replace the @selectedId with selected value on treenode</p><p> //get the selected index value from the treeview </p><p> //and then replace that with SQL string command</p><p> //therefore the condition is id everything related that ID will be updated.</p><p> //following fields will replace the txt boxes with SQL command parameters</p><p> cmd.Parameters.AddWithValue("@userName",txtName.Text);</p><p> cmd.Parameters.AddWithValue("@email",txtEmail.Text);</p><p> cmd.Parameters.AddWithValue("@userPass",txtPass.Text);</p><p> cmd.Parameters.AddWithValue("@date",DateTime.Now);</p><p> cmd.Parameters.AddWithValue("@userState",txtState.Text);</p><p></p><p> try</p><p> {</p><p> cmd.ExecuteNonQuery();//execute the command</p><p> con.Close();</p><p> }</p><p> catch (Exception error)</p><p> {</p><p> MessageBox.Show(error.Message);</p><p> }</p><p></p><p> }</p><p></p><p> //SELECT ALL</p><p> private void btnSelectAll_Click(object sender, EventArgs e)</p><p> {</p><p> treeView1.Nodes.Clear();//clear all nodes if there are any remaining</p><p> SqlConnection con = new SqlConnection(connectionString);</p><p> con.Open();</p><p> string commandString = "SELECT * FROM users";//get everything from the users table</p><p> SqlCommand cmd = new SqlCommand(commandString, con);</p><p> SqlDataReader reader;</p><p></p><p> try</p><p> {</p><p> reader = cmd.ExecuteReader();</p><p> </p><p> while (reader.Read())</p><p> {</p><p> TreeNode mainNode = treeView1.Nodes.Add(reader["user_id"].ToString());</p><p> mainNode.Nodes.Add(reader["user_name"].ToString());</p><p> mainNode.Nodes.Add(reader["user_email"].ToString());</p><p> mainNode.Nodes.Add(reader["user_pass"].ToString());</p><p> mainNode.Nodes.Add(reader["user_registration_date"].ToString());</p><p> mainNode.Nodes.Add(reader["user_state"].ToString());</p><p> }</p><p> }</p><p> catch (Exception error)</p><p> {</p><p> MessageBox.Show(error.Message);</p><p> }</p><p></p><p> }</p><p></p><p> //Delete data Field</p><p> private void btnDelete_Click(object sender, EventArgs e)</p><p> {</p><p> SqlConnection con = new SqlConnection(connectionString);</p><p> con.Open();</p><p> string commandStr = "DELETE FROM users WHERE user_id=@userID";//just select the primary key so it will delete the entire row</p><p> SqlCommand cmd = new SqlCommand(commandStr, con);</p><p> cmd.Parameters.AddWithValue("userID", treeView1.SelectedNode.Text);</p><p></p><p> try</p><p> {</p><p> cmd.ExecuteNonQuery();</p><p> con.Close();</p><p> }</p><p> catch (Exception error)</p><p> {</p><p> MessageBox.Show(error.Message);</p><p> }</p><p> }</p><p></p><p> //Select the TreeNode</p><p> private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)</p><p> {</p><p> TreeNode indexNode = treeView1.SelectedNode;//get the selected node</p><p> txtID.Text = indexNode.Text;//get the index id</p><p> txtName.Text = indexNode.Nodes[0].Text;</p><p> txtEmail.Text = indexNode.Nodes[1].Text;</p><p> txtPass.Text = indexNode.Nodes[2].Text;</p><p> txtDate.Text = indexNode.Nodes[3].Text;</p><p> txtState.Text = indexNode.Nodes[4].Text;</p><p> }</p><p></p><p> //search Keyword</p><p> //IN SQL SERVER EXPREE DON"T USE '' SINGLE QUOTATION MARKS OR % WHEN COMPARE LIKE OR NOT LIKE</p><p> //NEARLY EQUALITY</p><p> private void btnSearch_Click(object sender, EventArgs e)</p><p> {</p><p> /*</p><p> * user_name=@userName,user_email=@email,user_pass=@userPass,"+</p><p> "user_registration_date=@date,user_state=@userState</p><p> */</p><p> SqlConnection con = new SqlConnection(connectionString);</p><p> con.Open();</p><p> string commandStr = "SELECT * FROM users WHERE (user_name LIKE @userName) OR" +</p><p> "(user_email LIKE @userEmail) OR (user_pass LIKE @userPass) OR " +</p><p> "(user_registration_date LIKE @userReg) OR (user_state LIKE @userState)";</p><p></p><p> SqlCommand cmd = new SqlCommand(commandStr, con);</p><p> //replace the parameters with the C# components</p><p> cmd.Parameters.AddWithValue("@userName", txtSearchKey.Text);</p><p> cmd.Parameters.AddWithValue("@userEmail", txtSearchKey.Text);</p><p> cmd.Parameters.AddWithValue("@userPass", txtSearchKey.Text);</p><p> cmd.Parameters.AddWithValue("@userReg", txtSearchKey.Text);</p><p> cmd.Parameters.AddWithValue("@userState", txtSearchKey.Text);</p><p></p><p> try</p><p> {</p><p> SqlDataReader reader = cmd.ExecuteReader(); ;</p><p> while (reader.Read())//read the next record if non of record found return 0</p><p> {</p><p> TreeNode mainNode = treeView1.Nodes.Add(reader["user_id"].ToString());</p><p> mainNode.Nodes.Add(reader["user_name"].ToString());</p><p> mainNode.Nodes.Add(reader["user_email"].ToString());</p><p> mainNode.Nodes.Add(reader["user_pass"].ToString());</p><p> mainNode.Nodes.Add(reader["user_registration_date"].ToString());</p><p> mainNode.Nodes.Add(reader["user_state"].ToString());</p><p> }</p><p> }</p><p> catch (Exception error)</p><p> {</p><p> MessageBox.Show(error.Message);</p><p> }</p><p> </p><p> }</p><p></p><p> private void btnClear_Click(object sender, EventArgs e)</p><p> {</p><p> treeView1.Nodes.Clear();</p><p> }</p><p></p><p> }</p><p>}[/CODE]<strong>Instructions </strong></p><p><strong>First add a Server SQL Database into the solution explorer and just proceed with above codes.!</strong></p><p></p><p></p><p></p><p><strong><span style="color: Red">Copyright Notice</span></strong></p><p><strong><span style="color: Red">All Source Codes belong to Core and don't mess with my codes without my permission.</span></strong></p><p>I made this on another forum which I can't mention in here. <img src="http://www.elakiri.com/forum/images/icons/sq/7.gif" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p>but thought make the same thing in here is useful.</p></blockquote><p></p>
[QUOTE="Core, post: 10769205, member: 263471"] [CODE] .CS File using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Data.SqlClient; namespace DataBase_UserDetails { public partial class Form1 : Form { string connectionString; public Form1() { InitializeComponent(); connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=Database1 - Copy.mdf;Integrated Security=True;User Instance=True"; } //Create a New Table private void btnCreate_Click(object sender, EventArgs e) { SqlConnection SqlCon = new SqlConnection(connectionString); SqlCon.Open(); //open the connection string commandStr = "CREATE TABLE users("+ "user_id INT NOT NULL," + "user_name VARCHAR(40) NOT NULL," + "user_email VARCHAR(40) NOT NULL," + "user_pass VARCHAR(40) NOT NULL," + "user_registration_date DATETIME NOT NULL," + "user_state VARCHAR(10) NOT NULL," + "PRIMARY KEY(user_id))"; SqlCommand cmd = new SqlCommand(commandStr, SqlCon); try { cmd.ExecuteNonQuery(); SqlCon.Close(); } catch (Exception error) { MessageBox.Show(error.Message, "Error",MessageBoxButtons.OK,MessageBoxIcon.Error); } } //Create a DataBase private void btn_newDB_Click(object sender, EventArgs e) { string commandStr = "CREATE DATABASE Core"; SqlCommand cmd = new SqlCommand(commandStr); try { cmd.ExecuteNonQuery(); } catch (Exception error) { MessageBox.Show(error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //create new field private void btnInsert_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(connectionString); con.Open(); //open the connection string commandStr = "INSERT INTO users(user_id, user_name, user_email, user_pass, user_registration_date, user_state)" + " VALUES(@userID,@userName,@email,@userPass,@date,@userState)"; SqlCommand cmd = new SqlCommand(commandStr, con);//create an object from the sqlcommand cmd.Parameters.AddWithValue("@userID", txtID.Text); cmd.Parameters.AddWithValue("@userName", txtName.Text); cmd.Parameters.AddWithValue("@email", txtEmail.Text); cmd.Parameters.AddWithValue("@userPass", txtPass.Text); cmd.Parameters.AddWithValue("@date", DateTime.Now); cmd.Parameters.AddWithValue("@userState", txtState.Text); try { cmd.ExecuteNonQuery(); con.Close(); } catch (Exception error) { MessageBox.Show(error.Message); } } //UPDATE private void btnUpdate_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(connectionString); con.Open();//open the connection between the database and the application string commandString = "UPDATE users SET "+ "user_name=@userName,user_email=@email,user_pass=@userPass,"+ "user_registration_date=@date,user_state=@userState WHERE user_id=@selectedID"; string selectedID = treeView1.SelectedNode.Text;//get the selected index value SqlCommand cmd = new SqlCommand(commandString, con);//make the SQL command object cmd.Parameters.AddWithValue("@selectedID", selectedID);//replace the @selectedId with selected value on treenode //get the selected index value from the treeview //and then replace that with SQL string command //therefore the condition is id everything related that ID will be updated. //following fields will replace the txt boxes with SQL command parameters cmd.Parameters.AddWithValue("@userName",txtName.Text); cmd.Parameters.AddWithValue("@email",txtEmail.Text); cmd.Parameters.AddWithValue("@userPass",txtPass.Text); cmd.Parameters.AddWithValue("@date",DateTime.Now); cmd.Parameters.AddWithValue("@userState",txtState.Text); try { cmd.ExecuteNonQuery();//execute the command con.Close(); } catch (Exception error) { MessageBox.Show(error.Message); } } //SELECT ALL private void btnSelectAll_Click(object sender, EventArgs e) { treeView1.Nodes.Clear();//clear all nodes if there are any remaining SqlConnection con = new SqlConnection(connectionString); con.Open(); string commandString = "SELECT * FROM users";//get everything from the users table SqlCommand cmd = new SqlCommand(commandString, con); SqlDataReader reader; try { reader = cmd.ExecuteReader(); while (reader.Read()) { TreeNode mainNode = treeView1.Nodes.Add(reader["user_id"].ToString()); mainNode.Nodes.Add(reader["user_name"].ToString()); mainNode.Nodes.Add(reader["user_email"].ToString()); mainNode.Nodes.Add(reader["user_pass"].ToString()); mainNode.Nodes.Add(reader["user_registration_date"].ToString()); mainNode.Nodes.Add(reader["user_state"].ToString()); } } catch (Exception error) { MessageBox.Show(error.Message); } } //Delete data Field private void btnDelete_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(connectionString); con.Open(); string commandStr = "DELETE FROM users WHERE user_id=@userID";//just select the primary key so it will delete the entire row SqlCommand cmd = new SqlCommand(commandStr, con); cmd.Parameters.AddWithValue("userID", treeView1.SelectedNode.Text); try { cmd.ExecuteNonQuery(); con.Close(); } catch (Exception error) { MessageBox.Show(error.Message); } } //Select the TreeNode private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { TreeNode indexNode = treeView1.SelectedNode;//get the selected node txtID.Text = indexNode.Text;//get the index id txtName.Text = indexNode.Nodes[0].Text; txtEmail.Text = indexNode.Nodes[1].Text; txtPass.Text = indexNode.Nodes[2].Text; txtDate.Text = indexNode.Nodes[3].Text; txtState.Text = indexNode.Nodes[4].Text; } //search Keyword //IN SQL SERVER EXPREE DON"T USE '' SINGLE QUOTATION MARKS OR % WHEN COMPARE LIKE OR NOT LIKE //NEARLY EQUALITY private void btnSearch_Click(object sender, EventArgs e) { /* * user_name=@userName,user_email=@email,user_pass=@userPass,"+ "user_registration_date=@date,user_state=@userState */ SqlConnection con = new SqlConnection(connectionString); con.Open(); string commandStr = "SELECT * FROM users WHERE (user_name LIKE @userName) OR" + "(user_email LIKE @userEmail) OR (user_pass LIKE @userPass) OR " + "(user_registration_date LIKE @userReg) OR (user_state LIKE @userState)"; SqlCommand cmd = new SqlCommand(commandStr, con); //replace the parameters with the C# components cmd.Parameters.AddWithValue("@userName", txtSearchKey.Text); cmd.Parameters.AddWithValue("@userEmail", txtSearchKey.Text); cmd.Parameters.AddWithValue("@userPass", txtSearchKey.Text); cmd.Parameters.AddWithValue("@userReg", txtSearchKey.Text); cmd.Parameters.AddWithValue("@userState", txtSearchKey.Text); try { SqlDataReader reader = cmd.ExecuteReader(); ; while (reader.Read())//read the next record if non of record found return 0 { TreeNode mainNode = treeView1.Nodes.Add(reader["user_id"].ToString()); mainNode.Nodes.Add(reader["user_name"].ToString()); mainNode.Nodes.Add(reader["user_email"].ToString()); mainNode.Nodes.Add(reader["user_pass"].ToString()); mainNode.Nodes.Add(reader["user_registration_date"].ToString()); mainNode.Nodes.Add(reader["user_state"].ToString()); } } catch (Exception error) { MessageBox.Show(error.Message); } } private void btnClear_Click(object sender, EventArgs e) { treeView1.Nodes.Clear(); } } }[/CODE][B]Instructions First add a Server SQL Database into the solution explorer and just proceed with above codes.![/B] [B][COLOR=Red]Copyright Notice All Source Codes belong to Core and don't mess with my codes without my permission.[/COLOR][/B] I made this on another forum which I can't mention in here. [IMG]http://www.elakiri.com/forum/images/icons/sq/7.gif[/IMG] but thought make the same thing in here is useful. [/QUOTE]
Insert quotes…
Verification
Dawasata paya keeyak thibeda?
Post reply
Top
Bottom