Click here to Skip to main content
15,923,845 members
Please Sign up or sign in to vote.
2.33/5 (4 votes)
See more:
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=HP-PC\SQLEXPRESS;Initial Catalog=Enquiry;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("sp_userinformation", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Name", Name);
        cmd.Parameters.AddWithValue("@Emailid", Emailid);
        cmd.Parameters.AddWithValue("@Phoneno", Phoneno);
        cmd.Parameters.AddWithValue("@Address", Address);
        cmd.Parameters.AddWithValue("@Pincode", Pincode);
        cmd.Parameters.AddWithValue("@Heard", Heard);
        cmd.Parameters.AddWithValue("@Enquiryabout", EnquiryAbout);
        cmd.Parameters.AddWithValue("@Message", Message);
        cmd.ExecuteNonQuery();
        con.Close();
    }
}


i had written this code for registration page...but values are not getting to database...
my database name is: Enquiry
table: EnquiryTable

please help me out...
i m a beginer and want guidance.
Posted
Updated 27-Feb-14 0:53am
v2
Comments
Animesh Datta 27-Feb-14 6:52am    
provide your store procedure definition .
[no name] 27-Feb-14 6:54am    
Share "sp_userinformation"
V5709 27-Feb-14 7:01am    
Is there any error which you are getting? If possible post your stored procedure also.
Ramug10 27-Feb-14 7:02am    
Show your SP script
manishmns12 27-Feb-14 7:21am    
where i will get my stored procedure???

i just copied a code over internet..
i dnt know what to write instead of "sp_userinformation"

I think you are getting confused with stored procedure. Replace stored procedure parameters with simple query.

C#
protected void Button1_Click(object sender, EventArgs e)
    {
      try
       {
        SqlConnection con = new SqlConnection(@"Data Source=HP-PC\SQLEXPRESS;Initial                                                                    Catalog=Enquiry;Integrated Security=True");
        con.Open();
        
        string query="insert into EnquiryTable values('" + txtName.Text + "','" +             txtEmailid.Text + "','" + txtPhoneno.Text + "','" + txtAddress.Text + "','" + txtPincode.Text + "','" + txtHeardfrom.Text + "','" + txtEnquiryabout.Text + "','" + txtMessage.Text + "')"; 
      
        SqlCommand cmd = new SqlCommand(queryString, con);
        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
        con.Close();
        }
catch(exception ex)
     {
 
     }
 
    }


Fore more information refer this link

** To Get clear idea about STORED PROCEDURES check this link

Refer this link
for Using stored procedures with ado.net.
 
Share this answer
 
v2
Comments
manishmns12 28-Feb-14 0:49am    
sir i had tried this...but getting a error....
please look at this

the projet is runnning but aftr giving the values to textnboxes the error is coming.., means run time error is
error message is:

Error Occured! Try AgainSystem.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '@Name'. Incorrect syntax near the keyword 'where'. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Enquiry2.Button1_Click(Object sender, EventArgs e) in c:\Users\HP\Documents\Visual Studio 2010\WebSites\Grapple\Enquiry2.aspx.cs:line 36 ClientConnectionId:994391c8-ae81-46d7-ad86-1c7aa68a74ceThanks for Enquiring. We will be available shortly






and sir the code is:

protected void Button1_Click(object sender, EventArgs e)
{
try
{
string str = "Data Source=HP-PC\\SQLEXPRESS;Initial Catalog=Enquiry;Integrated Security=True";
SqlConnection con = new SqlConnection(str);
con.Open();
string insertQuery = "insert into EnquiryTable where (Name,Emailid,Phoneno,Address,Pincode,Heardfrom,Enquiryabout,Message) values (@Name,@Emailid,@Phoneno,@Address,@Pincode,@Heardfrom,@Enquiryabout,@Message)";
SqlCommand com = new SqlCommand(insertQuery, con);
com.Parameters.AddWithValue(" @Name", txtName.Text);
com.Parameters.AddWithValue(" @EmailId", txtEmailid.Text);
com.Parameters.AddWithValue(" @Phoneno", txtPhoneno.Text);
com.Parameters.AddWithValue(" @Address", txtAddress.Text);
com.Parameters.AddWithValue(" @Pincode", txtPincode.Text);
com.Parameters.AddWithValue(" @Heardfrom", txtHeardfrom.Text);
com.Parameters.AddWithValue(" @ENquiryabout", txtEnquiryabout.Text);
com.Parameters.AddWithValue(" @Message", txtMessage.Text);


com.ExecuteNonQuery();
Response.Write("Thank You for Submitting your query");
Response.Redirect("Home.aspx");

con.Close();
Label.Visible = true;
Label.Text = "Thanks forEnquiring";
}

catch (Exception ex)
{
Response.Write("Error Occured! Try Again" + ex.ToString());
}

Response.Write("Thanks for Enquiring. We will be available shortly");



}
V5709 28-Feb-14 0:55am    
My friend, check your query first. Replace your sql query by fallowing.

string insertQuery = "insert into EnquiryTable (Name,Emailid,Phoneno,Address,Pincode,Heardfrom,Enquiryabout,Message) values (@Name,@Emailid,@Phoneno,@Address,@Pincode,@Heardfrom,@Enquiryabout,@Message)";

Please mark it as answer if worked for you. :)
manishmns12 28-Feb-14 1:03am    
sorry sir but after submitting the values...error is still somin and is as:

Error Occured! Try AgainSystem.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '@Name'. Must declare the scalar variable "@Name". at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Enquiry2.Button1_Click(Object sender, EventArgs e) in c:\Users\HP\Documents\Visual Studio 2010\WebSites\Grapple\Enquiry2.aspx.cs:line 36 ClientConnectionId:ea9b27e2-1948-49c5-be6d-7cddd8c54571Thanks for Enquiring. We will be available shortly
manishmns12 28-Feb-14 1:03am    
i tried this sir:


protected void Button1_Click(object sender, EventArgs e)
{
try
{
string str = "Data Source=HP-PC\\SQLEXPRESS;Initial Catalog=Enquiry;Integrated Security=True";
SqlConnection con = new SqlConnection(str);
con.Open();
string insertQuery = "insert into EnquiryTable (Name,Emailid,Phoneno,Address,Pincode,Heardfrom,Enquiryabout,Message) values (@Name,@Emailid,@Phoneno,@Address,@Pincode,@Heardfrom,@Enquiryabout,@Message)";
SqlCommand com = new SqlCommand(insertQuery, con);
com.Parameters.AddWithValue(" @Name", txtName.Text);
com.Parameters.AddWithValue(" @EmailId", txtEmailid.Text);
com.Parameters.AddWithValue(" @Phoneno", txtPhoneno.Text);
com.Parameters.AddWithValue(" @Address", txtAddress.Text);
com.Parameters.AddWithValue(" @Pincode", txtPincode.Text);
com.Parameters.AddWithValue(" @Heardfrom", txtHeardfrom.Text);
com.Parameters.AddWithValue(" @ENquiryabout", txtEnquiryabout.Text);
com.Parameters.AddWithValue(" @Message", txtMessage.Text);


com.ExecuteNonQuery();
Response.Write("Thank You for Submitting your query");
Response.Redirect("Home.aspx");

con.Close();
Label.Visible = true;
Label.Text = "Thanks forEnquiring";
}

catch (Exception ex)
{
Response.Write("Error Occured! Try Again" + ex.ToString());
}

Response.Write("Thanks for Enquiring. We will be available shortly");



}
}
V5709 28-Feb-14 1:09am    
replace some code lines by fallowing..

com.Parameters.AddWithValue("@Name", txtName.Text);
com.Parameters.AddWithValue("@EmailId", txtEmailid.Text);
com.Parameters.AddWithValue("@Phoneno", txtPhoneno.Text);
com.Parameters.AddWithValue("@Address", txtAddress.Text);
com.Parameters.AddWithValue("@Pincode", txtPincode.Text);
com.Parameters.AddWithValue("@Heardfrom", txtHeardfrom.Text);
com.Parameters.AddWithValue("@ENquiryabout", txtEnquiryabout.Text);
com.Parameters.AddWithValue("@Message", txtMessage.Text);


Dont left space between " & '@'
Please add a try catch block like below

C#
protected void Button1_Click(object sender, EventArgs e)
    {
      try
       {
        SqlConnection con = new SqlConnection(@"Data Source=HP-PC\SQLEXPRESS;Initial                            Catalog=Enquiry;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("sp_userinformation", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Name", Name);
        cmd.Parameters.AddWithValue("@Emailid", Emailid);
        cmd.Parameters.AddWithValue("@Phoneno", Phoneno);
        cmd.Parameters.AddWithValue("@Address", Address);
        cmd.Parameters.AddWithValue("@Pincode", Pincode);
        cmd.Parameters.AddWithValue("@Heard", Heard);
        cmd.Parameters.AddWithValue("@Enquiryabout", EnquiryAbout);
        cmd.Parameters.AddWithValue("@Message", Message);
        cmd.ExecuteNonQuery();
        con.Close();
        }
catch(exception ex)
     {

     }

    }


then run this program in debug mode then you will get exact error message that will help you to solve this issue by yourself.
 
Share this answer
 
Comments
manishmns12 27-Feb-14 13:35pm    
sir cn u please explain me about sp_userinformation
what is this about..
i copied it over internet..so plz help me out

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