sqldatareader textbox [Resolved]

Posted by Anand under C# on 2/22/2012 | Points: 10 | Views : 2906 | Status : [Member] | Replies : 6
i am trying to fetch next record using next button but i am getting error i am using sqldatareader to achive this i am getting an error reader is not closed i have closed the reader but still then the next record is not fetched into thte textboxes

using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace datareadery
{
public partial class Form1 : Form
{
MySqlDataReader reader;

MySqlCommand cmd;


MySqlConnection con;
public Form1()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
con = new MySqlConnection(@"server=localhost;User Id=root;database=employee");
con.Open();
cmd = new MySqlCommand("select id ,first_name,last_name,start_date from employee", con);

reader = cmd.ExecuteReader();
call();
}
public void call()
{

while (reader.Read())
{
textBox1.Text = reader[0].ToString();
textBox2.Text = reader[1].ToString();
textBox3.Text = reader[2].ToString();
textBox4.Text = reader[3].ToString();

break;

}
}
public void clear()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
}
private void btnnext_Click(object sender, EventArgs e)
{
call();
con.Close();
}



private void btnclear_Click(object sender, EventArgs e)
{
clear();
}
}
}

-


Responses

Posted by: Anand on: 2/22/2012 [Member] Starter | Points: 25

Up
0
Down

Resolved
hey avdesh i got the out put properly

using System;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
namespace datareadery
{
public partial class Form1 : Form
{
MySqlDataReader reader;

MySqlCommand cmd;

MySqlConnection con;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
con = new MySqlConnection(@"server=localhost;User Id=root;database=employee");
con.Open();
cmd = new MySqlCommand("select id ,first_name,last_name,start_date from employee", con);

if (reader == null || reader.IsClosed)
reader = cmd.ExecuteReader();
call();
}
public void call()
{

while (reader.Read())
{
textBox1.Text = reader[0].ToString();
textBox2.Text = reader[1].ToString();
textBox3.Text = reader[2].ToString();
textBox4.Text = reader[3].ToString();
break;

}
}
public void clear()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
}
private void btnnext_Click(object sender, EventArgs e)
{
//call();
//con.Close();
}
private void btnclear_Click(object sender, EventArgs e)
{
clear();
}
}
}







-

Anand, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 2/22/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi Aanand,

Datareader are connection oriented forward only type of Data Variable.
you can't forward or rewind it. for that either use datatable or dataset

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Anand, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Avdesh on: 2/22/2012 [Member] Starter | Points: 25

Up
0
Down
TRY THIS
MAY BE THIS WILL WORK
using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace datareadery
{
public partial class Form1 : Form
{
MySqlDataReader reader;

MySqlCommand cmd;


MySqlConnection con;
public Form1()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
con = new MySqlConnection(@"server=localhost;User Id=root;database=employee");
}
private void button1_Click(object sender, EventArgs e)
{


cmd = new MySqlCommand("select id ,first_name,last_name,start_date from employee", con);
con.Open();

reader = cmd.ExecuteReader();
call();
}
public void call()
{

while (reader.Read())
{
textBox1.Text = reader[0].ToString();
textBox2.Text = reader[1].ToString();
textBox3.Text = reader[2].ToString();
textBox4.Text = reader[3].ToString();

break;

}
}
public void clear()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
}
private void btnnext_Click(object sender, EventArgs e)
{
call();
con.Close();
}



private void btnclear_Click(object sender, EventArgs e)
{
clear();
}
}
}


THANKS AND REGARDS
AVDESH

Anand, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anand on: 2/22/2012 [Member] Starter | Points: 25

Up
0
Down
it works but it is showing only two records not the third one


-

Anand, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anand on: 2/22/2012 [Member] Starter | Points: 25

Up
0
Down
after that error

-

Anand, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Avdesh on: 2/22/2012 [Member] Starter | Points: 25

Up
0
Down
IF CODE HELPS LOGIN TO MARK AS ANSWER
THANKS AND REGARDS
AVDESH

Anand, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response