Display arabic names in ASP.Net from database - asp.net

I have the following query in ASP.Net page:
SELECT 'محاولة'
This query is printing a hard coded Arabic word. I am saving this record in a DataTable and then printing on page in GridView (by passing that DataTable to GridView) but this Arabic word is being printed as square boxes.
How to print this word properly on ASP.Net page? I tried using following but it didn't work.
SELECT N'محاولة'
EDIT
Here's my code.
DataTable dt = new DataTable();
string sql = "SELECT 'محاولة'";
command.Connection = con;
command.CommandType = CommandType.Text;
command.CommandText = sql;
reader = command.ExecuteReader();
dt.Load(reader);
And then I am passing this "dt" to gridview control.
GridView1.DataSource = dt;
GridView1.DataBind();
Grid view is printing that Arabic word as square boxes (screenshot below).

Let assume your table structure is as fallow.
Create table Arabic(
id int primary key identity,
Title narchar(100),
);
Now lets add some demo data.
Insert into Arabic values (N'محاوله');
After adding data correctly there you won't have any problem with loading it asp.net page.

Related

asp.net - how to display a field from data row that is in html format

I have a field of text in html format, in a row of data that I need to select and then display it in a aspx page as html.
The text contains html tags, symbols like & ; etc... and of course lots of double quotes. For example....
<li>products</li>Check out our full line of these products, bla bla bla....<br><font color="blue">View</font><td bgcolor="#000000" align="center"><b><font color="#FFFFFF">Diameter</font><li>30° Helix<li></td>
...etc
The method that I am trying to use to retrieve it with in the codebehind, gives me the error...
An exception of type 'System.Data.SqlClient.SqlException' occurred in
System.Data.dll but was not handled in user code
Additional information: Incorrect syntax near '='.
public void GetHtml()
{
string gethtml = Request.QueryString["product"];
string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string query = "SELECT htmlpull FROM ejn_html WHERE newseries LIKE = #gethtml";
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.Add("#gethtml", SqlDbType.VarChar, 70).Value = gethtml;
SqlConnection con = new SqlConnection(conString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.Connection = con;
sda.SelectCommand = cmd;
DataTable dt2 = new DataTable();
sda.Fill(dt2);
foreach (DataRow dtRow in dt2.Rows)
{
pulledhtml = dtRow[0].ToString();
}
}
The exception is triggered on the line that contains... sda.Fill(dt2);
In my aspx page I try to call the variable using...
<%= pulledhtml %>
This works fine when I try to retrieve any other string variables that contain normal alphanumeric characters. This is a very large database that I inherited and I am not allowed nor would I want to edit the html in those fields.
How can I retrieve this html code and display it without exception errors?
Your sql text is incorrect
SELECT htmlpull FROM ejn_html WHERE newseries LIKE = #gethtml
is invalid. you can't use LIKE and = together.
Change you sql to either
SELECT htmlpull FROM ejn_html WHERE newseries = #gethtml
for an exact match or
SELECT htmlpull FROM ejn_html WHERE newseries LIKE #gethtml
where you define the parameter value with wildcards
cmd.Parameters.Add("#gethtml", SqlDbType.VarChar, 70).Value = "%" + gethtml + "%";
for a match where the value of gethtml is contained in the column value.

Binding Local Dataset To Reportviewer In ASP.net C#

I want to bind dataset's data to ReportViewer object , I create rdlc report and set it as ReportViewer's report source in design view , but it doesn't show anything ,I checked dataset's data by binding gridview and it's work and show the data in gridview but in ReportViewer object doesn't , Please Help . Here is the code:1(Note: "Temp" is the table's Name of database , that's i want to bind to ReportViewer)2(I don't want to use xsd dataset item)
string path = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";
SqlConnection con1 = new SqlConnection(path);
con1.Open();
const string query = "select * From [Table1] where date between #from and #to";
SqlCommand com1 = new SqlCommand(query, con1);
com1.Parameters.AddWithValue("#from", DateTime.Parse(r1));
com1.Parameters.AddWithValue("#to", DateTime.Parse(r2));
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataSet ds1 = new DataSet();
adp1.Fill(ds1);
com1.ExecuteNonQuery();
con1.Close();
GridView1.DataSource = ds1;
GridView1.DataBind();
ReportViewer1.Reset();
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Temp", ds1.Tables[0]));
ReportViewer1.LocalReport.Refresh();
If you have not done so, you also need to add controls like textboxes or tables to the report and create expressions that reference the fields in the dataset. For example, you could click on the Toolbox in visual studio, and drag a textbox to the report designer. Then right click the textbox, and choose Expression. When the Expression dialog box appears, look in the Category section for Fields(nameofYourDataset), click it, and look in the Values section for the names of fields from your dataset. Double click one of them which will populate the top section, and click OK.

Get Column Names from SQLDatasource

So I'm using a SQL data source that fires a Store procedure using the selected value of a drop down list as a parameter to return one of a multitude of differing tables. Then I have a gridview that uses this SQLdatasource as it's datasource to display the returned values.
What I want is to be able to get at the column names at run time so that I can use it in a later method. However the SQLDatasource doesn't seem to have a property to get at this data and when I try to get at the columns of the Datagridview after I databind it the gridview only shows a single column (the auto-generated select column). Is there a way to get at this?
I don't know about using the SQLDatasource, but you can do this easily in the codebehind if you load your data into a DataTable. E.g.
var dt = new DataTable();
var connectionString = "your connection string";
using (var db = new SqlConnection(connectionString))
{
var command = new SqlCommand("stored_procedure_name", db);
command.CommandType = CommandType.StoredProcedure;
db.Open();
var reader = command.ExecuteReader();
dt.Load(reader);
}
var columns = dt.Columns;
Each column has a ColumnName property. You can use Linq to get the column names as an array:
var columnNameArray = dt.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToArray();

Get value of BLOB field and show/hide a label based on it

I'm calling some data from my Oracle table and I have a field which is called 'image1' which is a BLOB field.
Sometimes an image is input into the field and others it's not. I need a way to read the BLOB field to see if its a blank and then show/hide my label based on its contents.
Here's my source code
query = OracleConn.CreateCommand()
query.CommandText = "select * from articles"
DataTable = New DataTable()
OracleDataAdapter = New OracleDataAdapter(query)
OracleDataAdapter.Fill(DataTable)
GridView1.DataSource = DataTable.DefaultView
GridView1.DataBind()
If DataTable.Rows(0).Item("image1") = "" Or DataTable.Rows(0).Item("image1") Is Nothing Then
lbl1.Visible = False
End If
This doesn't seem to do what it's suppose to do.
Any ideas how i can accomplish my goal?
Thanks
You should test to see if image1 is DBNull:
If DataTable.Rows(0).Item("image1") Is DBNull.Value Then

SqlDataSource.Select()? How do I use this? (ASP.net)

I'm trying to retrieve values using VB.NET from a SQL database. How do I use SqlDataSource.Select()? Is there a way to move the value to a variable that I can use for other things?
I know its kind of scattered and vague but that is the best I can do. I basically need to set a labels text to a value in a table.
This puts the result query in to a DataTable.
DataView view = (DataView)dataSource.Select(new DataSourceSelectArguments());
DataTable groupsTable = view.ToTable();
String value;
foreach (DataRow dr in dt.Rows)
{
// Do something here IE grab the value of the first column
value = dr[0];
}
Repying to last question in comment:
YourTable.Rows(index)(index)
YourTable.Rows(index)("columnname")
I was getting crazy trying to do this simple operation:
retrieving data from sqldatasource and put it into variables that I can manipulate.
At the end, Here the working behind code to do this for VB.NET:
Dim DV As New DataView()
Dim DataTable As New DataTable()
Dim SqlDataSource1 As New SqlDataSource()
Dim VALUE As String
SqlDataSource1.ID = "SqlDataSource1"
Me.Page.Controls.Add(SqlDataSource1)
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Connection_name").ConnectionString
SqlDataSource1.SelectCommand = "SELECT * from Table"
DV = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
DataTable = DV.ToTable()
For Each riga As DataRow In DataTable.Rows
VALUE = riga("table_name").ToString
Next
the for each, in this case gets only the first value but you can get any value from datatable and put it into vector, or other strings, so you can control data coming from sqldatasource.
ENJOY

Resources