fetch imagepath from database and display in image control in c# - c#

I am trying to fetch the imagepath of an image from the database and display it in an image control but the images are not being displayed. I tried :
public void FetchQimage()
{
connection.Open();
SqlCommand cmd = new SqlCommand("select image_path from Questionimage where sub_code = '" + Session["subject"] + "' ", connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
// for (int i = 0; i <= dt.Rows.Count - 1; i++)
// {
Image3.ImageUrl = dt.Rows[0]["image_path"].ToString();
}
}
The image path is in the format : D:\Project\projectxyz\image

Related

How to display gridview by using a table as condition then using select query to do the filter

I am new of the C# asp.net.
I have two tables. The first can be a setup table then second one is a table that wants to do a filter. I want use the setup table as the condition to search the second table then filter the result and display as gridview.
As example, the setup table got "S","50","BU2". then they will be condition to search the second table by using Select query
protected void Run_Click(object sender, EventArgs e)
{
try
{
string site = string.Empty;
string operation = string.Empty;
string bu = string.Empty;
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
using (SqlConnection conn = new SqlConnection(Contest))
{
string query = string.Empty;
conn.Open();
query = "Select Site, Operation, BU from [dbo].[Sampling_test]";
SqlDataAdapter adp = new SqlDataAdapter(query, conn);
adp.Fill(dt);
}
for (int b = 0; b < dt.Rows.Count; b++)
{
site = dt.Rows[b]["Site"].ToString();
operation = dt.Rows[b]["Operation"].ToString();
bu = dt.Rows[b]["BU"].ToString();
string[] operationArray = operation.Split(',');
string operationQuery = string.Empty;
for (int a = 0; a < operationArray.Length; a++)
{
operationQuery = operationQuery + "" + operationArray[a] + ",";
}
operationQuery.TrimEnd(',');
using (SqlConnection conn = new SqlConnection(Contest))
{
string query = string.Empty;
conn.Open();
query = "Select * from [dbo].[JHtest] where Site = '" +site+ "' AND Operation in ('" +operationQuery+ "') AND BU = '" +bu+ "' ";
SqlDataAdapter adp = new SqlDataAdapter(query, conn);
adp.Fill(dt1);
}
//insert dt into database
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["test"].ConnectionString))
{
conn.Open();
GridView2.DataSource = dt1;
GridView2.DataBind();
}
}
}
catch (Exception ex)
{
Response.Write("error" + ex.ToString());
}
}

How to get checkedboxlist item Id from C# winform

How i can get id when i click on item of checkedboxlist. Currently i am getting string name of the Item but i want to get id when i click on text item in checkedboxlist ?
Here is my code:
private void BindChackBox2()
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager
.ConnectionStrings["Conec"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT Id, Task FROM Mytodo_Task
Where Status='Ongoing' And Username='" + Login.recuser + "'", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
checkedListBoxongoing.Items.Add(dt.Rows[i]["Task"].ToString() +" "+ dt.Rows[i];
["Id"].ToString());
}
}
catch { }
}

How to use SQL count function

I want to show number of count in a Text Box
protected void PresentCalculator()
{
string cur_month = "May";
SqlDataAdapter da = new SqlDataAdapter("SELECT COUNT(EMP_CODE) FROM
Attendance where Month ='" + cur_month + "' and EMP_CODE='" + 2222 + "' ",
con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
txtPresentDay.Text = Convert.ToString(count);
}
}
Instead of SqlDataAdapter use a SqlCommand
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand(v=vs.110).aspx
and get the value by calling ExecuteScalar
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.110).aspx
Like this:
using(var sqlCon = new SqlConnetion())
{
using(var sqlCommand = sqlCon.CreateCommand())
{
sqlCommand.CommandText = "SELECT COUNT(EMP_CODE) FROM Attendance WHERE Month = #curMonth AND EMP_CODE= #empCode";
command.Parameters.Add("#curMonth", SqlDbType.Int).Value = curMonth;
command.Parameters.Add("#empCode", SqlDbType.Int).Value = 2222;
var count = (int)sqlCommand.ExecuteScalar();
txtPresentDay.Text = Convert.ToString(count);
}
}
I only know little about your database and your requirements, but here a little warning: you are only asking for the month, be sure that is really what you want.
So the attendance will be counted if it was on 2017/5/1, 2016/5/1, 1997/5/1 - since the month is always the same but the year changes.

How to retrieve image from database and paste to picturebox?

Any help is much appreciated, guys. I tried using the dataset and other solutions to no avail. The error I keep getting is "Parameter is not valid." My code is this:
try
{
conn.Open();
MySqlCommand comm = new MySqlCommand("SELECT lastname, firstname, picture FROM casestudyprofile WHERE caseid = " + id, conn);
MySqlDataAdapter adp = new MySqlDataAdapter(comm);
DataTable dt = new DataTable();
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
lblname.Text = dt.Rows[0]["firstname"].ToString() + " " + dt.Rows[0]["lastname"].ToString();
pbox2.Image = Image.FromStream(new MemoryStream((byte[])dt.Rows[0]["picture"]));
}
conn.Close();
}

why it get old dataset

My program is working normally when I select a listbox item.it getting datas correct dataset and correctly.when I write to textbox,my source method working and finding right results.but after when I selected listbox item it bringing old dataset datas to screen.What can I do in this methods?
public void ara(string str, int neyegore)
{
sqlcon.Open();
if (neyegore == 0)
komut = new SqlCommand("select * from FilmTablo WHERE ad LIKE '%" + str + "%'", sqlcon);
else if (neyegore == 1)
komut = new SqlCommand("select * from FilmTablo WHERE tur LIKE '%" + str + "%'", sqlcon);
else if (neyegore == 2)
komut = new SqlCommand("select * from FilmTablo WHERE yonetmen LIKE '%" + str + "%'", sqlcon);
else if (neyegore == 3)
komut = new SqlCommand("select * from FilmTablo WHERE oyuncular LIKE '%" + str + "%'", sqlcon);
sdr = null;
sdr = komut.ExecuteReader();
dt2 = new DataTable();
dt2.Load(sdr);
ds2 = new DataSet();
ds2.Tables.Add(dt2);
listBox1.Items.Clear();
// sayac2 = 1;
int i = 0;
while (i < ds2.Tables[0].Rows.Count)
{
listBox1.Items.Add(ds2.Tables[0].Rows[i].ItemArray[0]);
i++;
}
sqlcon.Close();
}
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
txtad.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[0].ToString().Trim();
txttur.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[1].ToString().Trim();
txtsure.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[2].ToString().Trim();
txtyonetmen.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[3].ToString().Trim();
txtoyuncular.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[4].ToString().Trim();
txtsenaryo.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[5].ToString().Trim();
byte[] resim = (byte[])ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[7];
image1.Source = GetBitmapImage(resim);
}
public void listeyiguncelle()
{
sqlcon.Open();
komut = new SqlCommand("select * from FilmTablo", sqlcon);
sdr = null;
sdr = komut.ExecuteReader();
dt = new DataTable();
dt.Load(sdr);
ds = new FilmDataDataSet();
ds.Tables.Add(dt);
listBox1.Items.Clear();
int i = 0;
//MessageBox.Show(ds.Tables[1].Rows.Count.ToString());
while (i < ds.Tables[1].Rows.Count)
{
listBox1.Items.Add(ds.Tables[1].Rows[i].ItemArray[0]);
i++;
}
sqlcon.Close();
lblfilmsayisi.Content = ds.Tables[1].Rows.Count;
}

Categories

Resources