954,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Inserting values into the database using a asp.net button click

Hi all,


I am new to asp.net programming and i have to design a website which should accept values from the user and store it in the databse on a button click event.

I am not able to do it In fact i have written a code for button click the code is as follows:

Dim conn As New SqlConnection
        conn.ConnectionString = "data source=.\\SQLEXPRESS;initial catalog=HRDATA;integrated security=true"
        Dim cmd As New SqlCommand
        cmd.CommandText = "INSERT INTO Employee Data values ('" & txt_name.Text & "')"
        cmd.Connection = conn
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()


the problem that i am facing is at
cmd.ExecuteNonQuery()
the error message i am getting is tht the ExecuteNonQuery is not initialized..


Thanks in advance ...
Karthik.

kodingkarthik
Light Poster
37 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

Maybe cmd.CommandType needs to be stated as text.

4advanced
Junior Poster in Training
67 posts since Nov 2008
Reputation Points: 33
Solved Threads: 10
 

try cmd.CommandType = CommandType.Text before

cmd.CommandText = "INSERT ...

binoj_daniel
Practically a Master Poster
645 posts since Dec 2006
Reputation Points: 25
Solved Threads: 18
 

Hi Dani,

Thanks for your reply and i have a new problem with the insert command that iam using please help me out

myCommand = New SqlCommand("Insert into Employee Data values(ddlid.selectedItem,'" & txtname.Text & "',ddldesig.selectedItem,'" & txtcontno & "')", "myConnection")


Whats wrong with my insert command that i am using in my code...


Thanks in advance
Karthik.

kodingkarthik
Light Poster
37 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

Try this :
DdlId.SelectedItem.Text;

Aneesh_Argent
Junior Poster
104 posts since Dec 2008
Reputation Points: 16
Solved Threads: 18
 

Hi, you still forgot for DropDownList

myCommand = New SqlCommand("Insert into Employee Data values('" & ddlid.selectedItem & "','" & txtname.Text & "','" & ddldesig.selectedItem & "','" & txtcontno.Text & "')", "myConnection")
Kusno
Junior Poster
191 posts since Aug 2007
Reputation Points: 11
Solved Threads: 17
 
protected void Button1_Click(object sender, EventArgs e)
    {
      SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["shortycaponeConnectionString"].ConnectionString);
       //SqlConnection mySqlConnection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["shortycaponeConnectionString"].ConnectionString);

       //// open the Sql connection            
       //mySqlConnection.Open();
       //DataSet ds = new DataSet();
       //SqlDataAdapter da1 = new SqlDataAdapter("Select Quantity from InwardEntries where ProductCode='" + DropDownList4.SelectedItem.Text + "'", mySqlConnection);
       //da1.Fill(ds);
       //GridView2.DataSource = ds;
       //GridView2.DataBind();

       for (int i = 0; i < GridView1.Rows.Count; i++)
       {
           GridViewRow row = GridView1.Rows[i];
           CheckBox chkbox = (CheckBox)row.FindControl("cbRows");
           TextBox Quantity = (TextBox)row.FindControl("tbx_TextBox");

           if (chkbox.Checked == true)
           {
               try
               {
                   con.Open();
                   SqlCommand cmd = new SqlCommand("INSERT INTO InwardEntries(ProductCode,Size,Quantity,DateTimeNow) VALUES (@ProductCode,'" + row.Cells[1].Text + "','" + Quantity.Text + "','" + TextBox1.Text + "')", con);
                   cmd.Parameters.AddWithValue("@ProductCode", DropDownList4.Text);
                   cmd.Parameters.AddWithValue("@DateTime", TextBox1.Text);
                   cmd.ExecuteNonQuery();

                   SqlCommand cmd2 = new SqlCommand("update Quantity1 set Quantity = Quantity +'" + Quantity.Text + "' where ProductCode='" + DropDownList4.SelectedItem.Text + "'", con);
                   cmd2.Parameters.AddWithValue("@ProductCode", DropDownList4.Text);
                   //cmd2.Parameters.Add("@qty", SqlDbType.Float);
                   cmd2.ExecuteNonQuery();
                   con.Close();
               }
               catch
               {
                   Response.Write("");

               }
               finally
               {
                   con.Close();
               }




           }
       }
diptibhatore
Newbie Poster
5 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

how to update data ???

diptibhatore
Newbie Poster
5 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 
protected void Button1_Click(object sender, EventArgs e)
    {
      SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["shortycaponeConnectionString"].ConnectionString);
       //SqlConnection mySqlConnection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["shortycaponeConnectionString"].ConnectionString);

       //// open the Sql connection            
       //mySqlConnection.Open();
       //DataSet ds = new DataSet();
       //SqlDataAdapter da1 = new SqlDataAdapter("Select Quantity from InwardEntries where ProductCode='" + DropDownList4.SelectedItem.Text + "'", mySqlConnection);
       //da1.Fill(ds);
       //GridView2.DataSource = ds;
       //GridView2.DataBind();

       for (int i = 0; i < GridView1.Rows.Count; i++)
       {
           GridViewRow row = GridView1.Rows[i];
           CheckBox chkbox = (CheckBox)row.FindControl("cbRows");
           TextBox Quantity = (TextBox)row.FindControl("tbx_TextBox");

           if (chkbox.Checked == true)
           {
               try
               {
                   con.Open();
                   SqlCommand cmd = new SqlCommand("INSERT INTO InwardEntries(ProductCode,Size,Quantity,DateTimeNow) VALUES (@ProductCode,'" + row.Cells[1].Text + "','" + Quantity.Text + "','" + TextBox1.Text + "')", con);
                   cmd.Parameters.AddWithValue("@ProductCode", DropDownList4.Text);
                   cmd.Parameters.AddWithValue("@DateTime", TextBox1.Text);
                   cmd.ExecuteNonQuery();

                   SqlCommand cmd2 = new SqlCommand("update Quantity1 set Quantity = Quantity +'" + Quantity.Text + "' where ProductCode='" + DropDownList4.SelectedItem.Text + "'", con);
                   cmd2.Parameters.AddWithValue("@ProductCode", DropDownList4.Text);
                   //cmd2.Parameters.Add("@qty", SqlDbType.Float);
                   cmd2.ExecuteNonQuery();
                   con.Close();
               }
               catch
               {
                   Response.Write("");

               }
               finally
               {
                   con.Close();
               }




           }
       }



       //for (int i = 0; i < GridView2.Rows.Count; i++)
       //{
       //    GridViewRow row = GridView2.Rows[i];
       //    //CheckBox chkbox = (CheckBox)row.FindControl("cbRows");
       //    TextBox Quantity = (TextBox)row.FindControl("tbx_TextBox1");

       //     try
       //        {
       //            //con.Open();
       //            //SqlCommand cmd = new SqlCommand("INSERT INTO InwardEntries(ProductCode,Size,Quantity,DateTimeNow) VALUES (@ProductCode,'" + row.Cells[1].Text + "','" + Quantity.Text + "','" + TextBox1.Text + "')", con);
       //            //cmd.Parameters.AddWithValue("@ProductCode", DropDownList4.Text);
       //            //cmd.Parameters.AddWithValue("@DateTime", TextBox1.Text);
       //            //cmd.ExecuteNonQuery();

       //            SqlCommand cmd2 = new SqlCommand("update Quantity1 set Quantity =@Quantity+Quantity where ProductCode='" + DropDownList4.SelectedItem.Text + "'", con);
       //            cmd2.Parameters.AddWithValue("@ProductCode", DropDownList4.Text);
       //            cmd2.Parameters.Add("@Quntity", SqlDbType.Int);
       //            cmd2.ExecuteNonQuery();
       //            con.Close();
       //        }
       //        catch
       //        {
       //            Response.Write("");

       //        }
       //        finally
       //        {
       //            con.Close();
       //        }

update opration are not working so how can perform update opration?

diptibhatore
Newbie Poster
5 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You