Click to See Complete Forum and Search --> : Databinding textbox


pre_wreck
April 10th, 2007, 03:02 AM
Kindly help me please.

I'm having some trouble in my code and I'm stack with this error.

// error: An unhandled exception of type 'System.IndexOutOfRangeException' occurred in system.data.dll

// Additional information: There is no row at position 0.

Here's the code.



private void tbSearchOption_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{

long totalRow = 0;

//Set the Data Adapter

OleDbDataAdapter da = new OleDbDataAdapter("SELECT qryStudentInfo.StudentNo, qryStudentInfo.Name, qryStudentInfo.Degree FROM qryStudentInfo WHERE qryStudentInfo.StudentNo LIKE '" + txtSearch + "' ",mdlConnection.conn);

DataSet ds = new DataSet();
da.Fill(ds,"qryStudentInfo");

totalRow = ds.Tables["qryStudentInfo"].Rows.Count -1;

if(e.Button == tbSearch)
{
if (txtSearch.Text == "")
MessageBox.Show("Please select a search field!", "Empty search field", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
{
if (cboSearchBy.SelectedIndex == 0)
{
txtStudID.DataBindings.Clear();
txtStudID.Text = ds.Tables["qryStudentInfo"].Rows[0].ItemArray.GetValue(0).ToString();

// error: An unhandled exception of type 'System.IndexOutOfRangeException' occurred in system.data.dll

// Additional information: There is no row at position 0.

}
else
{
dr = mdlProd.FillStudentsSearchBy(cboSearchBy, txtSearch.Text);
mdlFunc.FillRecords(lvStudents,dr);
}
}

}
else if(e.Button == tbBrowse)
{
MessageBox.Show("Reset!", "Reset...", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

tiago.teixeira
April 10th, 2007, 06:44 AM
Hi,

I think the SQL syntax is wrong and won't return any records.
I think in the like sql syntax you need to put an % symbol and i suggest to use the 'using' keyword also, to dispose the object when it is no more needed, like this:



using( OleDbDataAdapter da = new OleDbDataAdapter("SELECT qryStudentInfo.StudentNo, qryStudentInfo.Name, qryStudentInfo.Degree FROM qryStudentInfo WHERE qryStudentInfo.StudentNo LIKE '%" + txtSearch + "%' ",mdlConnection.conn))
{
//The rest of the code
}


you may use '%keyword%' or 'keyword%' or '%keyword'

I hope this can solve your problem.

Regards,
Teixeira

pre_wreck
April 10th, 2007, 06:55 AM
My friend I still got same error.

Hi,

I think the SQL syntax is wrong and won't return any records.
I think in the like sql syntax you need to put an % symbol and i suggest to use the 'using' keyword also, to dispose the object when it is no more needed, like this:



you may use '%keyword%' or 'keyword%' or '%keyword'

I hope this can solve your problem.

Regards,
Teixeira

tiago.teixeira
April 10th, 2007, 07:03 AM
Wich database engine are you using??


I read better your original code and i think the error is here:

totalRow = ds.Tables["qryStudentInfo"].Rows.Count -1;

ou should use totalRow = ds.Tables["qryStudentInfo"].Rows.Count ;

to don't get the outofrange exception

Regards,
Teixeira

pre_wreck
April 10th, 2007, 07:12 AM
Same error my friend.

I'm using OleDb. MS Access database to be specific.

thanks.

Wich database engine are you using??


I read better your original code and i think the error is here:

totalRow = ds.Tables["qryStudentInfo"].Rows.Count -1;

ou should use totalRow = ds.Tables["qryStudentInfo"].Rows.Count ;

to don't get the outofrange exception

Regards,
Teixeira

tiago.teixeira
April 10th, 2007, 07:29 AM
as youself say // Additional information: There is no row at position 0.

it has to be the sql syntax query or you don't have any records in your table.

a tip: try step by step go into the oledbdatadptarer.selectcommand.commandtext and copy the sql statement generated and next open msaccess make a simple query and paste the sql the to see where the errror comes from.

This is a sequence and you must intercept it into the most low level you can to find the spot of the issue.

Regards,
Teixeira

pre_wreck
April 10th, 2007, 08:26 PM
Is there any other way to bind the data unto my text boxes?


as youself say // Additional information: There is no row at position 0.

it has to be the sql syntax query or you don't have any records in your table.

a tip: try step by step go into the oledbdatadptarer.selectcommand.commandtext and copy the sql statement generated and next open msaccess make a simple query and paste the sql the to see where the errror comes from.

This is a sequence and you must intercept it into the most low level you can to find the spot of the issue.

Regards,
Teixeira

aniskhan
April 11th, 2007, 01:06 AM
Use the table data from dataset's datatable collection after a small row count check.
if (ds.Tables["qryStudentInfo"].Rows.Count > 0)
{
//here use the data returned

//u can assign the data to the textbox as you did
//as well as u can bind the textbox using txtBox.databindings.add method.
}
else
MessageBox.Show("No rows found"); i would have preferred to show search results in datagrid control using its datasource property.

pre_wreck
April 11th, 2007, 03:03 AM
Thanks aniskhan. Great help once again. Keep it up. :thumb:

However, I try other way around, the way I understand it. Though I understand your code better also. Please don't get me wrong.

Again thank you.



Use the table data from dataset's datatable collection after a small row count check.
if (ds.Tables["qryStudentInfo"].Rows.Count > 0)
{
//here use the data returned

//u can assign the data to the textbox as you did
//as well as u can bind the textbox using txtBox.databindings.add method.
}
else
MessageBox.Show("No rows found"); i would have preferred to show search results in datagrid control using its datasource property.