Hi all,
I am going to pull my hair out soon with this. I am trying to write a record to the database. As far as I know, it's being written to the DataSet and then trying to update the Db but I'm getting an error keep arising.
Object reference not set to an instance of an object.It seems that the value dRow is not being updated but I just cannot work out why.
The main screen form here; (It's probably really messy for you guys but I'm learning and trying to keep it clean!)
CODE
namespace Pok3r_1._0
{
public partial class mainScreen : Form
{
public mainScreen()
{
InitializeComponent();
}
public static System.Data.SqlClient.SqlConnection con;
public static System.Data.SqlClient.SqlDataAdapter da;
public static DataSet ds1 = new DataSet();
public static int MaxRows = 0;
public static int inc = 0;
private void btnGo_Click(object sender, EventArgs e)
{
if (radioAdd.Checked)
{
//Open add player form (addplayer.cs)
Form.ActiveForm.Hide();
add.Show();
}
else if (radioDelete.Checked)
{
//Open the delete player form (delplayer.cs)
Form.ActiveForm.Hide();
del.Show();
}
else
{
//Open the view player form (viewplayer.cs)
Form.ActiveForm.Hide();
view.Show();
}
}
private void MainScreen_Load(object sender, EventArgs e)
{
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Pok3rv1.mdf;Integrated Security=True;User Instance=True";
con.Open();
string sql = "SELECT * From player_info";
da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da.Fill(ds1, "Pok3rv1DataSet");
DataRow dRow = mainScreen.ds1.Tables["Pok3rv1DataSet"].Rows[inc];
MaxRows = ds1.Tables["Pok3rv1DataSet"].Rows.Count;
con.Close();
}
}
}
Then the Add Player screen;
CODE
namespace Pok3r_1._0
{
public partial class addplayer : Form
{
public addplayer()
{
InitializeComponent();
}
public void btnBack_Click(object sender, EventArgs e)
{
mainScreen main = new mainScreen();
//Add IF getting whether there IS any information in the text boxes
if (firstName.Text == "" && lastName.Text == "" && nickName.Text == "")
{
//Hide the form
Form.ActiveForm.Hide();
//Show the main menu
main.Show();
}
else
{
//Show a message box asking the user to confirm that they want to go back
DialogResult result = MessageBox.Show("Are you sure you want to go back? The information you have entered will not be saved", "Confirm",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
//Check the button the user clicked
if (result == DialogResult.Yes)
{
//User clicked Yes, so clear the boxes
firstName.Clear();
lastName.Clear();
nickName.Clear();
//Hide the form
Form.ActiveForm.Hide();
//Show the main menu
main.Show();
}
else
{
//Just close this message box
}
}
}
public void btnContinue_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlCommandBuilder cb;
cb = new System.Data.SqlClient.SqlCommandBuilder(mainScreen.da);
//Store the text entries as strings in the given variables
string first_name = firstName.Text;
string surname = lastName.Text;
string nick_name = nickName.Text;
//Check that first name and surname are not blanked as the Db will not accept NULL values
if (first_name == "" || surname == "")
{
MessageBox.Show("Please ensure at least first name and surname boxes are filled in", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
//Test that information is stored correctly
//MessageBox.Show(first_name);
//MessageBox.Show(surname);
//MessageBox.Show(nick_name);
DataRow dRow = mainScreen.ds1.Tables["player_info"].NewRow();
dRow[1] = firstName.Text;
dRow[2] = lastName.Text;
dRow[3] = nickName.Text;
mainScreen.ds1.Tables["player_info"].Rows.Add(dRow);
mainScreen.MaxRows = mainScreen.MaxRows + 1;
mainScreen.inc = mainScreen.MaxRows - 1;
mainScreen.da.Update(mainScreen.ds1, "player_info");
}
}
public void addplayer_Load(object sender, EventArgs e)
{
//Clear the text boxes when the player enters the form
firstName.Text = "";
lastName.Text = "";
nickName.Text = "";
}
}
}
The app runs fine right up until I enter some details in the add player screen and click continue, thats when I then get the error message
Object reference not set to an instance of an object.As i said, I have stepped through the code and it seems that dRow is what is causing the problem and is remaining at NULL.
The line causing the problem is in the addplayer code;
DataRow dRow = mainScreen.ds1.Tables["player_info"].NewRow();Can anyone help with this?
Thanks in advance all!
Premier2k
This post has been edited by Premier2k: 29 Aug, 2008 - 02:56 PM