I am passing some value from one page to another page via query string, it passes the values correctly and then passes to the stored procedure succesfully but typing the Request.QueryString["something"] again and again is very irritating, so I created a method for it, but the method does not return/passes any value and the nullreference exception is thrown.
protected void Page_Load(object sender, EventArgs e)
{
try
{
using (Property_dbDataContext context = new Property_dbDataContext())
{
_errMsg.Enabled = false;
_errMsg.Visible = false;
var find_prop = context.find_property(val("city"), val("type"), val("subtype"), val("bedrooms"), val("size_unit"), Convert.ToInt32(val("area_from")), Convert.ToInt32(val("areato")), Convert.ToInt32(val("pricefrom")), Convert.ToInt32(val("priceto"))).ToList();
//code above does not return any value
//var find_prop = context.find_property(Request.QueryString["city"], Request.QueryString["type"], Request.QueryString["subtype"], Request.QueryString["bedrooms"], Request.QueryString["size_unit"], Convert.ToInt32(Request.QueryString["area_from"]), Convert.ToInt32(Request.QueryString["areato"]), Convert.ToInt32(Request.QueryString["pricefrom"]), Convert.ToInt32(Request.QueryString["priceto"])).ToList();
//code above return value from the database but its a long procedure
rptr_properties.DataSource = find_prop;
rptr_properties.DataBind();
}
}
catch(Exception ex)
{
_errMsg.Enabled = true;
_errMsg.Visible = true;
_errMsg.Text = "Sorry! Property not found." + ex;
}
}
public string val(string a)
{
return Request.QueryString["" + a + ""].ToString();
}
You need to make sure the item exists before trying to access it:
public string val(string a)
{
if(Request.QueryString[a] != null)
return Request.QueryString[a].ToString();
return string.Empty;
}
In reference to your comment; this in particular:
Convert.ToInt32(val("area_from"))
Is going to cause a problem because the default value is an empty string.
Something like this should work for now though:
public string ValToString(string a)
{
if(Request.QueryString[a] != null)
return Request.QueryString[a].ToString();
return string.Empty;
}
public int ValToInt32(string a)
{
if(Request.QueryString[a] != null)
return Convert.ToInt32(Request.QueryString[a]);
return 0;
}
You would then modify your find code:
var find_prop = context.find_property(ValToString("city"), ValToString("type"), ValToString("subtype"), ValToString("bedrooms"), ValToString("size_unit"), Convert.ToInt32(val("area_from")), Convert.ToInt32(val("areato")), ValToInt32("pricefrom"), ValToInt32("priceto")).ToList();
This will work
public string val(string a)
{
return Request.QueryString[a];
}
Better method for converting a querystring value to an int(32):
public int GetIntFromQueryString(string key)
{
int result = int.MinValue;
if(Request.QueryString[key] != null)
{
int.TryParse(Request.QueryString[key].ToString(), out result);
}
return result;
}
If TryParse fails to convert the value the method will return 'int.MinValue', you can then check the returned value does not equal 'int.MinValue' and go from there.
Related
I have followed the code shown on this site: "https://www.veonconsulting.com/integrating-sap-using-nco/",
I'm trying to get data from the function BAPI_PRODORD_GET_DETAIL, I tested <bapi_prodord_get_detail><number>1000262</number><struct><order_objects><header>X</header><operations>X</operations><components>X</components></order_objects></struct></bapi_prodord_get_detail>, on SAP GUI, I can see data for Header, Components & Operations, but in the code, I 'm not getting the same response, could you please help.
Code:
public bool testConnection()
{
bool state = false;
string rfcRequest = "<RFC_READ_TABLE><QUERY_TABLE>MARD</QUERY_TABLE><DELIMITER>*"
+ "</DELIMITER><ROWSKIPS>0</ROWSKIPS><ROWCOUNT>0</ROWCOUNT><TABLE><OPTIONS><ROW>"
+ "<TEXT>MATNR IN (</TEXT></ROW><ROW><TEXT>'testConnection'</TEXT></ROW><ROW>"
+ "<TEXT>)</TEXT></ROW></OPTIONS></TABLE></RFC_READ_TABLE>";
Utils.RfcClient client = new Utils.RfcClient();
try
{
XElement response = client.PullRequestToSAPrfc(rfcRequest);
state = true;
}
catch (RfcLogonException ex)
{
Console.Write("Logon Failed");
}
catch (RfcInvalidStateException ex)
{
Console.Write("RFC Failed");
}
catch (RfcBaseException ex)
{
Console.WriteLine("communication error" + ex.Message);
}
catch (Exception ex)
{
Console.Write("Connection error");
}
finally
{
//client.disconnectDestination();
}
return state;
}
public bool testConnection()
{
bool state = false;
string rfcRequest = "<bapi_prodord_get_detail><number>1000262</number><struct>"
+ "<order_objects><header>X</header><operations>X</operations><components>X"
+ "</components></order_objects></struct></bapi_prodord_get_detail>";
Utils.RfcClient client = new Utils.RfcClient();
try
{
XElement response = client.PullRequestToSAPrfc(rfcRequest);
state = true;
}
catch (RfcLogonException ex)
{
Console.Write("Logon Failed");
}
catch (RfcInvalidStateException ex)
{
Console.Write("RFC Failed");
}
catch (RfcBaseException ex)
{
Console.WriteLine("communication error" + ex.Message);
}
catch (Exception ex)
{
Console.Write("Connection error");
}
finally
{
//client.disconnectDestination();
}
return state;
}
public XElement PullRequestToSAPrfc(string XMLRequest)
{
IRfcFunction requestFn;
requestFn = PrepareRfcFunctionFromXML(XElement.Parse(XMLRequest));
RfcSessionManager.BeginContext(_ECCsystem);
requestFn.Invoke(_ECCsystem);
RfcSessionManager.EndContext(_ECCsystem);
XElement XMLResponse = PrepareXMLFromrfc(requestFn);
return XMLResponse;
}
public IRfcFunction PrepareRfcFunctionFromXML(XElement xmlFunction)
{
RfcRepository repo = _ECCsystem.Repository;
IRfcFunction RfcFunction = repo.CreateFunction(xmlFunction.Name.ToString());
foreach (XElement xelement in xmlFunction.Elements())
{
if (xelement.Name.ToString().Equals("TABLE"))
{
if (NotProcessSpecialTable(xelement))
continue;
IRfcTable options = RfcFunction.GetTable(xelement.Descendants().First().Name.ToString());
foreach (XElement row in xelement.Elements().First().Elements())
{
options.Append();
foreach (XElement rowElement in row.Elements())
{
string elementName = rowElement.Name.ToString();
RfcElementMetadata elementMeta = options.GetElementMetadata(elementName);
var elementValue = getValueAsMetadata(ref elementMeta, rowElement.Value);
if (elementValue is string && string.IsNullOrEmpty((string)elementValue)) { continue; }
options.SetValue(elementName, elementValue);
}
}
}
else if (xelement.Name.ToString().Equals("STRUCT"))
{
IRfcStructure options = RfcFunction.GetStructure(xelement.Descendants().First().Name.ToString());
foreach (XElement structElement in xelement.Elements().First().Elements())
{
string elementName = structElement.Name.ToString();
RfcElementMetadata elementMeta = options.GetElementMetadata(elementName);
var elementValue = getValueAsMetadata(ref elementMeta, structElement.Value);
if (elementValue is string && string.IsNullOrEmpty((string)elementValue)) { continue; }
options.SetValue(elementName, elementValue);
}
}
else
{
string elementName = xelement.Name.ToString();
RfcElementMetadata elementMeta = RfcFunction.GetElementMetadata(elementName);
var elementValue = getValueAsMetadata(ref elementMeta, xelement.Value);
if (elementValue is string && string.IsNullOrEmpty((string)elementValue)) { continue; }
RfcFunction.SetValue(elementName, elementValue);
}
}
return RfcFunction;
}
public XElement PrepareXMLFromrfc(IRfcFunction rfcFunction)
{
var XMLRoot = new XElement(rfcFunction.Metadata.Name);
for (int functionIndex = 0; functionIndex < rfcFunction.ElementCount; functionIndex++)
{
var functionMatadata = rfcFunction.GetElementMetadata(functionIndex);
if (functionMatadata.DataType == RfcDataType.TABLE)
{
var rfcTable = rfcFunction.GetTable(functionMatadata.Name);
var XMLTable = new XElement(functionMatadata.Name);
foreach (IRfcStructure rfcStracture in rfcTable)
{
XElement XMLRow = new XElement("ROW");
for (int i = 0; i < rfcStracture.ElementCount; i++)
{
RfcElementMetadata rfcElementMetadata = rfcStracture.GetElementMetadata(i);
if (rfcElementMetadata.DataType == RfcDataType.BCD)
{ XMLRow.Add(new XElement(rfcElementMetadata.Name, rfcStracture.GetString(rfcElementMetadata.Name))); }
else
{
XMLRow.Add(new XElement(rfcElementMetadata.Name, rfcStracture.GetString(rfcElementMetadata.Name)));
}
}
XMLTable.Add(XMLRow);
}
XMLRoot.Add(XMLTable);
}
else if (functionMatadata.DataType == RfcDataType.STRUCTURE)
{
var rfcStructure = rfcFunction.GetStructure(functionMatadata.Name);
XElement XMLRow = new XElement(functionMatadata.Name);
for (int elementIndex = 0; elementIndex < rfcStructure.ElementCount; elementIndex++)
{
RfcElementMetadata eleMeta = rfcStructure.GetElementMetadata(elementIndex);
XMLRow.Add(new XElement(eleMeta.Name, rfcStructure.GetString(eleMeta.Name)));
}
XMLRoot.Add(XMLRow);
}
else
{
RfcElementMetadata rfcElement = rfcFunction.GetElementMetadata(functionIndex);
XMLRoot.Add(new XElement(rfcElement.Name, rfcFunction.GetString(rfcElement.Name)));
}
}
return XMLRoot;
}
# Below function is used for the data types.
private object getValueAsMetadata(ref RfcElementMetadata elementMeta, string value)
{
switch (elementMeta.DataType)
{
case RfcDataType.BCD:
return value;
case RfcDataType.NUM:
if (value.Contains("."))
{
int elementValue;
int.TryParse(value, out elementValue);
return elementValue;
}
else
{
return Convert.ToInt32(value);
}
case RfcDataType.INT1:
return Convert.ToInt32(value);
case RfcDataType.INT2:
return Convert.ToInt32(value);
case RfcDataType.INT4:
return Convert.ToInt32(value);
case RfcDataType.INT8:
return Convert.ToInt64(value);
case RfcDataType.CHAR:
return value;
case RfcDataType.DATE:
return DateTime.ParseExact(value, "yyyy-MM-dd", CultureInfo.InvariantCulture);
default:
return string.Empty;
}
}
You are confronted to the classic issue of external and internal values in SAP.
Your screenshot shows the ABAP Function Module Test screen in SAP system. When you enter a value in the screen, it may be transformed internally before calling the function module.
These are called the external and internal formats. "External" is what is shown in the User Interface (typed or displayed), "internal" is the value written to the database.
For instance, imagine a database object whose primary key is a GUID, this is the object key in internal format, but in the user interface this object is always referred or shown by its name (candidate key).
In your precise case, when a Production Order is a number, the internal format always contains leading zeroes on 12 digits, and the external format does not display them. In the Function Module Test screen, if you enter this external value:
1000262
it's converted to the following internal value and the BAPI is called with it:
000001000262
Generally speaking, when you call any function module from another program, you must indicate the internal value, because there's no user interface implied between the two.
i.e., use this XML in your method testConnection:
string rfcRequest = "<bapi_prodord_get_detail><number>000001000262</number><struct>"
+ "<order_objects><header>X</header><operations>X</operations><components>X"
+ "</components></order_objects></struct></bapi_prodord_get_detail>";
See also this answer about external and internal formats: Converting MATNR via conversion exit fails for custom table
If you would like to do the required field conversions programmatically, which are explained in Sandra Rossi's answer, you may use the RFMs BAPI_CONVERSION_EXT2INT, BAPI_CONVERSION_EXT2INT1, BAPI_CONVERSION_INT2EXT and BAPI_CONVERSION_INT2EXT1 for doing so.
However, every additional RFC call has of course a negative impact on the performance.
Besides, SAP Note 206068 is a good resource for an explanation of some RFC BAPI pitfalls which you also stepped in.
Getting a single value from by SQLite database but I get the error System.InvalidCastException: 'Specified cast is not valid.'
I need only the first value of the select results, tried both First() and ElementAtOrDefault(0) but same thing happens.
Error gets catched when trying to assing to the variable IDPromoMainPage, not before.
Basically this is how I call the value:
private void GetPromoLocal()
{
try
{
var databasePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "LocalDB.db3");
var db = new SQLiteConnection(databasePath);
IEnumerable<T_Promo> resultado = SELECT_WHERE(db);
if (resultado.Count() > 0)
{
IDPromoMainPage = Convert.ToInt32(resultado.First()); // ElementAtOrDefault(0));
}
}
catch (Exception)
{
throw;
}
}
public static IEnumerable<T_Promo> SELECT_WHERE(SQLiteConnection db)
{
return db.Query<T_Promo>("SELECT IDPromo FROM T_Promo");
}
Also have the same error happening here, exactly at line "PisterosLista = resultado.Cast().ToList();"
List<Pisteros> PisterosLista = new List<Pisteros>();
public void GetPisterosLocal()
{
try
{
var databasePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "LocalDB.db3");
var db = new SQLiteConnection(databasePath);
IEnumerable<T_Pisteros> resultado = SELECT_WHERE_Pist(db);
if (resultado.Count() > 0)
{
PisterosLista = resultado.Cast<Pisteros>().ToList();
//Console.WriteLine("content :: " + content);
Console.WriteLine("Data :: " + PisterosLista);
}
}
catch (Exception)
{
throw;
}
}
public static IEnumerable<T_Pisteros> SELECT_WHERE_Pist(SQLiteConnection db)
{
return db.Query<T_Pisteros>("SELECT * FROM T_Pisteros");
}
if the column IDPromo is an int, then just do this
IEnumerable<int> resultado = SELECT_WHERE(db);
public static IEnumerable<int> SELECT_WHERE(SQLiteConnection db)
{
return db.Query<int>("SELECT IDPromo FROM T_Promo");
}
I have three different databases that I need to check that I am connected to. This is what I originally have, which works perfectly fine.
public async Task<ServiceAvailabilityDTO> ServiceAvailabilityStatus()
{
return new ServiceAvailabilityDTO
{
IsDb1Online = await IsDb1Available(),
IsDb2Online = IsDb2Available(),
IsDb3Online = await IsDb3Available()
};
}
private async Task<bool> IsDb1Available()
{
var count = await _db1Service.GetDbCount();
if (count > 0)
return true;
return false;
}
private bool IsDb2Available()
{
if (_db2Service.GetDbCount() > 0)
return true;
return false;
}
private async Task<bool> IsDb3Available()
{
var pong = await _db3Provider.PingDb();
if(pong.Success == true && pong.Version != null)
return true;
return false;
}
Now however, I need to log exception messages in my DTO for each DB check.
public async Task<ServiceAvailabilityDTO> ServiceAvailabilityStatus()
{
return new ServiceAvailabilityDTO
{
IsDb1Online = await IsDb1Available(),
IsDb2Online = IsDb2Available(),
IsDb3Online = await IsDb3Available(this) // This is an example. I want to pass the reference of **ServiceAvailabilityDTO** to **IsDb3Available**
};
}
private async Task<bool> IsDb3Available(ServiceAvailabilityDTO availability)
{
try
{
var pong = await _db3Provider.PingDb();
if(pong.Success == true && pong.Version != null)
return true;
return false;
}
catch (Exception e)
{
var exceptionMessage = e.Message;
if (e.InnerException != null)
{
// This is what I hope to put into the object reference
exceptionMessage = String.Join("\n", exceptionMessage, e.InnerException.Message);
availability.db3Exception = exceptionMessage ;
}
return false;
}
}
My question is;
Can I keep my return method the same as in the first example, and pass the object reference to each method to store the exception and still return my bool value.
Or does the object not get created until all of the method calls have happened, and then create the object with the returned values?
I know I could just create the object normally and pass it in each
method call, but it is specifically this way of doing it that has
inspired me to ask this question, purely to be informed and learn
from.
Thanks.
No, you cannot do it like this because in the context of what you're doing this does not refer to the object you're populating, it refers to the object containing the method you're calling.
public async Task<ServiceAvailabilityDTO> ServiceAvailabilityStatus()
{
return new ServiceAvailabilityDTO
{
IsDb1Online = await IsDb1Available(),
IsDb2Online = IsDb2Available(),
IsDb3Online = await IsDb3Available(this) // here 'this' does NOT ref to ServiceAvailabilityDTO
};
}
There is no keyword which does refer to ServiceAvailabilityDTO either, so you're left with creating the object, and passing it to each method. At this point, I dont think there is much point you returning the boolean either - you may as well set the boolean property in line
public async Task<ServiceAvailabilityDTO> ServiceAvailabilityStatus()
{
var sa = new ServiceAvailabilityDTO();
await CheckDb1Available(sa);
CheckDb2Available(sa);
await CheckDb3Available(sa);
return sa;
}
(Note I've renamed the methods from Is* to Check* as the former implies a return boolean, the latter implies something going on inline.)
I am working on asp.net application. In this application i have used Url rewrinting method to show pages in the browser. Whenever any wants to edit any record i have checked whether the 'ID' to be edit is null or not as:
if (Page.RouteData.Values["ID"] != null)
{
}
If this condition will be true then it means the wants to edit the record of id passed through
Page.RouteData.Values["ID"]
from the form all the data is being displayed. and user select any one of them.the above "if" condition works fine when it is not used in any webMethod but it is used in the webmethod the error comes.
My webMethod is
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod(e)]
public static string SaveData(string Category,string SubCategory,string ProductType)
{
try
{
ProductBrand objBrand = new ProductBrand();
DataTable dt = new DataTable();
ProductType objProductType = new ProductType();
objProductType.SubCategoryID = Convert.ToInt64(SubCategory);
objProductType.ProductTypeName = ProductType;
if (Page.RouteData.Values["ID"] != null)
{
objProductType.TypeID = Convert.ToInt64(Page.RouteData.Values["ID"].ToString());
objProductType.Flag = "U";
else
{
dt = new ProductType().SelectByCategory(objProductType.SubCategoryID, objProductType.ProductTypeName);
if (dt.Rows.Count > 0)
{
objProductType.Flag = "D";
}
else
{
objProductType.Flag = "I";
}
}
if (objProductType.Flag.Equals("D"))
{
objBrand.BrandID = 2;
}
else
{
int retval = new ProductType().Insert(objProductType);
if (retval > 0)
{
objBrand.BrandID = 1;
}
else
{
objBrand.BrandID = 0;
}
}
return objBrand.BrandID.ToString();
}
catch (Exception ex)
{
throw (ex);
}
}
The above webmethod has been written to insert data into datbase using jquery. In the above webmethod in the statement "if (Page.RouteData.Values["ID"] != null)" the error is showing as
An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.RouteData.get'
But when i use the above condition outside of webmethod this error is not coming. Please help me someone here.
try remove "static" in function SaveData and call like instance
var xx = new YOURCLASS();
xx.SaveData(xx,yy,zz);
How do I call a non-static method from a static method in C#?
I have to pass a count value to another namespace here. I have 2 classes Select.cs and Value.cs. Select.cs has the following code:
public bool Login(string UserName, string Password)
{
string strinvselect = string.Format("select * from newlog where
pass='{0}'", Password);
DataTable dtlog = ExecuteStatement(strinvselect);
if (dtlog.Rows.Count == 1)
{
string strvalue = string.Format("Select * FROM login WHERE uid=
'{0}'", UserName);
DataTable newlogin = ExecuteStatement(strvalue);
try
{
if (newlogin.Rows.Count == 1)
{
loginStatus = true;
}
}
catch (Exception ex)
{
loginStatus = false;
}
return loginStatus;
}
}
I have to create a count value if(newlogin.Rows.Count == 1), count=1 and this value should be available in Value.cs to check a functionality.
In Value.cs a function getdetails() is called. Here I need to check
if (count == 1)
{
getdetails();
}
else
{
// call another function
}
I found the answer:
Namespaces are not really relevant here, the issue is that you need to access the variable from a different class. The way you have "global" variables that are specific to the user is the Session, so store the value in a Session variable and have your other class access the Session variable.
HttpContext.Current.Session["myData"] = "somevalue";
Value function
string x;
if (Session["myData"] == null)
{
// handle the data not being set
}
else
{
x = (string)HttpContext.Current.Session["myData"].ToString(); ;
}
You can check for query for count outside Login ( basically wherever you need it ) then pass it as "username + count" (you will need to split it) to login first parameter as you can avoid checking count inside Login again.