Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

i have a problem
i try to insert into database but this not happen i check my database to see value but i see nothing !!
i don't know WHY ??
can you help me
i want to create web application use C# and Asp.net;

1- first i have Table3 in Database which have 2 column [Emp_id]&[password]
with data type nchar(5) for [Emp_id], and nchar(10) for [password]
then i create Stored procedure as following:
ALTER PROCEDURE dbo.StoredProcedure1

@Emp_id nchar(5),
@Password nchar(10)
	
AS
INSERT INTO Table3 (Emp_id,password)
VALUES (@Emp_id,@Password)

RETURN


2-in my "aspx" page i drag 2 textboxes and 1 button then in "aspx.cs" page and during the button i try to write insert code into database use "Stored procedure" how i do that. Note that, the insert from the user

Thanks :)
Posted
Updated 5-Apr-11 19:43pm
v2
Comments
wizardzz 5-Apr-11 12:34pm    
Are you simply asking how to call a stored procedure?
Cs.A M A L 5-Apr-11 20:33pm    
both i want to learn how to call a stored procedure with more than one parameter and i want to create inserting button to fill database , thanks;
[no name] 6-Apr-11 1:46am    
First you should use parametrize store procedure

You need to know how to use stored procedure using ADO.NET.

Just pick any link from here[^], first few:
Link 1[^]
Link 2[^]
Link 3[^]
 
Share this answer
 
v2
You need to use ADO.net. You need to put some code into the event of the button. Ideally you should separate and do it in a different class so that you separate your view from your model and data access.

Sample Code you can use is.

SqlConnection sqlConnection;
SqlCommand sqlCommand;
SqlParameter sqlparameter;
DataTable dt = null;

try
{
    sqlConnection = Database.DatabaseConnection;
               
    sqlCommand = new SqlCommand();
    sqlCommand.Connection = sqlConnection;
    sqlCommand.CommandType = CommandType.StoredProcedure;
    sqlCommand.CommandTimeout = 0;
    sqlCommand.CommandText = "StoredProcedure1";

    sqlparameter = new SqlParameter();
    sqlparameter.ParameterName = "@Emp_id";
    sqlparameter.SqlDbType = SqlDbType.VarChar;
    sqlparameter.Value = Emp_id;
    sqlparameter.Direction = ParameterDirection.Input;
    sqlCommand.Parameters.Add(sqlparameter);

    sqlparameter = new SqlParameter();
    sqlparameter.ParameterName = "@Password";
    sqlparameter.SqlDbType = SqlDbType.VarChar;
    sqlparameter.Value = Password;
    sqlparameter.Direction = ParameterDirection.Input;
    sqlCommand.Parameters.Add(sqlparameter);


    SqlDataAdapter sqlDA = new SqlDataAdapter();
    dt = new DataTable();
    sqlDA.SelectCommand = sqlCommand;
    sqlConnection.Open();
    sqlDA.Fill(dt);
    sqlConnection.Close();

}
catch (Exception ex)
{
              
}

    return dt;
 
Share this answer
 
Thank you so much for answer
can i ask a small question, please !
it is possible to insert value into database by using button and save in database ? or not?
 
Share this answer
 
Thanks;

i try to create method return DataTable value as following:

public DataTable table(string e,string p)
        {
            SqlParameter sqlparameter;
            DataTable dt = null;
            
            //1)connection:
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = "server=XXX; database=Test; integrated security=true";

            try
            {
                //---------------------------------------------------//
            //2)request:
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandTimeout = 0;
                cmd.CommandText = "StoredProcedure1";
                
                sqlparameter = new SqlParameter();
                sqlparameter.ParameterName = "@Emp_id";
                sqlparameter.SqlDbType = SqlDbType.NChar; //DataType of Emp_id is nChar(5)
                sqlparameter.Value = e; //this to assign value, (e) which is from Textbox1.Text
                sqlparameter.Direction = ParameterDirection.Input;
                cmd.Parameters.Add(sqlparameter);

                sqlparameter = new SqlParameter();
                sqlparameter.ParameterName = "@Password";
                sqlparameter.SqlDbType = SqlDbType.NChar; //DataType of Emp_id is nChar(10)
                sqlparameter.Value = p; //this to assign value, (p) which is from Textbox2.Text
                sqlparameter.Direction = ParameterDirection.Input;
                cmd.Parameters.Add(sqlparameter);
                
                SqlDataAdapter sqlDA = new SqlDataAdapter();
                dt = new DataTable();
                sqlDA.SelectCommand = cmd;
             //3)Open Connection:
                cn.Open();
                sqlDA.Fill(dt);
             //3)Close Connection:
                cn.Close();
            }
            catch (Exception ex)
            {
                Response.Write("Error");
            }
            return dt;
        }



And in button i invoke the method as following :

protected void Button1_Click(object sender, EventArgs e)
        {
            string EmpID = TextBox1.Text;
            string password = TextBox2.Text;

            GridView1.DataSource = table(EmpID, password); //try to display result as table
            GridView1.DataBind();
 
        }


but nothing happen i don't know if there something wrong, the GridView1 not display and the in database not result i go to my database and right click then show table data but no any thing plezzzzzzz :( if i have any error tell me. Note, when am i run there are no any error in code .

Thanks All :)
 
Share this answer
 
Comments
Michael Bookatz 5-Apr-11 17:15pm    
What happens if you debug the code and go through it one line at a time?
Cs.A M A L 5-Apr-11 20:31pm    
there is no error !!
and when i click to the button the page is refresh with do nothing?
can we inserting data into database by using button, really i confusing, just for ask !!!
And what about my codes above can i using to insert data by use button or may i have big mistake in codes and i don't know about it, for this i ask us to help me :)?

Thanks every one
 
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