Muhammad Sajid Lakha Muhammad Sajid Lakha - 3 months ago 21
C# Question

There is no row at position 0 for inserting data in database

i am facing a similar problem and i dont know how to solve them i tried many things but this issue i can't solve yet please help
actually problem is i want to select value from database and if ds == null so textbox value i want to insert in database but when i insert it's shown me error
here is the code :

string query = "Select * From Contacts where [Phone No] = " + textBox00.Text + "";
using (OleDbConnection conn = new OleDbConnection(connStr))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
{
conn.Open();
DataSet ds = new DataSet();
if (ds != null)
{
adapter.Fill(ds);
label128.Text = ds.Tables[0].Rows[0]["Name"].ToString();
setaluefortext00001 = ds.Tables[0].Rows[0]["Phone No"].ToString();
setaluefortext00002 = ds.Tables[0].Rows[0]["Address"].ToString();
conn.Close();
}
else
{
adapter.Fill(ds);
connection.Open();
OleDbCommand command1 = new OleDbCommand();
command1.Connection = connection;
command1.CommandText = "insert into Contacts ([Name],[Phone No],[Address])values('" + textBox.Text + "'," + textBox00.Text + ",'" + textBox000.Text + "');";
command1.ExecuteNonQuery();
connection.Close();
label128.Text = textBox.Text;
setaluefortext00002 = textBox00.Text;
setaluefortext00003 = textBox000.Text;
}

Answer

It seems your return database: table has no any row.

Please also put validation for row count as below:

if (ds != null && ds.tables[0].rows.count>0)

Because in your code ds never null as you assign new dataset : DataSet ds = new DataSet();

So always if statement execute, code not reach to else statement

You can also put your adapter.Fill(ds); in the above if statement

string query = "Select * From Contacts where [Phone No] = " + textBox00.Text + "";
                using (OleDbConnection conn = new OleDbConnection(connStr))
                {
                    using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
                    {
                        conn.Open();
                        DataSet ds = new DataSet();
                        adapter.Fill(ds);
                        if (ds != null && ds.Tables[0].rows.count>0)
                        {                                
                            label128.Text = ds.Tables[0].Rows[0]["Name"].ToString();
                            setaluefortext00001 = ds.Tables[0].Rows[0]["Phone No"].ToString();
                            setaluefortext00002 = ds.Tables[0].Rows[0]["Address"].ToString();

                        }
                        else
                        {                                

                            OleDbCommand command1 = new OleDbCommand();
                            command1.Connection = conn ;
                            command1.CommandText = "insert into Contacts ([Name],[Phone No],[Address])values('" + textBox.Text + "'," + textBox00.Text + ",'" + textBox000.Text + "');";
                            command1.ExecuteNonQuery();                                
                            label128.Text = textBox.Text;
                            setaluefortext00002 = textBox00.Text;
                            setaluefortext00003 = textBox000.Text;
                        }
                        conn.Close();
Comments