This is my code as following
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 FleetManagment
{
public partial class Driver : Form
{
string value1;
string value2;
string value3;
string value4;
private int currrecord=0;
private int totalrecord=0;
private bool insertselected;
DataTable dt = new DataTable();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
SqlConnection con = new SqlConnection();
public Driver(string str1,string str2,string str3,string str4)
{
InitializeComponent();
value1 = str1;
value2 = str2;
value3 = str3;
value4 = str4;
}
private void fillcontrol()
{
textBox1.Text = dt.Rows[currrecord]["DriverID"].ToString();
textBox2.Text = dt.Rows[currrecord]["DriverName"].ToString();
textBox3.Text = dt.Rows[currrecord]["Nationality"].ToString();
textBox4.Text = dt.Rows[currrecord]["Address"].ToString();
}
private void Driver_Load(object sender, EventArgs e)
{
string constr = "Data Source=" + value1 + ";Initial Catalog=" + value2 + ";User ID=" + value3 + ";Password=" + value4 + "";
SqlConnection con = new SqlConnection(constr);
string comdstr = "select * from Driver";
SqlDataAdapter da = new SqlDataAdapter(comdstr, constr);
DataSet ds = new DataSet();
da.Fill(ds, "Driver");
dt = new DataTable();
dt = ds.Tables["Driver"];
currrecord = 0;
totalrecord = dt.Rows.Count;
fillcontrol();
}
private void NextBtn_Click(object sender, EventArgs e)
{
if (ds != null)
{
currrecord++;
if (currrecord >= totalrecord)
{
currrecord = totalrecord - 1;
}
fillcontrol();
label5.Text = currrecord + "of" + totalrecord;
}
}
private void buttoninsert_Click(object sender, EventArgs e)
{
}
}
}
I have driver table this is my fields as following
DriverID INT
DriverName nvarchar50
Nationality nvarchar50
Address nvarchar50
I have form driver have 4 texbox
textbox1 DriverID
textbox2 DriverName
textbox3 Nationality
textbox4 address
this table have two records
when i press buton next (NextBtn_Click)to go third record it not accept
I need next button increase by 1 if record not exist and update this in table
How i do this
example
if i have two records
1 aln american newyork
2 adam british british
when i press next button it ok work in records exist but when i press next button to third record it not accept why
what i need is when press next after 2 it come 3 in text box driver id and update this number in table
How i do that
please help me
Loading
SUNIL GUTTAPosted Jan 29, 2014, 1:09 PM
----------------------------PLEASE RATE THE EFFORTS-------------------------------------
ahmed saPosted Jan 29, 2014, 5:56 AM
SUNIL GUTTAPosted Jan 29, 2014, 2:24 AM
ahmed saPosted Jan 28, 2014, 3:44 PM
SUNIL GUTTAPosted Jan 28, 2014, 9:08 AM
//HERE I USED DATAREADER WHICH WILL BE COOL & FAST .. NO NEED OF DS AS YOU ARE NOT MODIFYING ANYTHING ..
UNDER ELSE of next button_click Try or complely change your code use DataReader ...
textbox1.text="";textbox2.text="";textbox3.text="";
textbox4.text="";
cmd.commandtext="select IsNull(max(driverid),1)+1"; // auto generating driverid
textbox1.text = cmd.executescalar().tostring();
con.close();
//writing insert stmt inclusive as u dont have 3rd record etc ...
con.open();
string str = insert into tablename(driverid,drivername,nationality,address) into({0},'{1}','{2}','{3}'),textbox1.text,textbox2.text,textbox3.text,textbox4.text);
cmd.commandtext=str ;
cmd.executenonquery();
}
Code looks complex but very simple to understand .. take some useful things and implement the way you need
--------------------------------MARK THE ANSWER AS HELPFUL -------------------------
ahmed saPosted Jan 28, 2014, 8:11 AM
ahmed saPosted Jan 28, 2014, 8:09 AM
SUNIL GUTTAPosted Jan 28, 2014, 6:03 AM
int rno = 0;
UNDER NEXT BUTTON CLICK :
IF(rno < ds.tables[0].rows.count-1)
{
rno +=1 ;
if (ds.tables[0].rows[rno].rowstate == datarowstate.deleted )
{
messagebox.show("deleted row can't be accessed");
return ;
}
showdata(); ------------------------> this points to method which fetches data to textboxes from DB .
ex : textbox1.text=ds.tables[0].rows[rno][0].Tostring(); like this implement for any number of textboxes with incrementing column value
}
else
{
messagebox.show("last record of the table")
}
SUNIL GUTTAPosted Jan 28, 2014, 5:42 AM
What i understand is you are unable to move to next record when you click next ?? Am i getting you right ? if not please help me to understand ..
(OR)
If 2nd record is deleted just in case you need to move to 3rd record ha if exists ? ? later update back .
Regards