Pulling String from Database. Says Cannot Implicitly Convert to 'string' - c#

In my database I have a field that is storing a simple string. The user can edit it, as it will be used as the text on a button later.
The storing of the string is working fine, but when I try to pull the string out again in case the user wants to edit it, the code behind is showing me the error:
Cannot implicitly convert type 'store.storeProductsDataTable' to 'string'
I have an XSD with a super basic get statement to retrieve the text and the field in the database is flagged as type text. Like I mentioned, it's storing the string great. Also on the same page, it successfully retrieves the product's name, description, price, etc using the same method.
All I am trying to do with the string is apply it to a textbox with the following code:
customBuyNowText.Text = pdb.getBuyNowText(pID);
Any suggestions on what I could be missing for it to cause this error?

I'll try this yet again in C#: if it's a datatable, try:
DataTable dt = pdb.getBuyNowText(pID);
customBuyNowText.Text = dt.Rows[0]["your_column_name"].ToString();

Related

How to convert a Guid when copying data from a database into private members - C# ASP.NET

I am trying to copy data from my database into private data members (so that it ultimately may be displayed in an ASP Listbox). I have easily achieved this with strings and integers, e.g.:
Forename = Convert.ToString(DB.DataTable.Rows[0]["Forename"]);
PhoneNumber = Convert.ToInt32(DB.DataTable.Rows[0]["PhoneNumber"]);
The data for forename and phone number can consequently be passed and used in a listbox. When trying to achieve the same for the primary key, a uniqueidentifier/GUID, the data doesn't pass (it shows up as empty/null/lots of 0s in place of the actual value). I've tried this:
UserID = new Guid(Convert.ToString(DB.DataTable.Rows[0]["UserID"]));
AND
UserID = new Guid(DB.DataTable.Rows[0]["UserID"].ToString());
I've also tried this, which results in the error 'cannot convert from object to byte[]':
UserID = new Guid(DB.DataTable.Rows[0]["UserID"]);
I'm clueless on how to proceed. Any help's appreciated. To reiterate: The first two solutions I've tried don't result in errors, but the data doesn't pass (it shows up as 0s/empty in a listbox).
Any help's appreciated. Thank you.
Make your UserID member a string, and just use Convert.ToString(DB.DataTable.Rows[0]["UserID"])

How to format from SqlDataReader string to currency format in Asp.net?

I retrieve value from database like this 173557.0000. I want to display like 1,73,557.00.
This is my code what tried
Table1001Hour.Rows[i].Cells[0].Text = rd["hour"].ToString();
Table1001Hour.Rows[i].Cells[1].Text = string.Format("{0:C}", Convert.ToInt32(rd["salesCost"].ToString()));
But it throws an error message.
Input string was not in a correct format.
If I don't convert to string it shows like same as it is 173557.0000
Thanks
It looks like the problem is here:
Convert.ToInt32(rd["salesCost"].ToString())
So... don't do that? The data coming back from the database is probably just an int, so: (int)rd["salesCost"] (or, if you're unsure of the specific type, Convert.ToInt32(rd["salesCost"])) should be fine. You could also then use the overload ToString; giving you:
Table1001Hour.Rows[i].Cells[1].Text = ((int)rd["salesCost"]).ToString("C");
(although based on the .0000, it could be decimal or double - the latter of which would be a very bad choice for financial data)
To be honest, I think it is a mistake to have UI code and DB code touching each-other directly, but that might just be me. If it were me, I'd be populating an object model from the database, and separately writing the object model to the UI.

Retrieve Resource Values Programmatically in asp.net C#

I want to get strings from my App_resource programmatically because I get from the DB a keyword with the name of the string in the app_resource mlbRes.
I have a stored procedure that indicates what the user should do next, sending a string that corresponds to a string in my app_resources file. I can do it with a switch, but that will only complicate my code.
As you can see in the following screenshot, I've tried different approaches, but none worked so far.
screenshot
Can anyone point me out what I'm doing wrong?
Thanks in advance.
Use this Syntax Here mlbRes is your resource file name
string value= Resources.mlbRes.ResourceManager.GetString("StringName");

How to enter a parameter in Winform to be used in ReportViewer

I am trying to learn how to use reportviewer in VS 2013. I know how to add a report and load it up with data but I am trying to add a parameter. The MS Access database contains one table called BasicForm and has 4 fields ID, firstName, lastName, and note.
this.BasicFormTableAdapter.Fill(this.FormFillDBDataSet.BasicForm);
this.reportViewer1.RefreshReport();
this is the code to load the report. Now according to YouTube Instruction I have to enter this SQL syntax.
SELECT BasicForm.ID, BasicForm.firstName, BasicForm.lastName, BasicForm.note
FROM BasicForm
WHERE BasicForm.firstName=#x
Then I have to enter to go back to code where I load up the report and enter the string that would be the value of parameter, as written below:
this.BasicFormTableAdapter.Fill(this.FormFillDBDataSet.BasicForm, "Gaston");
this.reportViewer1.RefreshReport();
but it does not work because VS will give me a SQL syntax error so I changed it as such:
SELECT BasicForm.ID, BasicForm.firstName, BasicForm.lastName, BasicForm.note
FROM BasicForm
WHERE BasicForm.firstName=[x]
Just as I would write it in MS access and it would work but then when I went back to the code to add the string that would be the value of the parameter, VS would give me an error message of unknown method. I am sure I am writing the syntax wrong but I am new to c# so no idead how to rewrite it the correct way.
Thank you for helping

Error in inserting statement when inserting

I am working on c# project and using winform.
Here the problem is the query was working previously but now it is not working
Here the todaydate is a datetimePicker which is set to short date format
and my datatype of column is smalldatetime the error i am getting is
The conversion of a nvarchar data type to a
smalldatetime data type resulted in an out-of-range value.
The statement has been terminated.
if i have two date time picker one for date and second for time then how can i insert? please can you guide me
AddWithValue determines the datatype of the parameter from the value you pass.
In your case you are passing a string and thus the parameter is passed to the database as a string not as a datetime expected by the database
you should change that line
cmd.Parameters.AddWithValue("#today", todaydate.Value);
You're currently passing in the text value, which means something else is having to then parse it as a date. Don't do that. Given that you've got a DateTimePicker, you should just use the DateTime value it provides:
cmd.Parameters.AddWithValue("#today", todaydate.Value);
... or create the parameter first with a specific type (SqlDbType.SmallDateTime), then set the value using todaydate.Value. The important point is to avoid the string conversion.
Wherever possible, you should avoid converting values into text. Keep them in their "natural" form (e.g. DateTime in this case) for as much of the time as possible: on input, parse into the natural form, and if you can avoid ever converting to a string, do so!
I think your time7 column in database is smalldatetime and you tried to assign it a string. I don't suggest it.
Try with Add() method like this;
command.Parameters.Add("#today", SqlDbType.SmallDatetime);
command.Parameters["#today"].Value = todaydate.Value;
or you can use AddWithValue() as also like this;
cmd.Parameters.AddWithValue("#today", todaydate.Value);

Categories

Resources