Click here to Skip to main content
16,021,181 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying this tutorial from online to add delete search and navigate a database. I have successfully made the connection. I could call the NavitegateRecords from Form_load But btnNext or btnLast such is not happening. Im getting a NullReferenceException bcoz the dRow has null value. Also just to check if correct value is passed I used a messageBox to check the content of objConnect.Sql...But this is giving an error saying "property cannot be used in this context because it lacks the get accessor"(So I not understanding how to solve it commented it out for the time being). My code is as follows:


C#
    DatabaseConnection objConnect;
    string conString;

    DataSet ds;
    DataRow dRow;
        
    int MaxRows;
    int inc = 0;
    SqlConnection cnn = new SqlConnection();
    SqlDataAdapter daAdapter = new SqlDataAdapter();
        
    private void Form1_Load(object sender, EventArgs e)
    {
        DataSet ds=new DataSet();
        String SQ;
            
        objConnect = new DatabaseConnection();
        conString = Properties.Settings.Default.EmployeesConnectionString;
        //MessageBox.Show(conString);
        objConnect.connection_string = conString;
        SQ=Properties.Settings.Default.SQL;
        objConnect.Sql = SQ;
        MessageBox.Show(objConnect.Sql);
        ds = objConnect.GetConnection;
                
        MaxRows = ds.Tables[0].Rows.Count;
        MessageBox.Show(MaxRows.ToString());
        try
        {
            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 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);
            }
            MessageBox.Show("No More Rows");
        }
    }

    private void btnPrevious_Click(object sender, EventArgs e)
    {
        if (inc > 0)
        {
           inc= inc-1;
           NavigateRecords(ds);
        }
        else
        {
            if (inc != 0)
            {
                inc = 0;
                NavigateRecords(ds);
            }
            MessageBox.Show("First Record");
        }
    }

    private void btnLast_Click(object sender, EventArgs e)
    {
        if (inc != MaxRows - 1)
        {
            inc = MaxRows - 1;
            NavigateRecords(ds);
        }
    }

    private void btnFirst_Click(object sender, EventArgs e)
    {
        if (inc != 0)
        {
            inc = 0;
            NavigateRecords(ds);
        }
    }
Posted
Updated 11-Jul-15 9:54am
v6

1 solution

From the code you have provided as far as I can see you don't populate the dataset anywhere. You do set the SQL statement and the connection and so on but I can't find a call to Fill[^] method anywhere. If this is true there's no data fetched from the database thus leaving your data row non existent.
 
Share this answer
 
Comments
Member 11341510 11-Jul-15 15:43pm    
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...
Wendelius 11-Jul-15 15:49pm    
If you debug the program, what is the value of the MaxRows in
MaxRows = ds.Tables[0].Rows.Count;
Member 11341510 11-Jul-15 15:53pm    
5(I have 5 records so the value is correct)
Wendelius 11-Jul-15 15:56pm    
Is the error occurring on this row:

dRow = ds.Tables[0].Rows[inc];

If it is, place a breakpoint on that row and check, what is the value of inc If the error occurs on later rows, still check the value of inc.
Member 11341510 11-Jul-15 15:55pm    
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900