943,866 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 4970
  • ASP.NET RSS
Dec 15th, 2008
0

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

Expand Post »
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:

ASP.NET Syntax (Toggle Plain Text)
  1. Dim conn As New SqlConnection
  2. conn.ConnectionString = "data source=.\\SQLEXPRESS;initial catalog=HRDATA;integrated security=true"
  3. Dim cmd As New SqlCommand
  4. cmd.CommandText = "INSERT INTO Employee Data values ('" & txt_name.Text & "')"
  5. cmd.Connection = conn
  6. conn.Open()
  7. cmd.ExecuteNonQuery()
  8. 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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
kodingkarthik is offline Offline
37 posts
since Dec 2008
Dec 15th, 2008
0

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

Maybe cmd.CommandType needs to be stated as text.
Reputation Points: 33
Solved Threads: 10
Junior Poster in Training
4advanced is offline Offline
67 posts
since Nov 2008
Dec 16th, 2008
0

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

try cmd.CommandType = CommandType.Text before

cmd.CommandText = "INSERT ...
Reputation Points: 25
Solved Threads: 18
Practically a Master Poster
binoj_daniel is offline Offline
645 posts
since Dec 2006
Dec 18th, 2008
0

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

Hi Dani,

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

ASP.NET Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 10
Solved Threads: 0
Light Poster
kodingkarthik is offline Offline
37 posts
since Dec 2008
Dec 18th, 2008
0

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

Try this :
DdlId.SelectedItem.Text;
Reputation Points: 16
Solved Threads: 18
Junior Poster
Aneesh_Argent is offline Offline
104 posts
since Dec 2008
Dec 19th, 2008
0

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

Hi, you still forgot for DropDownList

ASP.NET Syntax (Toggle Plain Text)
  1. myCommand = New SqlCommand("Insert into Employee Data values('" & ddlid.selectedItem & "','" & txtname.Text & "','" & ddldesig.selectedItem & "','" & txtcontno.Text & "')", "myConnection")
Last edited by peter_budo; Dec 21st, 2008 at 4:55 am. Reason: Please, either use [code] or [code=asp.net] (or other langiage as necessary). DO NOT USE [QUOTE]
Reputation Points: 11
Solved Threads: 17
Junior Poster
Kusno is offline Offline
191 posts
since Aug 2007
Jan 21st, 2010
0
Re: Inserting values into the database using a asp.net button click
ASP.NET Syntax (Toggle Plain Text)
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["shortycaponeConnectionString"].ConnectionString);
  4. //SqlConnection mySqlConnection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["shortycaponeConnectionString"].ConnectionString);
  5.  
  6. //// open the Sql connection
  7. //mySqlConnection.Open();
  8. //DataSet ds = new DataSet();
  9. //SqlDataAdapter da1 = new SqlDataAdapter("Select Quantity from InwardEntries where ProductCode='" + DropDownList4.SelectedItem.Text + "'", mySqlConnection);
  10. //da1.Fill(ds);
  11. //GridView2.DataSource = ds;
  12. //GridView2.DataBind();
  13.  
  14. for (int i = 0; i < GridView1.Rows.Count; i++)
  15. {
  16. GridViewRow row = GridView1.Rows[i];
  17. CheckBox chkbox = (CheckBox)row.FindControl("cbRows");
  18. TextBox Quantity = (TextBox)row.FindControl("tbx_TextBox");
  19.  
  20. if (chkbox.Checked == true)
  21. {
  22. try
  23. {
  24. con.Open();
  25. SqlCommand cmd = new SqlCommand("INSERT INTO InwardEntries(ProductCode,Size,Quantity,DateTimeNow) VALUES (@ProductCode,'" + row.Cells[1].Text + "','" + Quantity.Text + "','" + TextBox1.Text + "')", con);
  26. cmd.Parameters.AddWithValue("@ProductCode", DropDownList4.Text);
  27. cmd.Parameters.AddWithValue("@DateTime", TextBox1.Text);
  28. cmd.ExecuteNonQuery();
  29.  
  30. SqlCommand cmd2 = new SqlCommand("update Quantity1 set Quantity = Quantity +'" + Quantity.Text + "' where ProductCode='" + DropDownList4.SelectedItem.Text + "'", con);
  31. cmd2.Parameters.AddWithValue("@ProductCode", DropDownList4.Text);
  32. //cmd2.Parameters.Add("@qty", SqlDbType.Float);
  33. cmd2.ExecuteNonQuery();
  34. con.Close();
  35. }
  36. catch
  37. {
  38. Response.Write("");
  39.  
  40. }
  41. finally
  42. {
  43. con.Close();
  44. }
  45.  
  46.  
  47.  
  48.  
  49. }
  50. }
Last edited by nav33n; Jan 21st, 2010 at 5:06 am. Reason: Please use [code] tags to wrap your code for easy readability.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
diptibhatore is offline Offline
5 posts
since Nov 2009
Jan 21st, 2010
0
Re: Inserting values into the database using a asp.net button click
how to update data ???
Reputation Points: 10
Solved Threads: 0
Newbie Poster
diptibhatore is offline Offline
5 posts
since Nov 2009
Jan 21st, 2010
0
Re: Inserting values into the database using a asp.net button click
ASP.NET Syntax (Toggle Plain Text)
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["shortycaponeConnectionString"].ConnectionString);
  4. //SqlConnection mySqlConnection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["shortycaponeConnectionString"].ConnectionString);
  5.  
  6. //// open the Sql connection
  7. //mySqlConnection.Open();
  8. //DataSet ds = new DataSet();
  9. //SqlDataAdapter da1 = new SqlDataAdapter("Select Quantity from InwardEntries where ProductCode='" + DropDownList4.SelectedItem.Text + "'", mySqlConnection);
  10. //da1.Fill(ds);
  11. //GridView2.DataSource = ds;
  12. //GridView2.DataBind();
  13.  
  14. for (int i = 0; i < GridView1.Rows.Count; i++)
  15. {
  16. GridViewRow row = GridView1.Rows[i];
  17. CheckBox chkbox = (CheckBox)row.FindControl("cbRows");
  18. TextBox Quantity = (TextBox)row.FindControl("tbx_TextBox");
  19.  
  20. if (chkbox.Checked == true)
  21. {
  22. try
  23. {
  24. con.Open();
  25. SqlCommand cmd = new SqlCommand("INSERT INTO InwardEntries(ProductCode,Size,Quantity,DateTimeNow) VALUES (@ProductCode,'" + row.Cells[1].Text + "','" + Quantity.Text + "','" + TextBox1.Text + "')", con);
  26. cmd.Parameters.AddWithValue("@ProductCode", DropDownList4.Text);
  27. cmd.Parameters.AddWithValue("@DateTime", TextBox1.Text);
  28. cmd.ExecuteNonQuery();
  29.  
  30. SqlCommand cmd2 = new SqlCommand("update Quantity1 set Quantity = Quantity +'" + Quantity.Text + "' where ProductCode='" + DropDownList4.SelectedItem.Text + "'", con);
  31. cmd2.Parameters.AddWithValue("@ProductCode", DropDownList4.Text);
  32. //cmd2.Parameters.Add("@qty", SqlDbType.Float);
  33. cmd2.ExecuteNonQuery();
  34. con.Close();
  35. }
  36. catch
  37. {
  38. Response.Write("");
  39.  
  40. }
  41. finally
  42. {
  43. con.Close();
  44. }
  45.  
  46.  
  47.  
  48.  
  49. }
  50. }
  51.  
  52.  
  53.  
  54. //for (int i = 0; i < GridView2.Rows.Count; i++)
  55. //{
  56. // GridViewRow row = GridView2.Rows[i];
  57. // //CheckBox chkbox = (CheckBox)row.FindControl("cbRows");
  58. // TextBox Quantity = (TextBox)row.FindControl("tbx_TextBox1");
  59.  
  60. // try
  61. // {
  62. // //con.Open();
  63. // //SqlCommand cmd = new SqlCommand("INSERT INTO InwardEntries(ProductCode,Size,Quantity,DateTimeNow) VALUES (@ProductCode,'" + row.Cells[1].Text + "','" + Quantity.Text + "','" + TextBox1.Text + "')", con);
  64. // //cmd.Parameters.AddWithValue("@ProductCode", DropDownList4.Text);
  65. // //cmd.Parameters.AddWithValue("@DateTime", TextBox1.Text);
  66. // //cmd.ExecuteNonQuery();
  67.  
  68. // SqlCommand cmd2 = new SqlCommand("update Quantity1 set Quantity =@Quantity+Quantity where ProductCode='" + DropDownList4.SelectedItem.Text + "'", con);
  69. // cmd2.Parameters.AddWithValue("@ProductCode", DropDownList4.Text);
  70. // cmd2.Parameters.Add("@Quntity", SqlDbType.Int);
  71. // cmd2.ExecuteNonQuery();
  72. // con.Close();
  73. // }
  74. // catch
  75. // {
  76. // Response.Write("");
  77.  
  78. // }
  79. // finally
  80. // {
  81. // con.Close();
  82. // }
update opration are not working so how can perform update opration?
Last edited by nav33n; Jan 21st, 2010 at 8:32 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
diptibhatore is offline Offline
5 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC