15,905,010 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by Member 11341510 (Top 14 by date)
Member 11341510
12-Jul-15 16:13pm
View
this is the complete code...im still not getting it :(
Member 11341510
12-Jul-15 12:40pm
View
yes you are right, I will remove the ds1 dataset:
I have created a class DatabaseConnection.cs
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testNavData
{
class DatabaseConnection
{
private string sql_String;
private string strCon;
System.Data.SqlClient.SqlDataAdapter da_1;
public string Sql
{
set { sql_String = value; }
}
public string connection_string
{
set { strCon = value; }
}
public System.Data.DataSet GetConnection
{
get { return MyDataSet(); }
}
private System.Data.DataSet MyDataSet()
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
con.Open();
da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_String, con);
System.Data.DataSet dat_set = new System.Data.DataSet();
da_1.Fill(dat_set, "Table_Data_1");
con.Close();
return dat_set;
}
public void UpdateDatabase(System.Data.DataSet ds)
{
System.Data.SqlClient.SqlCommandBuilder cb = new SqlCommandBuilder(da_1);
cb.DataAdapter.Update(ds.Tables[0]);
}
}
}
Member 11341510
12-Jul-15 11:51am
View
kindly help plz plz
Member 11341510
12-Jul-15 11:17am
View
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace testNavData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DatabaseConnection objConnect;
string conString;
DataSet ds;
DataRow dRow;
int MaxRows;
int inc = 0;
private void Form1_Load(object sender, EventArgs e)
{
DataSet ds1=new DataSet();
try
{
objConnect = new DatabaseConnection();
conString = Properties.Settings.Default.EmployeesConnectionString;
objConnect.connection_string = conString;
objConnect.Sql = Properties.Settings.Default.SQL;;
//MessageBox.Show(get{return objConnect.Sql});
ds = objConnect.GetConnection;
MaxRows = ds.Tables[0].Rows.Count;
NavigateRecords(ds);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
private void NavigateRecords(DataSet ds)
{
dRow = ds.Tables[0].Rows[inc];
txtFirstName.Text = dRow.ItemArray.GetValue(1).ToString();
txtSurname.Text = dRow.ItemArray.GetValue(2).ToString();
txtJobTitle.Text = dRow.ItemArray.GetValue(3).ToString();
txtDepartment.Text = dRow.ItemArray.GetValue(4).ToString();
}
private void btnAddNew_Click(object sender, EventArgs e)
{
txtFirstName.Clear();
txtSurname.Clear();
txtJobTitle.Clear();
txtDepartment.Clear();
btnAddNew.Enabled = false;
btnSave.Enabled = true;
btnCancel.Enabled = true;
}
private void btnCancel_Click(object sender, EventArgs e)
{
NavigateRecords(ds);
btnCancel.Enabled = false;
btnAddNew.Enabled = true;
btnSave.Enabled = false;
}
private void btnSave_Click(object sender, EventArgs e)
{
DataRow row = ds.Tables[0].NewRow();
row[1] = txtFirstName .Text;
row[2] = txtSurname.Text;
row[3] = txtJobTitle.Text;
row[4] = txtDepartment.Text;
ds.Tables[0].Rows.Add(row);
try
{
objConnect.UpdateDatabase(ds);
MaxRows = MaxRows + 1;
inc = MaxRows - 1;
this.ds.Tables[0].Clear();
MessageBox.Show("Database is Updated");
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
btnCancel.Enabled = false;
btnSave.Enabled = false;
btnAddNew.Enabled = true;
}
public string Sql { get; set; }
private void btnNext_Click(object sender, EventArgs e)
{
if (inc != (MaxRows-1))
{
inc= inc+1;
NavigateRecords(ds);
}
else
{
if (inc != (MaxRows - 1))
{
inc = (MaxRows-1);
NavigateRecords(ds);
}
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
if (inc > 0)
{
inc= inc-1;
NavigateRecords(ds);
}
else
{
if (inc != 0)
{
inc = 0;
NavigateRecords(ds);
}
}
}
private void btnLast_Click(object sender, EventArgs e)
{
if (inc != M
Member 11341510
12-Jul-15 0:30am
View
There is one more thing I am not clear about. How to work this out in the above code...
MessageBox.Show(objConnect.Sql);
Member 11341510
11-Jul-15 16:13pm
View
Yes...Thank u a ton for your guidance :)
Member 11341510
11-Jul-15 16:03pm
View
Wow...it's working fine now. Can you please expain how 'this' works
Member 11341510
11-Jul-15 15:59pm
View
yes the error is occuring on that line itself.
Member 11341510
11-Jul-15 15:55pm
View
The first time the NavigateRecords function is working fine and the textboxes are populated. I think it has to do with the scope of ds dataset arguement that is passed. But don't know how to solve it.
Member 11341510
11-Jul-15 15:53pm
View
5(I have 5 records so the value is correct)
Member 11341510
11-Jul-15 15:43pm
View
I have used a separate class for it:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testNavData
{
class DatabaseConnection
{
private string sql_String;
private string strCon;
System.Data.SqlClient.SqlDataAdapter da_1;
public string Sql
{
set { sql_String = value; }
}
public string connection_string
{
set { strCon = value; }
}
public System.Data.DataSet GetConnection
{
get { return MyDataSet(); }
}
private System.Data.DataSet MyDataSet()
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
con.Open();
da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_String, con);
System.Data.DataSet dat_set = new System.Data.DataSet();
da_1.Fill(dat_set, "Table_Data_1");
con.Close();
return dat_set;
}
public void UpdateDatabase(System.Data.DataSet ds)
{
System.Data.SqlClient.SqlCommandBuilder cb = new SqlCommandBuilder(da_1);
cb.DataAdapter.Update(ds.Tables[0]);
}
}
}
Also I rephrased the question...If u can please check once...
Member 11341510
8-Jun-15 9:45am
View
Thank u Staffan! it is now clear to me. Your answer was of great help.
Member 11341510
5-Jun-15 13:44pm
View
thank you! the soln worked. But it is still not clear to me why it is skipping the condition when textbox.text is not the same as the chk variable. I am a new to c# coding. If could please explain it.
Member 11341510
5-Jun-15 13:38pm
View
Deleted
Thank u!
Show More