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();
}
Related
I am trying to have the item selected from the combo box (Site PSI) auto-populate all the textboxes to the right. I have scoured the web and there is some data source selections and auotocomplete sections but they are not working. I have tried several steps that were resolved with a similar issue on this website but it is not working. Here is my code. the combobox is called cmbSitePSI and the text boxes txtSiteName, txtSiteLName, etc...I was able to autopopulate them from a grid but I am not using a grid. Please help, thank you so much in advance.
con.Open();
SqlDataAdapter da = new SqlDataAdapter(#"Select * from Site order by SitePSI", con);
DataTable dt = new DataTable();
da.Fill(dt);
SqlCommand cmd = new SqlCommand("Select SiteName, SiteLongName, SiteAddress1, SiteAddress2, SiteAddress3, SiteCity, SiteState, SiteZipCode, SiteCountry, " +
"SiteOperationsRegion from Site where SitePSI = '" + cmbSitePSI.Text + "'", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#SitePSI", int.Parse(cmbSitePSI.Text));
SqlDataReader dr = cmd.ExecuteReader();
dt1.Load(dr);
UPDATE
I have tried using this method but I have not been successful. I get squiggilies on these lines string sSiteName = r.GetString("SiteName");
private void CmbSitePSI_SelectedIndexChanged(object sender, EventArgs e)
{
con.Open();
SqlDataAdapter da2 = new SqlDataAdapter(#"Select * from Site where SitePSI = '" + cmbSitePSI + "'", con);
string constring = "Data Source=*********;Initial Catalog=DIETS;Integrated Security=True";
string Query = "Select * from Site where SitePSI = '" + cmbSitePSI.Text + "' ;";
SqlConnection condata = new SqlConnection(constring);
SqlCommand cmddata = new SqlCommand(Query, condata);
SqlDataReader r;
try
{
condata.Open();
r = cmddata.ExecuteReader();
while (r.Read())
{
string sSiteName = r.GetString("SiteName");
string sSiteLongName = r.GetString("SiteLongName");
txtSiteName.Text = sSiteName;
txtSiteLName.Text = sSiteLongName;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
I searched for above query and got so many answers but none of that answer is helpful for me. Hence Posting this question.
I have a gridview with Two dates.
From
To
When I select two dates and if data between two dates is available in database then it will get displayed on gridview. If data is not available then I want to show a Message on Label i.e. "No Records Found"
Here is my code.
myConn.Open();
SqlCommand cmd = new SqlCommand(#"select User_id , LoginDate from LoginLog where LoginDate between ('" + TextBox1.Text + "') and ('" + TextBox2.Text + "')", myConn);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;
if (GridView1.Rows.Count > 0)
{
GridView1.DataBind();
}
else
{
Lab4.Text = "No Records Found ";
}
myConn.Close();
Add this to your ASPX file :
<asp:GridView ID="GridView1" runat="server" CellPadding="5" BorderStyle="Ridge" ShowHeaderWhenEmpty="true" EmptyDataText="No Records Found." EmptyDataRowStyle-ForeColor="Red">
And your CS will be :
myConn.Open();
SqlCommand cmd = new SqlCommand(#"select User_id , LoginDate from LoginLog where LoginDate between
('" + TextBox1.Text + "') and ('" + TextBox2.Text + "')", myConn);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;
if (dt.Rows.Count > 0)
{
GridView1.DataBind();
}
myConn.Close();
Or you can see this link:
Show asp.net Gridview Header when no data/records found
Add this in gridview control
EmptyDataText="No Records Found"
Just check whether DataTable contains values or not.
myConn.Open();
SqlCommand cmd = new SqlCommand(#"select User_id , LoginDate from LoginLog where LoginDate between
('" + TextBox1.Text + "') and ('" + TextBox2.Text + "')", myConn);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;
if (dt.Rows.Count > 0)
{
GridView1.DataBind();
}
else
{
Lab4.Text = "No Records Found ";
}
myConn.Close();
hope this helps. Regards :)
Try this method
if (dt.Rows.Count > 0)
{
GridView1.DataBind();
}
else
{
Lab4.Text = "No Records Found ";
}
myConn.Close();
private void Retrieve()
{
if(loadPositions() != null){
dgvEmployeePositions.DataSource = loadPositions();
}
else{
Lab4.Text = "No Records Found ";
}
}
private DataTable loadPositions()
{
DataTable dt = new DataTable();;
myConn.Open();
String q = "your query here";
MySqlCommand cmd = new MySqlCommand(q, connectionString);
MySqlDataReader r = cmd.ExecuteReader();
if(r.hasRows){
dt.Load(r);
return dt;
}
else{
return null;
}
}
Hi before you mark this as a duplicate I have looked and tried others and had no luck. I keep getting the error for the string getBrand saying that:
not all code paths return a value.
private string getBrand(string id)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select brand from tbl_products where productId = '" + id + "'";
cmd.ExecuteNonQuery();
con.Close();
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
getBrand = dt.Rows[0][0].ToString();
}
Below is where I get the string 'id' that pass to the getBrand String wish the run the query from.
for (int i = 0; i < salesGridView.Rows.Count; i++)
{
table2.AddCell(new Phrase(salesGridView[1, i].Value.ToString(), normFont));
string id = salesGridView[0, i].Value.ToString();
table2.AddCell(new Phrase(getBrand(id), normFont));
}
You've stored the dt.Rows[0][0].ToString(); into the method's name. You need to return the following line from your method:
return dt.Rows[0][0].ToString();
Or store it in a different variable's name and then return that variable. Like this:
var temp = dt.Rows[0][0].ToString();
return temp;
You should do it this way:
private string getBrand(string id)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select brand from tbl_products where productId = '" + id + "'";
cmd.ExecuteNonQuery();
con.Close();
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
return dt.Rows[0][0].ToString();
}
I want to write code to compare the value for Serial number and equipment id with the column in SQL Server. If the value that user search have been deleted or remove I want to show " no record found".
My problem here is I do not know how to write the code for if statement.
Here is my code:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=5CG50749V3\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("Select * FROM [Equipment] WHERE SerialNumber='" + SerialNumber.Text + "' or EquipmentID ='" + EquipmentID.Text + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
SerialNumber.Text = dt.Rows[0][5].ToString();
EquipmentID.Text = dt.Rows[0][4].ToString();
}
it will show a popup if the record is not found
SqlConnection con = new SqlConnection("Data Source=5CG50749V3\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("Select * FROM [Equipment] WHERE SerialNumber='" + SerialNumber.Text + "' or EquipmentID ='" + EquipmentID.Text + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count != 0) // this will check weather any records are returned
{
// If record is found
SerialNumber.Text = dt.Rows[0][5].ToString();
EquipmentID.Text = dt.Rows[0][4].ToString();
}
else //if no record is found it will display alert
{
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('no record found');", true);
}
You can use the rows.count of the datatable (dt)
If (dt.rows.count == 0)
{
' no rows returned, notify user
}
give it a try
if (dt != null && dt.Rows.Count >0)
{
//Then your logic
}
else
{
//popup script
}
SqlCommand cmd1 = new SqlCommand("select * from app where date='" + dateTimePicker2.Value + "'", connection);
// MessageBox.Show(cmd1.CommandText);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
DataTable dt = new DataTable();
da.Fill(dt);
//dataGridView1.DataSource = dt;
connection.Open();
if (dt.)
{
textBox1.Text = dt.Rows[0]["company"].ToString();
textBox2.Text = dt.Rows[0]["place"].ToString();
dateTimePicker1.Text = dt.Rows[0]["date"].ToString();
textBox3.Text = dt.Rows[0]["time"].ToString();
}
else
{
}
connection.Close();
control_logoff();
adminlogin();
}
In my application i want to check if data table does not have records it should not throw the error like there is no row at position 0.
How about
if(dt.Rows.Count() != 0){}
if(dt.Rows.Count > 0) should do it.