Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP ASP.NET SQL ADO ADO.NET
it is not inserting the data where i am doing the mistak... , its showing catch error message

 
  string Patient_name = NameTxtBx.Text, Export_TO = ToTxtBx0.Text, repType = RadioButtonList2.SelectedValue  ;
                        int PatNoVal;
                        PatNoVal = Convert.ToInt32(PatNo.Text);
                        PatNoVal = int.Parse(PatNo.Text);
                        decimal PatID = decimal.Parse(PatID_NO.Text);
                        int? replay_To_type = Int16.Parse(DropDownList1.SelectedValue);
                        int idcount;            
                           try
                            {                            
                                SqlConnection con =  new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
                                con.Open();
                                SqlCommand cmd = new SqlCommand;
                                cmd.CommandType = CommandType.StoredProcedure;
                                cmd.CommandText = "Transaction";
                                cmd.Parameters.AddWithValue(" @Patient ", Patient_name);
                                cmd.Parameters.AddWithValue("@E_TO ", Export_TO);
                                cmd.Parameters.AddWithValue ("@R_type ",repType);
                                cmd.Parameters.AddWithValue("@ReportType ",replay_To_type);
                                cmd.Parameters.AddWithValue(" @Patient_no  ",PatNoVal);
                                cmd.Parameters.AddWithValue(" @Patient_ID_NO ",PatID);
                                cmd.Parameters.Add("@idcount", SqlDbType.Decimal);
                                //idcount.Precision = 18;
                                //idcount.Scale = 0;
                                cmd.Parameters["@idcount"].Direction = ParameterDirection.Output;
                                cmd.Connection = con;
                                cmd.ExecuteNonQuery();
                                con.Close();
                                TextBox1.Text = cmd.Parameters["@RETURNOUT"].Value.ToString();
                                ClientScriptManager cs = Page.ClientScript;
                              cs.RegisterStartupScript(this.GetType(), "IDCount", "alert('"+cmd.Parameters["@RETURNOUT"].Value.ToString()+"');", true);
                           }
                            catch
                            {
                                Message("block 3:Error", this);
 
                            }
 

[edit]Code block added - OriginalGriff[/edit]
Posted 30-Jun-12 23:06pm
Edited 30-Jun-12 23:11pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

We can't tell for the same reason you can't: Your code hides the error.
Change the catch block:
catch (Exception ex)
   {
   Message("Block 3: Error\nThe Reported fault is:\n" + ex.Message, this);
   }
That way, if the error message does not give you enough information, you can put a break point on the exception and view the detail.
  Permalink  
Comments
ooffooooo at 1-Jul-12 4:27am
   
@sandeep...i have changed the line to
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Transaction";
and i hae
ooffooooo at 1-Jul-12 4:29am
   
@OriginalGriff ..
i have pasted your lines in my code it is not showing any message , or error ,from this lines its going to catch cmd.ExecuteNonQuery();
con.Close();
ooffooooo at 1-Jul-12 4:33am
   
This is my error in execption ...
Procedure or function 'Transaction' expects parameter '@Patient', which was not supplied.
 
my stored procedure
ALTER PROCEDURE [dbo].[Transaction]
(

@Patient nvarchar(50),
@E_TO nvarchar(50),
@R_type int,
@User_id uniqueidentifier,
@ReportType nvarchar(50),
@Patient_no int,
@Patient_ID_NO numeric(18,0),
@idcount numeric(18,0) output

)
AS
BEGIN

declare @tempid numeric(18,0)
set @tempid = 0;
declare @idcnt numeric(18,0)
select @idcnt =isnull( max(idcount),0) from Transactions where year(R_date)=year(getdate())
if (@idcnt =0)
set @tempid=1
else
set @tempid = @idcnt +1


INSERT INTO dbo.Transactions (Patient,E_TO,R_date,R_from,User_id,report_type,Patient_no,Patient_ID_NO,idcount)values (@Patient,@E_TO,getdate(),@R_type,@User_id,@ReportType,@Patient_no,@Patient_ID_NO,@tempid)
 
END
OriginalGriff at 1-Jul-12 4:45am
   
Take the spaces out!
The Parameter names are not trimmed before being passed to SQL:
cmd.Parameters.AddWithValue(" @Patient ", Patient_name);
Becomes:
cmd.Parameters.AddWithValue("@Patient", Patient_name);
You will need to do this for the others as well.
 
ooffooooo at 1-Jul-12 4:51am
   
yes i did .. brother i have one more issue .. when a user login , how can i save the user name in my table , you can see in my stored procedure there is @User_id , i want to save the user name in my table with every transaction ...
because its showing this error now..
Procedure or function 'Transaction' expects parameter '@User_id', which was not supplied.
OriginalGriff at 1-Jul-12 5:08am
   
You would have to add it as a parameter to the list in your C# code - at the moment you don't set it at all. So SQL (rightly) complains. :laugh:
ooffooooo at 1-Jul-12 5:03am
   
?
ooffooooo at 1-Jul-12 5:15am
   
it is a uniqueidentifier , i will change it to nvchar ,.. will this line of code save the username ? cmd.Parameters.AddWithValue("User_ID", Label1.Text = Session["name"].ToString ());
OriginalGriff at 1-Jul-12 5:22am
   
NO! Don't change Guids to strings! Pass it the Guid - there is a constructor which takes a string if you only have it as a string in your C# for some strange reason.
 
Never convert data away from a sensible type to a generic one (particularly string) unless it is for presentation and you need a specific format.
 
And no, I suspect it won't - a "name" is unlikely to be a Guid or anything resembling a Guid. Look for where you use the userID elsewhere in your code, and get the code from that.
ooffooooo at 1-Jul-12 5:30am
   
or should i change datatype of User_ID uniqueidentifier to User_name nvarchar in my table??
ooffooooo at 1-Jul-12 5:37am
   
tell me brother waiting for your comment .. should i try it like that by changing datatype to nvarchar???
ooffooooo at 1-Jul-12 5:49am
   
??????????
ooffooooo at 1-Jul-12 6:10am
   
i tried and i can save the name in the table
OriginalGriff at 1-Jul-12 6:18am
   
Yes, but that doesn't mean it works. If there is other code which uses the UserID to refer to other data, then putting a name in where it should be a unique ID that matches a user in a different table may mean you have just broken other code, but don't know it yet...:laugh:
ooffooooo at 1-Jul-12 6:21am
   
i dint use that id any where ,,, previously i was using but i change the code for that that id....
 
how can i display the column in message box ?????? i want to display "idcount" in my message box or textbox with every transaction.
OriginalGriff at 1-Jul-12 6:23am
   
If you aren't using it, get rid of it. Don't fill it with "wrong" information, and it will come back to bite you later on.
Use the MessageBox.Show method.
ooffooooo at 1-Jul-12 6:37am
   
can you show me .... how to display it from sql table
my stored procedure
ALTER PROCEDURE [dbo].[Transaction]
(

@Patient nvarchar(50),
@E_TO nvarchar(50),
@R_type int,
@User_name nvarchar(50),
@ReportType nvarchar(50),
@Patient_no int,
@Patient_ID_NO numeric(18,0),
@idcount numeric(18,0) output

)
AS
BEGIN

declare @tempid numeric(18,0)
set @tempid = 0;
declare @idcnt numeric(18,0)
select @idcnt =isnull( max(idcount),0) from Transactions where year(R_date)=year(getdate())
if (@idcnt =0)
set @tempid=1
else
set @tempid = @idcnt +1


INSERT INTO dbo.Transactions (Patient,E_TO,R_date,R_from,User_name,report_type,Patient_no,Patient_ID_NO,idcount)values (@Patient,@E_TO,getdate(),@R_type,@User_name,@ReportType,@Patient_no,@Patient_ID_NO,@tempid)
return @idcount
END
my code
try
{
if (NameTxtBx.Text == "" || ToTxtBx0.Text == "" || DropDownList1.SelectedIndex == 0 || RadioButtonList2.SelectedIndex == -1 || ( PatID_NO.Text == "")|| (Convert.ToUInt64(PatNo.Text) <= 0) || (Convert.ToUInt64(PatNo.Text) > 200000000))
{
Message("Please check the field u entered", this);
return;

}
else
{
try
{


string Patient_name = NameTxtBx.Text, Export_TO = ToTxtBx0.Text, repType = RadioButtonList2.SelectedValue ;
int PatNoVal;
PatNoVal = Convert.ToInt32(PatNo.Text);
PatNoVal = int.Parse(PatNo.Text);
decimal PatID = decimal.Parse(PatID_NO.Text);
int? replay_To_type = Int16.Parse(DropDownList1.SelectedValue);
string idcount;
 
try
{
 
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Transaction";
cmd.Parameters.AddWithValue("@Patient", Patient_name);
cmd.Parameters.AddWithValue("@E_TO", Export_TO);
cmd.Parameters.AddWithValue("@R_type", repType);
cmd.Parameters.AddWithValue("@ReportType", replay_To_type);
cmd.Parameters.AddWithValue("@Patient_no", PatNoVal);
cmd.Parameters.AddWithValue("@Patient_ID_NO", PatID);
cmd.Parameters.AddWithValue("@User_name", Label1.Text = Session["name"].ToString ());
cmd.Parameters.Add("@idcount", SqlDbType.Decimal);
cmd.Parameters["@idcount"].Direction = ParameterDirection.Output;
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
TextBox1.Text = cmd.Parameters["@RETURNOUT"].Value.ToString();
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(this.GetType(), "IDCount", "alert('" + cmd.Parameters["@RETURNOUT"].Value.ToString() + "');", true);
 
}
 
catch (Exception ex)
{
Message("Block 3: Error\nThe Reported fault is:\n" + ex.Message, this);
}
 
PatNo.Text = "";
OriginalGriff at 1-Jul-12 6:48am
   
This is the last query on this that I will answer - you really should open new queries for each of these, as tehy are not related to the original.
 
Looking at your SP, it returns the value you want - so replace ExecuteNonQuery with ExecuteScalar and it should return an integer value you can display.
 
I think you need to go back and look at what you are doing - these are very basic questions for the level of code you are using.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Is it even compiling?
 
For instance, this line:
SqlCommand cmd = new SqlCommand;
  Permalink  
Comments
ooffooooo at 1-Jul-12 5:03am
   
?

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

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 256
1 OriginalGriff 251
2 BillWoodruff 240
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 1 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100