Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hello
Need help data is not going into tables.

I have separate cs file for connection and command objects,

C#
DataSet _ds = new DataSet();
DBUtilities _util = new DBUtilities();
        

SqlConnection con;
SqlCommand cmd = new SqlCommand();


Now in the button add code i have this code,
But the data is not getting into database table

C#
protected void Button1_Click(object sender, EventArgs e)
        
con = new SqlConnection();
            

cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text.Trim();
cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = txtDesc.Text.Trim();
cmd.Parameters.Add("@Image", SqlDbType.Image).Value = txtImage.Text.Trim();
cmd.Parameters.Add("@Active", SqlDbType.VarChar).Value = txtActive.Text.Trim();
cmd.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = txtCreatedBy.Text.Trim();
cmd.Parameters.Add("@CreatedDate", SqlDbType.DateTime).Value = txtCreDate.Text.Trim();

cmd.Connection = con;
cmd.CommandText = "sp_SqlImage";
}


Plz can any one tell me the right code for this.
Plz
Posted
Updated 18-Jun-12 0:29am
v2

You only created the command object. You haven't execute it yet :)
You need to do something like this:

C#
cmd.CommandType = CommandType.StoredProcedure;

// execute the command
cmd.ExecuteReader();


Look at this article [^]to read how to execute a stored proc from C#
 
Share this answer
 
check out the following line...


cmd.Parameters.Add("@Image", SqlDbType.Image).Value = txtImage.Text.Trim();



because of the above line you data is not inserting...


Thanks
 
Share this answer
 
C#
SqlConnection con = new SqlConnection();
con.ConnectionString=ConfigurationManager.ConnectionString["Name Of Connection String"].toString();
SqlCommand cmd=new SqlCommand("Name of the stored procedure",con);
cmd.Parameters.Add("@Name", txtName.Text.toString());
cmd.Parameters.Add("@Description", txtDesc.Text.toString());
cmd.Parameters.Add("@Image",txtImage.Text.toString());
cmd.Parameters.Add("@Active",txtActive.Text.toString());
cmd.Parameters.Add("@CreatedBy",txtCreatedBy.Text.toString());
cmd.Parameters.Add("@CreatedDate", convert.toDateTime(txtCreDate.Text));
cmd.CommandType=CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
cmd.close();
con.close();
 
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