Dal code
public DataSet selectlogin(string u_name, string u_password, string u_email, string action)
{
SqlConnection con = new SqlConnection(h);
SqlCommand cmd = new SqlCommand("", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_login";
cmd.Parameters.AddWithValue("#name", u_name);
cmd.Parameters.AddWithValue("#email", u_email);
cmd.Parameters.AddWithValue("#password", u_password);
cmd.Parameters.AddWithValue("#action", action);
con.Open();
cmd.ExecuteNonQuery();
DataSet ds = new DataSet();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(ds);
return ds;
con.Close();
}
Bal code
public DataSet selectlogin(string u_name, string u_password, string u_email, string action)
{
DataSet ds = new DataSet();
ds = obj.selectlogin(u_name, u_password, u_email, action);
return ds;
}
CS code
protected void Btn_log(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = obj.selectlogin("", TextBox1.Text, TextBox2.Text,"login");
if (ds.Tables[0].Rows.Count > 0)
{
Response.Redirect("dashboard.aspx");
}
}
Stored procedure
if(#action='login')
select * from login where u_email=#email and u_pass=#password
The trouble might be here:
if (ds.Tables[0].Rows.Count > 0)
First check if the table with the index of 0 exists, then try to access the properties...
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 )
This should help. Or at least it'll tell you that the returned dataset is empty (has no tables inside).
Try this
protected void Btn_log(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = obj.selectlogin("", TextBox1.Text, TextBox2.Text,"login");
bool hasRows = ds.Tables.Cast<DataTable>()
.Any(table => table.Rows.Count != 0);
if (hasRows)
{
Response.Redirect("dashboard.aspx");
}
}
Or try it( use != operator instead of > operator)
if (ds.Tables[0].Rows.Count **!=** 0)
{
Response.Redirect("dashboard.aspx");
}
CS code
protected void Btn_log(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = obj.selectlogin("", TextBox1.Text, TextBox2.Text,"login");
if (ds!=null && ds.Tables[0].Count > 0 && ds.Tables[0].Rows.Count > 0)
{
Response.Redirect("dashboard.aspx");
}
}
Related
I tried the following way, the data set is coming and table is binding.
But data is not inserting to the assigned drop down list in the gridview while Row Editing event
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
int index = e.NewEditIndex;
DropDownList ddl = (DropDownList)GridView2.Rows[index].FindControl("Receipttypeddl");
if (ddl != null)
{
DataSet ds = new DataSet();
SqlDataAdapter da;
con.Open();
string qry;
qry = "select * from ReceiptType";
SqlCommand cmd = new SqlCommand(qry);
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
ddl.DataSource = ds;
ddl.DataTextField = "Receiptmode";
ddl.DataValueField = "Receiptmode";
ddl.DataBind();
ListItem i = new ListItem("", "");//Data is not inserting into ddl
ddl.Items.Insert(0, i);
}
DataTable dts = new DataTable();
dts = (DataTable)ViewState["Receiptdetails"];
GridView2.DataSource = dts;
GridView2.DataBind();
}
you need bind dropdown on RowDataBound.
there seems to be no need to go to dB for each item.
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
BindGridView();
}
private void BindGridView()
{
DataTable dts = new DataTable();
dts = (DataTable)ViewState["Receiptdetails"];
GridView2.DataSource = dts;
GridView2.DataBind();
}
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("Receipttypeddl");
BindDropdown(ddl);
}
}
DataTable ds = new DataTable();
private void BindDropdown(DropDownList ddl)
{
if (ds.Rows.Count == 0)
{
SqlDataAdapter da;
con.Open();
string qry;
qry = "select * from ReceiptType";
SqlCommand cmd = new SqlCommand(qry);
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
}
ddl.DataSource = ds;
ddl.DataTextField = "Receiptmode";
ddl.DataValueField = "Receiptmode";
ddl.DataBind();
ListItem i = new ListItem("", "");//Data is not inserting into ddl
ddl.Items.Insert(0, i);
}
Two Crystal reports
CrystalReport1.rpt
employeemonthly.rpt
Call these reports on signal aspx.net page.
protected void Button1_Click1(object sender, EventArgs e)
{
if (Page.IsValid)
{
if (rd.SelectedValue == "1")
{
DateTime to = Convert.ToDateTime(sdate.Text);
dbconnect a = new dbconnect();
ReportDocument rDoc = new ReportDocument();
a.OpenConnection();
a.cmd = new SqlCommand("viewattandace_repoeting",a.con);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
a.cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = a.cmd;
da.Fill(ds, "viewattandace_repoeting");
rDoc.Load(Server.MapPath("CrystalReport1.rpt"));
rDoc.SetDataSource(ds);
rDoc.SetParameterValue("date", to);
rDoc.SetParameterValue("depid", 1);
CrystalReportViewer1.ReportSource = rDoc;
CrystalReportViewer1.DataBind();
a.CloseConnection();
CleartextBoxes(this);
}
else
if (rd.SelectedValue == "2")
sd = Convert.ToDateTime(m.SelectedItem.Text + "/" + "1" + "/" + yt.SelectedItem.Text);
ed = LastDayOfMonth(sd);
dbconnect a = new dbconnect();
using (ReportDocument rDoc = new ReportDocument())
{
a.OpenConnection();
a.cmd = new SqlCommand("viewattandace_repoeting", a.con);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
a.cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = a.cmd;
da.Fill(ds, "viewattandace_repoeting");
rDoc.Load(Server.MapPath("employeemonthly.rpt"));
rDoc.SetDataSource(ds);
rDoc.SetParameterValue("sdate", sd);
rDoc.SetParameterValue("edate", ed);
rDoc.SetParameterValue("depid", 1);
rDoc.SetParameterValue("email", eet.Text.Trim());
CrystalReportViewer1.ReportSource = rDoc;
CrystalReportViewer1.DataBind();
a.CloseConnection();
}
}
}
}
view only one report at a time which dropdownlist selected index.
Now problem is view only first time selected report . when i selected other report then show this "parameter incorrect".
For Example
I selected CrystalReport1 report in dropdownlist and click button .Then report view ,then I selected employeemonthly report and click button then show this "parameter incorrect" and Vice versa.
I can solved this by add new CrystalReportViewer control
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (rd.SelectedValue == "1")
{
CrystalReportViewer2.Visible = false;
yl.Visible = false;
yt.Visible = false;
}
if (rd.SelectedValue == "2")
{
CrystalReportViewer1.Visible = false;
sdate.Visible = false;
}
}
**Button click code **
protected void Button1_Click1(object sender, EventArgs e)
{
if (Page.IsValid)
{
if (rd.SelectedValue == "1")
{
CrystalReportViewer1.Visible = true;
DateTime to = Convert.ToDateTime(sdate.Text);
dbconnect a = new dbconnect();
ReportDocument rDoc = new ReportDocument();
a.OpenConnection();
a.cmd = new SqlCommand("viewattandace_repoeting", a.con);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
a.cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = a.cmd;
da.Fill(ds, "viewattandace_repoeting");
rDoc.Load(Server.MapPath("CrystalReport1.rpt"));
rDoc.SetDataSource(ds);
rDoc.SetParameterValue("date", to);
rDoc.SetParameterValue("depid", 1);
CrystalReportViewer1.ReportSource = rDoc;
CrystalReportViewer1.DataBind();
CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
a.CloseConnection();
}
else
if (rd.SelectedValue == "2")
{
CrystalReportViewer2.Visible = true;
sd = Convert.ToDateTime(m.SelectedItem.Text + "/" + "1" + "/" + yt.SelectedItem.Text);
ed = LastDayOfMonth(sd);
dbconnect a = new dbconnect();
ReportDocument rDoc = new ReportDocument();
a.OpenConnection();
a.cmd = new SqlCommand("viewattandace_repoeting", a.con);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
a.cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = a.cmd;
da.Fill(ds, "viewattandace_repoeting");
rDoc.Load(Server.MapPath("employeemonthly.rpt"));
rDoc.SetDataSource(ds);
rDoc.SetParameterValue("sdate", sd);
rDoc.SetParameterValue("edate", ed);
rDoc.SetParameterValue("depid", 1);
rDoc.SetParameterValue("email", eet.Text.Trim());
CrystalReportViewer2.ReportSource = rDoc;
CrystalReportViewer2.DataBind();
CrystalReportViewer2.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
a.CloseConnection();
}
}
Please change if part of you code to
ReportDocument rDoc = new ReportDocument())
as you have it in else part
private void BrokerWiseSalesReport_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = null;
dataGridView1.Rows.Clear();
ds = GetBrokerDetailspageload();
//int ii = 0;
if (ds.Tables[0].Rows.Count != 0)
{
dataGridView1.DataSource = ds.Tables[0];
}
}
In Dataset[ if (ds.Tables[0].Rows.Count != 0)] I am getting the No of rows but
while storing in gridview using the statement
dataGridView1.DataSource = ds.Tables[0];
i am Getting no of rows as null
I am using c# connecting with Mysql
Only thing is m Not able to store the data in gridview from dataset
The which m using to store the data set data to gridview is right?
just guide me
dataGridView1.DataSource = ds.Tables[0];
You need to use MySqlDataAdapter.
private void BrokerWiseSalesReport_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = null;
dataGridView1.Rows.Clear();
ds = GetBrokerDetailspageload();
MySqlDataAdapter msd= new MySqlDataAdapter();
msd.Fill(ds);
//int ii = 0;
//if (ds.Tables[0].Rows.Count != 0)
// {
dataGridView1.DataSource = ds;
// }
}
Please let me know the further issues.
Update
public DataSet GetBrokerDetailspageload()
{
MySqlConnection mycon=new MySqlConnection("Your connection string");
string str = "SELECT sm.BrokerName,st.ID,sm.SalesCode,sm.BillNo,sm.SalesBy,st.ProductName,st.Quantity,st.SalesRate,st.NetWeight,st.Expense,st.Amount,st.VatP,St.VatAmt FROM salesmaster sm INNER JOIN salestransaction st ON sm.SalesCode=st.SalesCode";
MySqlCommand cmd=new MySqlCommand(str,mycon);
DataSet ds=new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(ds);
return ds;
}
private void BrokerWiseSalesReport_Load()
{
DataSet ds = new DataSet();
ds = null;
ds=GetBrokerDetailspageload();
dataGridView1.DataSource = ds.Tables[0];
}
I think you are missing one line of code.
Add this line after dataGridView1.DataSource = ds.Tables[0];
dataGridView1.DataBind();
I am trying to fetch the matching database attribute from textbox text and display the related database in GridView. What am I doing wrong here?
protected void btnsearch_Click(object sender, EventArgs e)
{
string q = "Select * from facultyreg where fname ='"+txtsearch.Text.ToString() + "'";
sda = new SqlDataAdapter(q, con);
ds = new DataSet();
sda.Fill(ds, "facultyreg");
GridView2.DataSource = null;
//GridView2.DataBind();
GridView2.DataSource=ds.Tables[0];
/* cmd = new SqlCommand(q,con);
if (sdr.HasRows && sdr != null)
{
sdr.Read();
}*/
}
You can easily bind your gridview with SqlInjection and Parameterized Query like this.
protected void btnsearch_Click(object sender, EventArgs e)
{
var searchresult = SqlInjection(txtsearch.Text.ToString());
var dt = GetData(searchresult);
if(dt != null)
{
GridView2.DataSource= dt;
GridView2.DataBind();
}
}
private DataTable GetData(string searchvalue)
{
using (var dataset = new DataSet())
{
dataset.Locale = CultureInfo.InvariantCulture;
using (var connection = new SqlConnection("Your connection string"))
{
using (var sqlCommand = new SqlCommand("write your store procedure name here", connection))
{
sqlCommand.Parameters.AddWithValue("parameter name from store procedure", searchvalue);
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.CommandTimeout = 180;
using (var sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
dataset.Reset();
sqlDataAdapter.Fill(dataset);
sqlCommand.Connection.Close();
}
}
}
return dataset.Tables[0];
}
}
private static string SqlInjection(string stringValue)
{
if (null == stringValue)
{
return null;
}
enter code here
return stringValue
.RegexReplace("-{2,}", "-") // transforms multiple --- in - use to comment in sql scripts
.RegexReplace(#"(;|\s)(exec|execute|select|insert|update|delete|create|alter|drop|rename|truncate|backup|restore)\s", string.Empty, RegexOptions.IgnoreCase);
}
You are not binding your gridview use somthing like this
protected void btnsearch_Click(object sender, EventArgs e)
{
string q = "Select * from facultyreg where fname ='"+txtsearch.Text.ToString() + "'";
sda = new SqlDataAdapter(q, con);
ds = new DataSet();
sda.Fill(ds);
GridView2.DataSource = ds;
GridView2.DataBind();
/* cmd = new SqlCommand(q,con);
if (sdr.HasRows && sdr != null)
{
sdr.Read();
}*/
}
also as pravpab says, don't use parameterized query to avoid sql injection(ie, don.t concatenate textbox in the query directly).
try this out
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub BindGrid(searchText As String)
Dim connection As New OleDbConnection("myconnection")
Dim cmd As New OleDbCommand
Dim sql As String = "SELECT * FROM OPENQUERY([xxxx.NET\CSI], 'SELECT * FROM SReader.table1 WHERE CurrentCostCenter IN(''27177'') ')"
cmd.Parameters.AddWithValue("#CurrentCostCenter", searchText)
Dim dt As New DataTable()
Dim ad As New OleDbDataAdapter(cmd)
ad.Fill(dt)
If dt.Rows.Count > 0 Then
'check if the query returns any data
GridView1.DataSource = dt
GridView1.DataBind()
'No records found
Else
End If
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs)
BindGrid(TextBox1.Text.Trim())
End Sub
http://www.mikesdotnetting.com/Article/116/Parameterized-IN-clauses-with-ADO.NET-and-LINQ
in assign.aspx page i have
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
str1+=li.Value+" ";
}
}
sql = " insert into assgnmovie values ('"+mvnm+"','"+thnm+"','"+date+"','"+str1+"')";
SqlDatabase.Insert(sql);
}
now in booking.aspx page
protected void ddlMov_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter("select thname from assgnmovie where mvname='"+ddlMov.SelectedItem.Value+"'",con);
adp.Fill(ds);
ddlTheater.DataSource = ds;
ddlTheater.DataValueField = "thname";
ddlTheater.DataBind();
ddlTheater.Items.Insert(0, new ListItem("SELECT", "0"));
}
protected void ddlTheater_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter("select showtime from assgnmovie where mvname='"+ddlMov.SelectedItem.Value+"' and thname='"+ddlTheater.SelectedItem.Value+"'",con);
String a = adp.ToString();
String b = a.Substring(1, 6);
ddlShow.Text = b;
adp.Fill(ds);
how do i split the string and make the words as dataset for ddlshow.list???
Try something like this:
protected void ddlTheater_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter("select showtime from assgnmovie where mvname='"+ddlMov.SelectedItem.Value+"' and thname='"+ddlTheater.SelectedItem.Value+"'",con);
String a = adp.ToString();
String b = a.Substring(1, 6);
ddlShow.Text = b;
adp.Fill(ds);
ddlTheater.DataSource = ds;
ddlTheater.DataValueField = "showtime";
ddlTheater.DataBind();
}