User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 429,998 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,505 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 498 | Replies: 5 | Solved
Reply
Join Date: Jun 2008
Location: Pasig City, Philippines
Posts: 10
Reputation: dlayante is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
dlayante's Avatar
dlayante dlayante is offline Offline
Newbie Poster

SQL Database Problem

  #1  
Jun 20th, 2008
I have already connect my SQL database in C# windows application, Now, can somebody please give me sample codes for data manipulation, add/edit/delete of records....THANK you!
"LIFE is NOT A MATTER of CHANCE, it's a MATTER of CHOICE."
Layante, Dennis P.
Ripple E - Business International, Inc.
2202C-East Tower, Pasig City, Philippines 1605
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: � Jogja �
Posts: 2,585
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Rep Power: 11
Solved Threads: 235
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: SQL Database Problem

  #2  
Jun 20th, 2008
hmmm, how far u have done?show the code and other member can correct any mistaken..
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote  
Join Date: Jun 2008
Location: Pasig City, Philippines
Posts: 10
Reputation: dlayante is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
dlayante's Avatar
dlayante dlayante is offline Offline
Newbie Poster

Re: SQL Database Problem

  #3  
Jun 20th, 2008
Please help me...here's so far I have...I need codes for add/edit/delete records....
thanx...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string connectionString ="server=sroque\\sqlexpress;database=InventoryDB;uid=sa;pwd=ripple";

SqlConnection mySqlConnection = new SqlConnection(connectionString);

string selectString = "SELECT * FROM tblUsers";

SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = selectString;
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();

mySqlDataAdapter.SelectCommand = mySqlCommand;

DataSet myDataSet = new DataSet();

mySqlConnection.Open();

string dataTableName = "tblUsers";
mySqlDataAdapter.Fill(myDataSet, dataTableName);

DataTable myDataTable = myDataSet.Tables[dataTableName];

foreach (DataRow myDataRow in myDataTable.Rows)
{
txtId.Text = "" + myDataRow["userID"];
txtUser.Text = "" + myDataRow["Name"];
txtPosition.Text = "" + myDataRow["Position"];
}
mySqlConnection.Close();
}

private void btnAdd_Click(object sender, EventArgs e)
{
if (btnAdd.Text == "&Add")
{
txtId.Text = "";
txtUser.Text = "";
txtPosition.Text = "";
btnAdd.Text = "&Save";
}
else if (btnAdd.Text == "&Save")
{
//then here I want to save records from the textbox to my database

}
}
}
}
"LIFE is NOT A MATTER of CHANCE, it's a MATTER of CHOICE."
Layante, Dennis P.
Ripple E - Business International, Inc.
2202C-East Tower, Pasig City, Philippines 1605
Reply With Quote  
Join Date: Jun 2008
Location: Pasig City, Philippines
Posts: 10
Reputation: dlayante is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
dlayante's Avatar
dlayante dlayante is offline Offline
Newbie Poster

Re: SQL Database Problem

  #4  
Jun 20th, 2008
Please correct me if there's something wrong with my codes...It already have SQL Database connection and I successfully display my record from the database....

Please help me...here's so far I have...I need codes for data manipulation adding/edit/delete records....im using C#Windows Application

THANK YOU...you're a big help...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string connectionString ="server=sroque\\sqlexpress;database=InventoryDB;uid=sa;pwd=ripple";

SqlConnection mySqlConnection = new SqlConnection(connectionString);

string selectString = "SELECT * FROM tblUsers";

SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = selectString;
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();

mySqlDataAdapter.SelectCommand = mySqlCommand;

DataSet myDataSet = new DataSet();

mySqlConnection.Open();

string dataTableName = "tblUsers";
mySqlDataAdapter.Fill(myDataSet, dataTableName);

DataTable myDataTable = myDataSet.Tables[dataTableName];

foreach (DataRow myDataRow in myDataTable.Rows)
{
txtId.Text = "" + myDataRow["userID"];
txtUser.Text = "" + myDataRow["Name"];
txtPosition.Text = "" + myDataRow["Position"];
}
mySqlConnection.Close();
}

private void btnAdd_Click(object sender, EventArgs e)
{
if (btnAdd.Text == "&Add")
{
txtId.Text = "";
txtUser.Text = "";
txtPosition.Text = "";
btnAdd.Text = "&Save";
}
else if (btnAdd.Text == "&Save")
{
//then here I want to save records from the textbox to my database

}
}
}
}
"LIFE is NOT A MATTER of CHANCE, it's a MATTER of CHOICE."
Layante, Dennis P.
Ripple E - Business International, Inc.
2202C-East Tower, Pasig City, Philippines 1605
Reply With Quote  
Join Date: Aug 2006
Location: Egypt
Posts: 762
Reputation: RamyMahrous is on a distinguished road 
Rep Power: 4
Solved Threads: 59
Featured Poster
RamyMahrous's Avatar
RamyMahrous RamyMahrous is offline Offline
Master Poster

Re: SQL Database Problem

  #5  
Jun 20th, 2008
I can't debug your code or even read it, to make it easy, inform us with error raises when you run the code.
B.Sc Computer Science, Helwan University
Microsoft Student Partner
Personal blog http://ramymahrous.blogspot.com/
Arabic technical blog http://fci-h-ar.blogspot.com/
English technical blog http://fci-h.blogspot.com/
Reply With Quote  
Join Date: Nov 2006
Location: Bonners Ferry, ID
Posts: 280
Reputation: JerryShaw is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 39
JerryShaw JerryShaw is offline Offline
Posting Whiz in Training

Re: SQL Database Problem

  #6  
Jun 20th, 2008
You forgot to assign the SqlConnection to your adapter.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 12:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC