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.

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.

Hi, you still forgot for DropDownList

myCommand = New SqlCommand("Insert into Employee Data values('" & ddlid.selectedItem & "','" & txtname.Text & "','" & ddldesig.selectedItem & "','" & txtcontno.Text & "')", "myConnection")
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();
               }




           }
       }

Edited 6 Years Ago by nav33n: Please use [code] tags to wrap your code for easy readability.

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?

Edited 6 Years Ago by nav33n: n/a

Imports System.Data
Imports System.Data.OleDb

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim con As New OleDbConnection
    Dim cmd As OleDbCommand
    Dim dreader As OleDbDataReader
    Dim dadapter As OleDbDataAdapter
    Dim sqlquery As String

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


        con = New OleDbConnection
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\blood.mdb"
        con.Open()

        sqlquery = "insert into mreg values ('" & txtName.Text & "','" & Trim(ddBg.SelectedItem.ToString) & "','" & txtAge.Text & "','" & ddGender.SelectedItem.ToString & "','" & txtMob.Text & "','" & txtLand.Text & "','" & txtEmail.Text & "','" & txtAddr.Text & "','" & ddcity.SelectedItem.ToString & "')"
        cmd = New OleDbCommand(sqlquery, con)


        cmd.ExecuteNonQuery()
        con.Close()


        txtName.Text = ""
        txtAge.Text = ""
        txtMob.Text = ""
        txtLand.Text = ""
        txtAddr.Text = ""
        txtEmail.Text = ""
        ddGender.ClearSelection()
        ddBg.ClearSelection()
        ddcity.ClearSelection()


        Response.Redirect("thanks.aspx")



    End Sub

End Class

its work in local host..but error occur in web servers...
error in
cmd.ExecuteNonQuery() this part..
plssss helpppppp

@charlesvijay - hello and welcome...it appears that you are a new member. One thing to point is that you may want to consider starting your own thread when you have a question rather than hijaking someone else's thread. It can get confusing for others that arrive at this thread to follow the question, if there is more than one submitted by different members.

In any case, with regard to your problem, you should check with your hosting provider. They may not support hosting an Access database. If they do, the connection provider that you are using in your code may be outdated. There is a newer Jet OLEDB driver available.

Edited 3 Years Ago by JorgeM

This article has been dead for over six months. Start a new discussion instead.