I have an application that consists of a DataGridView and a few buttons to manage it more easily (it is about managing gym subscriptions). I add the row to the DataGridView like this.
DataTable dataTable = ((SubscriptionsDatabaseDataSet)(tableBindingSource.DataSource)).Tables[0];
dataTable.Rows.Add(new object[] {id, name, dateMade, expiryDate, daysRemaining, sessionsRemaining, cardType});
and after that, in order to save the data to the database I do this:
this.tableTableAdapter.Update(this.subscriptionsDatabaseDataSet);
this.tableTableAdapter.Update(this.subscriptionsDatabaseDataSet.Table);
The card type is in cyrillic and it appears fine if I do not call tableTableAdapter.Update(), it only gets broken and appears as question marks after I call the method.
Q: What's the data type of the column in database?
A: VARCHAR (MAX).
You should change the type of the column to NVARCHAR(MAX) to support variable length unicode data.
Related
Is there a way to get the actual encoded string saved in the Database of column with DataType Bytea. Because when the record is fetched in C# it returns as System.Byte[] which I don't want. I want the data which is saved in that column
E'\\\142\\\247\\\ and so on till the data ends.
I will appreciate your support
When I am querying the data through
SELECT tpl::TEXT from Cards where ecode="xyz";
I get the following error
Error: Cannot cast type bytea to text
Line1: Select tpl::TEXT from cards
Thank you
Like this
As you see that the Bytea column is showing System.Byte[] which was overwritten by my application because the code in C# stores the data in the DataTable column as System.Byte[] while updating the data I didn't decode it and update it .
I am using Navicat premium when I query data it shows me the result when I right click on the grid result and copy as insert statement it shows me two result for different rows
like this
INSERT INTO "public"."cards" ("ecode", "tpl") VALUES ('4210149888659', E'System.Byte[]');
INSERT INTO "public"."cards" ("ecode", "tpl") VALUES('3650257637661',E '\\247\\377\\001\\021\\340\\000\\230\\000\\002U\\000e\\000\\362\\000\\002-\\000\\253\\000p\\000\\002\\207\\000~\\000g\\000\\002\\215\\000{\\000\\317\\000\\002\\334\\000h\\000\\222\\000\\001|\\000\\004\\001U\\000\\002\\202\\000K\\000\\201\\000\\001\\000\\000\\204\\000\\241\\000\\001w\\000\\213\\000\\305\\000\\002\\021\\000V\\000\\237\\000\\002L\\001=\\001\\364\\000\\001X\\001"\\001\\313\\000\\002J\\000\\010\\001\\324\\000\\001\\370\\000\\037\\001J\\000\\002;\\0017\\000\\202\\000\\002\\300\\000\\317\\0007\\000\\002\\215\\000[\\000\\004\\011\\017\\007\\012\\005\\015\\014\\006\\016\\012\\007\\010\\005\\005\\007\\011\\010\\001\\004\\012\\017\\002\\003\\010\\012\\004\\010\\005\\003\\013\\014\\005\\017\\007\\003\\010\\003\\001\\011\\004\\012\\006\\020\\011\\005\\013\\015\\010\\002\\004\\005\\010\\007\\011\\012\\000\\002\\002\\020\\012\\003\\015\\000\\005\\002\\017\\003\\000\\006\\016\\020\\010\\017\\014\\000\\001\\012\\001\\010\\011\\002\\004\\007\\010\\000\\002\\006\\011\\007\\003\\020\\011\\003\\001\\005\\011\\000\\007\\002\\012\\002\\000\\020\\000\\016\\004\\017\\004\\003\\011\\017\\000\\003\\004\\000\\001\\007\\017\\002\\001\\017\\014\\006\\002\\016\\015\\011\\015\\006\\014\\016\\010\\020\\013\\000\\003\\006\\015\\002\\005\\020\\015\\016\\015\\004\\001\\003\\015\\010\\010\\006\\014\\002\\007\\020\\014\\011\\001\\000\\014\\010\\003\\016\\001\\015\\017\\020\\013\\006\\013\\016\\013\\011\\001\\014\\013\\004\\013\\002\\013\\001\\000'
);
You can't just convert it because PostgreSQL can't guarantee it can be converted safely. The best you can do is to convert the escaped form into a string and that's not what you probably want. Keep in mind that since bytea is binary data there is no way that PostgreSQL can be sure that the string that comes out will be legit. You could have embedded nulls in a UTF-8 string, for example, which could cause some fun with buffer overruns if you aren't careful.
This is the sort of thing that should be done in the client-side and you should assume that the data is binary, and not necessarily a valid string. If you want to store strings, store text fields, not bytea.
I am using a dataset and a table adapter to populate a datagridview. In my SQL statement I am using the RTrim function for two of the columns. For both of them, I am setting the result variable to the same name as the original column name.
This works, but then I cannot update the data using the dataset, because the trimmed values are read-only.
What I want is to fill a datagridview with trimmed values, and then be able to update using the same dataset. This seems simple, yet it will not allow me to do this. Everything updates except the two columns that I used Trim on.
Here is the SQL statement I am using.
SELECT
PK, RTRIM(Description) AS Description, ContractNumber,
RTRIM(Status) AS Status, Active
FROM
ConstructionProjects
ORDER BY
CASE WHEN ContractNumber > 0
THEN ContractNumber
ELSE 99999
END
I know I can easily trim the cells on the client side in the Windows App., but I was looking for a way to do this at the SQL side, at the query. Is there an easy way to do this, and still be able to call the Update method?
Thanks,
Matt Fomich
A possible workaround could be tried loading the column Description and Status without the trimming operation,then hide them in your gridview. When you update your trimmed (and visible) column copy the value back to the untrimmed (and hidden) column in the same row. Then the update should work as usual.
Perhaps you should change the name of the column. (the trimmed Description and Status columns)
I have a stored procedure that returns columns with spaces and dashes in the column names. I cannot simply use the "Get Column Information" button to automatically generate a complex type for this stored procedure.
How do I handle the spaces and dashes in the field names since they are not legal characters for field names in C#?
I found the answer to this.
Go ahead and create the complex type substituting underscores for spaces in the column names or follow whatever convention you like naming the fields in the complex type.
Then, click on your function import in the Model Browser. At the bottom of the Visual Studio 2010 window, there should be a tab that says "Mapping Details". Here, you can specify the name of the column that corresponds to each field in your complex type. Type in the column names with the spaces here.
Another approach would be to use a SqlDataAdapter to execute the stored procedure. You can then use the Fill method which will store the contents into a DataSet in which you can then iterate through the rows. This works with columns that have spaces. Please refer to this post here for a detailed solution on this.
I have a C# Winforms app that is connecting to a SQL Server 2005 database. The form I am currently working on allows a user to enter a large amount of different types of data into various textboxes, comboboxes, and a DataGridView to insert into the database. It all represents one particular type of machine, and the data is spread out over about nine tables.
The problem I have is that my DataGridView represents a type of data that may or may not be added to the database. Thus, when the DataGridView is created, it is empty and not databound, and so data cannot be entered. My question is, should I create the table with hard-coded field names representing the way that the data looks in the database, or is there a way to simply have the column names populate with no data so that the user can enter it if they like? I don't like the idea of hard-coding them in case there is a change in the database schema, but I'm not sure how else to deal with this problem.
You could query the database for the names of the columns, if you're using a SqlDataReader you can use it's GetSchemaTable. Here's some sample code for doing this:
http://support.microsoft.com/kb/310107
I am using C# & MYSQL to develop a desktop application.
In one of my form I have a DataGridView (dgvBookings) and in my database table I have a table tblBookings which contains a field specialization of type BLOB.
I am selecting data with following query,
SELECT * FROM tblBookings WHERE IsActive=1;
and then binding the data with DataGridView as,
dgvBookings.DataSource = ds.Tables["allbookings"];
BUT after binding the data in gridview it shows Byte[] Array value for all rows of column specialization which is of BLOB type.
How yo solve this problem, I want the data in String format, whatever is written in that column should be show as it is there in the grid.
You'll have to convert the byte array to a string before using the DataSet:
DataTable allBookings = ds.Tables["allbookings"];
DataColumn column = allBookings.Columns.Add("NotABlobAnyMore", typeof(string));
foreach (DataRow row in allBookings.Rows) {
row[column] = Encoding.Unicode.GetString((byte[])row["specialization"]);
}
dgvBookings.DataSource = allBookings ;
In the sample I'm using Unicode, but you should use whatever encoding was used to generate the byte array (blob).
If you are going to allow updates on the new column, you'll have to convert the string back to an array of bytes (in the old column) before sending the data back to the database.
Sink the CellFormatting event and cast the byte array to a String, or cast the BLOB to a character type in your SQL query.
Well, the inherent problem with a BLOB is that it... isn't text. It is binary. It could be an image (jpg etc), an exe, or it could be text... but in which encoding? UTF8? Not simple. If it was a CLOB I'd hope it works OK - but otherwise I would fully anticipate that you'd have to parse the BLOB manually, otherwise how would it be interpreted?
For an arbitrary BLOB, the closest I can think of is to just show something like the hex... but with DataTable even that is a challenge. You could do it for a class via a TypeConverter...