Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,


i want to insert textbox value into database using store procedure.can any one help me??




Regards
Deepak
Posted

Procedure in MySQL:
CREATE PROCEDURE insertToTable
(IN param1 CHAR(20))
BEGIN
  INSERT INTO table(field1) VALUES(param1)
END


Code in program:
MySqlCommand cmd = new MySqlCommand("insertToTable", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@param1", TextBox1.Text);
cmd.ExecuteNonQuery();


Ref: http://dev.mysql.com/doc/refman/5.5/en/connector-net-tutorials-intro.html#connector-net-tutorials-stored-procedures[^]
 
Share this answer
 
Comments
guptaadeepak 24-Jun-11 8:17am    
i need some more help you code is working but i have problem on executenonquery i am post my code too plz help me
guptaadeepak 24-Jun-11 8:31am    
problem is sort out thanks
Prerak Patel 24-Jun-11 12:18pm    
You are welcome.
this is dummy program of Insert data into database using store procedure

store procedure defination
--------------------------------------------
SQL
create procedure sp_Test
(
    @Id int, @Name varchar(50)
)
Insert into table1(Id, FullName) values(@Id,@Name)

--------------------------------------------


C# code for call store procedure and insert data in database.(perform it a button click event)
--------------------------------------
C#
try
  {
     sqlConnection = new SqlConnection(dbConnectionString);
     SqlCommand command = new SqlCommand("sp_Test", sqlConnection);
     command.CommandType = CommandType.StoredProcedure;
     command.Parameters.Add("@Id", SqlDbType.VarChar).Value = txtId.Text;
     command.Parameters.Add("@Name", SqlDbType.DateTime).Value = txtName.Text;
     sqlConnection.Open();
     return command.ExecuteNonQuery();
     sqlConnection.Close();
  }
catch (SqlException ex)
  {
     Console.WriteLine("SQL Error" + ex.Message.ToString());
     return 0;
  }

-------------------------------------

@Id and @Name is database field parameter value if your table has more field just serially add like
MIDL
command.Parameters.Add("@Id", SqlDbType.VarChar).Value = txtId.Text;
command.Parameters.Add("@Name", SqlDbType.DateTime).Value = txtName.Text;
.
.
.
.


thanks
 
Share this answer
 
Comments
Er.Siddharth Singh 5-Aug-16 3:32am    
please write code for same task using mysql
<ssssssssssssssssssssssssssssssssssss>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900