Created below code (for some reason is not adding the record in database - any help?):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Configuration;
namespace Test
{
public partial class Register_New_Item : System.Web.UI.Page
{
private string connectionString = WebConfigurationManager.ConnectionStrings["InsertRecord"].ConnectionString;
mentori79
Member
2 Points
8 Posts
Saving Records in Database from Visual Studio using C# - Help/Suggestion
Jan 31, 2013 10:29 AM|LINK
I've already created:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Configuration;
namespace Test
{
public partial class Register_New_Item : System.Web.UI.Page
{
private string connectionString = WebConfigurationManager.ConnectionStrings["InsertRecord"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Submit_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("sp_InsertUpdate", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Name",SqlDbType.NVarChar, 50));
cmd.Parameters["@Name"].Value=txtName.Text;
cmd.Parameters.Add(new SqlParameter("@Gender",SqlDbType.NVarChar, 50));
cmd.Parameters["@Gender"].Value=Convert.ToInt32(ddGender.SelectedValue.ToString());
cmd.Parameters.Add(new SqlParameter("@Address",SqlDbType.NVarChar, 50));
cmd.Parameters["@Address"].Value=txtAddress.Text;
cmd.Parameters.Add(new SqlParameter("@Phone",SqlDbType.NVarChar, 50));
cmd.Parameters["@Phone"].Value=txtPhone.Text;
cmd.Parameters.Add(new SqlParameter("@Zip",SqlDbType.NVarChar, 50));
cmd.Parameters["@Zip"].Value=Convert.ToInt32(ddZip.SelectedValue.ToString());
try
{
cnn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new ApplicationException ("Data error.");
Console.WriteLine(ex.Message);
}
finally
{
cnn.Close();
}
}
}
}
}
AidyF
Star
9250 Points
1578 Posts
Re: Saving Records in Database from Visual Studio using C# - Help/Suggestion
Jan 31, 2013 10:35 AM|LINK
What is in sp_InsertUpdate?
cool_cupid_j...
Participant
1741 Points
354 Posts
Re: Saving Records in Database from Visual Studio using C# - Help/Suggestion
Jan 31, 2013 10:38 AM|LINK
Keep a break point in the catch block (press f9)and run ur code to figure out what is the error.
you migth be able to figure out the problem like data type casting problem or connection establishing problem.
Robert
cool_cupid_j...
Participant
1741 Points
354 Posts
Re: Saving Records in Database from Visual Studio using C# - Help/Suggestion
Jan 31, 2013 10:46 AM|LINK
Try this on your sql server
exec sp_InsertUpdate @Name='raj',@Gender=1,@Address='India',@Phone='9876543210',@Zip='654321'
ddGender.SelectedValue might not have a int value
if the procedure is executed check your code connection string.
Robert
mentori79
Member
2 Points
8 Posts
Re: Saving Records in Database from Visual Studio using C# - Help/Suggestion
Jan 31, 2013 10:50 AM|LINK
ALTER proc [dbo].[sp_InsertUpdate]
@Name nvarchar(50),
@Gender nvarchar(50),
@Address nvarchar(50),
@Phone nvarchar(50),
@Zip nvarchar(50)
as
declare @c int
set @c=(select COUNT(Name) from RegisterNewItem where Zip=@Zip)
if @c=1
begin
update RegisterNewItem set Name=@Name, Gender =@Gender, Address =@Address , Phone =@Phone where Zip=@Zip
end
if @c=0
begin
insert into RegisterNewItem (Name, Gender, Address , Phone, Zip) values (@Name, @Gender, @Address , @Phone, @Zip)
end
cool_cupid_j...
Participant
1741 Points
354 Posts
Re: Saving Records in Database from Visual Studio using C# - Help/Suggestion
Jan 31, 2013 11:03 AM|LINK
did u execute this?
exec sp_InsertUpdate @Name='raj',@Gender='1',@Address='India',@Phone='9876543210',@Zip='654321'
did u keep break point and see wat was the error?
Robert
oned_gk
All-Star
36200 Points
7374 Posts
Re: Saving Records in Database from Visual Studio using C# - Help/Suggestion
Jan 31, 2013 11:19 AM|LINK
What the problem? Any exception?
Suwandi - Non Graduate Programmer
mentori79
Member
2 Points
8 Posts
Re: Saving Records in Database from Visual Studio using C# - Help/Suggestion
Jan 31, 2013 04:53 PM|LINK
I've tried (in SQL Server to execute stored procedure) and it says: Command(s) completed successfully.
However, it is not saving the record in database (I've also tried to refresh the database, but still nothing)!
I don't know why is not saving it?!
Any idea/help?
mentori79
Member
2 Points
8 Posts
Re: Saving Records in Database from Visual Studio using C# - Help/Suggestion
Jan 31, 2013 04:56 PM|LINK
It is highlighting Console:
Console.WriteLine(ex.Message);
and is showing this message: Unreachable code detected
Any idea why?
oned_gk
All-Star
36200 Points
7374 Posts
Re: Saving Records in Database from Visual Studio using C# - Help/Suggestion
Feb 01, 2013 12:31 AM|LINK
Temporary remove try { ... } to get the exception message
Suwandi - Non Graduate Programmer