7
Answers

inserting data from a textbox..

Heaven Valenzona

Heaven Valenzona

14y
3.6k
1
Hi.. I'm trying to insert data from a textbox. But there's an error..
here's my code: 

 protected void Button1_Click(object sender, EventArgs e)
    {
        String insertSQL;
        insertSQL = "INSERT INTO SERVICES (";
        insertSQL += "Service_Code, Service_Name)";
        insertSQL += "VALUES (";
        insertSQL += "@Service_Code, @Service_Name)";



        String connectionstring = WebConfigurationManager.ConnectionStrings["HelpDesk_System"].ConnectionString;
        SqlConnection conn = new SqlConnection(connectionstring);
        SqlCommand cmd = new SqlCommand(insertSQL, conn);

        cmd.Parameters.AddWithValue("@Service_Code", TextBox1.Text);
        cmd.Parameters.AddWithValue("@Service_Name", TextBox2.Text);

         int added = 0;
        try
        {
            conn.Open();
            added = cmd.ExecuteNonQuery();
            UpdatePanel1.Update();
            conn.Close();


        }
        catch (Exception err)
        {
            Label1.Text = "Error inserting";
            Label2.Text += err.Message;
        }




    }
}
 



the error states that:

Object reference not set to an instance of an object.


What does it mean?
Thank you.

---ms SQL
---C#
---asp.net



Answers (7)
0
Dipa Ahuja

Dipa Ahuja

NA 4.1k 612.6k 14y
m gal ... so dont call me sir  :)


any ways the connection string that you have added in we.config file.. is that correct?

for ex:
<connectionStrings>
<add name="ConnString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db1.mdb;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>




retrive:

String connectionstring = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;

Accepted
0
Dipa Ahuja

Dipa Ahuja

NA 4.1k 612.6k 14y
Chirag is right..

the autonumber field does not allow you to insert data...


because its automatically generated..
0
Crish

Crish

NA 3.7k 76.4k 14y
hi

You can not insert value in Identity (primary key value where you have set autoincrement true) in table 'SERVICES'. So don't insert this ID value in this table. so it will work.



Don't forget to Mark Do you like this Answer
 that solved your problem!

0
Heaven Valenzona

Heaven Valenzona

NA 23 0 14y
thank you!.. can I ask 1 more question? :)

may primary key service_code is a autonumber..

I get this error: 
Error inserting 
LabelCannot insert explicit value for identity column in table 'SERVICES' when IDENTITY_INSERT is set to OFF. 

Why? 
0
Dipa Ahuja

Dipa Ahuja

NA 4.1k 612.6k 14y
check the "name" of connection string i think thats y you r getting this erro: 
Object reference not set to an instance of an object. 
0
Heaven Valenzona

Heaven Valenzona

NA 23 0 14y
yes sir. I've already include that.. I use 

String connectionstring = WebConfigurationManager.ConnectionStrings["HelpDesk_System"].ConnectionString; 
 

then I use your suggested code.. but still no luck..

What do you mean by my connection string? is that the name of my database? 


0
Dipa Ahuja

Dipa Ahuja

NA 4.1k 612.6k 14y
may be you are accessing wrong connection string Use the ConfigurationManager.ConnectionStrings

try this...


protected void Button1_Click(object sender, EventArgs e)
    {
        String insertSQL;
        insertSQL = "INSERT INTO SERVICES (Service_Code, Service_Name)VALUES (@Service_Code, @Service_Name)";

        String connectionstring = ConfigurationManager.ConnectionStrings["HelpDesk_System"].ConnectionString; 
       
        SqlConnection conn = new SqlConnection(connectionstring); 
        SqlCommand cmd = new SqlCommand(insertSQL, conn); 
        cmd.Parameters.AddWithValue("@Service_Code", TextBox1.Text);
        cmd.Parameters.AddWithValue("@Service_Name", TextBox2.Text); 
        
        int added = 0; 
        try 
        {
            conn.Open();
            added = cmd.ExecuteNonQuery(); 
            UpdatePanel1.Update(); 
            conn.Close(); 
        }
        catch (Exception err)
        {
            Label1.Text = "Error inserting"; Label2.Text += err.Message;
        }
    }
Next Recommended Forum