cascading dropdownlist with out reload page - sql-server-2008

how to cascade second dropdown without page reload on selecting first dropdown
without autopostback in dropdown
<asp:DropDownList ID="dropcaste" Width="128px" runat=server AutoPostBack=true
onselectedindexchanged="dropcaste_SelectedIndexChanged"></asp:DropDownList>
protected void dropcaste_SelectedIndexChanged(object sender, EventArgs e)
{
string select = dropcaste.SelectedItem.Value;
con = new SqlConnection(str);
cmd = new SqlCommand("select c.castename,s.subcastename from subcastemaster_mrg s inner join castemas_mrg c on(s.castecode=c.castecode) where c.castecode=" + select + "", con);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
subcastegrid.DataSource = dt;
subcastegrid.DataBind();
con.Close();
}

Related

how to display 2 tables from mysql in visual studio 17

this is in my controller
string connStr = "server=" + IpAddress + ";user=" + UserName + ";database=" + DatabaseName + ";port=" + PortNumber + ";password=" + Password + ";SslMode=none;";
MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();
string query = " select * from Cities,Regions";
MySqlCommand com = new MySqlCommand(query, conn);
MySqlDataAdapter da = new MySqlDataAdapter(com);
DataTable ds = new DataTable();
da.Fill(ds);
conn.Close();
I use a view model for displaying data from mysql and i fill them as below.
List<Models.ViewModels.city> CT = new List<Models.ViewModels.city>();
SftpClient sftpclient = new SftpClient("port", "root", "name");
sftpclient.Connect();
foreach (DataRow item in ds.Rows)
{
city ct = new city
{
CityName = item.itemArray[1].ToString(),
CityNumber = Convert.ToInt32(item.itemArray[2])
}
region rg = new region
{
RegionName = item.itemArray[1].ToString(),
RegionNumber = Convert.ToInt32(item.itemArray[2])
}
}
CT.Add(ct);
return View(CT.ToList());
How could i show regions in my razor. i already used viewbag for sending a list form my region viewmodel. but it doesn't fill the viewmodel obviously but how should i fill the region viewmodel

asp.net:query with multiple where conditions

1. I have query.But couldn't add second where condition.Please suggest me the correct semantic .
2. And how can fetch the data from dropdownlist and show it in gridview.
3. How can i fetch value from Tution fee Column of my database when condition satisfies ,and Hostel Fee on fail of condition.??
protected void BindGridview()
{
constr = ConfigurationManager.ConnectionStrings["connstring_DETMIS"].ToString(); // connection string
// String FID = DropDownList1.SelectedItem.Value;
using (var conn = new MySql.Data.MySqlClient.MySqlConnection(constr)) {
conn.Open();
using (var cmd = new MySql.Data.MySqlClient.MySqlCommand("select * from fees_collect_category" + " where F_id =" + DropDownList1.SelectedItem.Value " and C_id=" + DropDownList2.SelectedItem.Value, conn)) {
using (var reader = cmd.ExecuteReader()) {
if (reader.HasRows) {
gvDetails.DataSource = reader;
gvDetails.DataBind();
} else
lblWarning.Text = "There are no records..";
}
}
}
}
Welcome to Stackoverflow. You should first research on the google as why you are not able to add multiple conditions(It's just because of simple syntax mistake).
The exact code of line would be something like this.
using (var cmd = new MySql.Data.MySqlClient.MySqlCommand("select * from fees_collect_category" +
" where F_id = '" + DropDownList1.SelectedItem.Value + "' and C_id=" + DropDownList2.SelectedItem.Value + "'", conn))
NOTE:- As a fellow developer, I won't suggest you to do this by passing values from here as it is dangerous and prone to SQL INJECTION
I would rather tell you to go by using Parameterized queries
Hope that helps and for future go for the Parametrized ones, as it is easy and technically preffered.
protected void BindGridview()
{
String strConnString = ConfigurationManager
.ConnectionStrings["connstring_DETMIS"].ConnectionString;
String strQuery = "select * from student_details " +
"where F_id=#F_Id and C_id=#C_Id";
MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection(strConnString);
MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
cmd.Parameters.AddWithValue("#F_Id",
DropDownList1.SelectedItem.Value);
cmd.Parameters.AddWithValue("#C_Id",
DropDownList2.SelectedItem.Value);
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
gvDetails.DataSource = cmd.ExecuteReader();
gvDetails.DataBind();
}
catch (Exception ex)
{
throw ex;
}
}

how to store columns values in array in asp.net

I am writing code in asp.net. I have a column named c_name in database. I want to store all my columns values in an array. Here is my incomplete code. I am confused what to do here.
String str = "select c_name from contacts where user_id = " + user_id + "";
MySqlCommand cmd = new MySqlCommand(str, dbConnection);
cmd.ExecuteNonQuery();
MySqlDataReader mdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (mdr.Read())
{
}
Please help.
Check below code:
String str = "select c_name from contacts where user_id = " + user_id + "";
MySqlCommand cmd = new MySqlCommand(str, dbConnection);
MySqlDataReader mdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
List<string> list = new List<string>();
while (mdr.Read())
{
list.Add(mdr.GetString(0));
}
string[] strMyArray = list.ToArray<string>();
I suspect one of your your problems is this line:
cmd.ExecuteNonQuery();
You're actually executing cmd twice, once with this line and once again when you create your reader. I've also added a little code to copy the result into an element in a list. Try the following:
string str = "select c_name from contacts where user_id = " + user_id + "";
MySqlCommand cmd = new MySqlCommand(str, dbConnection);
MySqlDataReader mdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
// create a list to hold the results in
List<string> results = new List<string>();
while (mdr.Read())
{
// for each row read a string and add it in
results.Add(mdr.GetString(0));
}
You've asked for an array - I've used a list because it's easier. If you REALLY want an array then just call:
var theArray = results.ToArray();
I would also suggest throwing in some using blocks around the connection, command and reader.

Assign a odbcReader to a dataSet

I had executed a stored Procedure
Dim conStr As String = "Dsn=dat;database=dat;option=0;port=0;server=localhost;uid=root"
Dim con As New OdbcConnection(conStr)
Dim cmd As New OdbcCommand("dat.selectRowCount", con)
con.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("#startIndex", 5)
cmd.Parameters.AddWithValue("#endIndex", 15)
Dim reader As OdbcDataReader
reader = cmd.ExecuteReader
While reader.Read
'Some operations performed
End While
if i need to add this reader to a DataTable means
Dim dt As DataTable = New DataTable("adm_ttemp")
dt.Load(reader)
But how can i assign this reader to a DataSet ?
Why you are looking for a odbcReader to a dataSet assignment? you can directly read query result to a dataset as mentioned in the above answer?
Dim conStr As String = "Dsn=dat;database=dat;option=0;port=0;server=localhost;uid=root"
Dim con As New OdbcConnection(conStr)
Dim cmd As New OdbcCommand("dat.selectRowCount", con)
con.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("#startIndex", 5)
cmd.Parameters.AddWithValue("#endIndex", 15)
Dim DS As New DataSet
'<---- lines to be noticed
Using AD As New OdbcDataAdapter(cmd)
AD.Fill(DS)
End Using
You can call a stored procedure in following way, just try
Dim conStr As String = "Dsn=dat;database=dat;option=0;port=0;server=localhost;uid=root"
Dim con As New OdbcConnection(conStr)
Dim cmd As New OdbcCommand("dat.selectRowCount", con)
con.Open()
'<----
Dim callString As String = "CALL dat.selectRowCount(5,15)"
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = callString
'<-----
Dim dlist As New DataList
Dim DS As New DataSet
Using AD As New OdbcDataAdapter(cmd)
AD.Fill(DS)
End Using
I'd say to change your approach a little, and use an OdbcDataAdapter as follows:
Dim conStr As String = "Dsn=dat;database=dat;option=0;port=0;server=localhost;uid=root"
Dim con As New OdbcConnection(conStr)
Dim cmd As New OdbcCommand("dat.selectRowCount", con)
con.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("#startIndex", 5)
cmd.Parameters.AddWithValue("#endIndex", 15)
Dim DS as new DataSet
Using AD as new OdbcDataAdapter(cmd)
AD.Fill(DS)
End Using
' Perform your operations on the DataSet

Connecting datagrid view to database in vb.net

Ok so im trying to display everything in my MYSQL database using a datagrid view. but what i got is not working, can somebody help me with my code
Public Sub dataview()
DataGridView1.DataSource = ""
Dim bindingSource1 As New BindingSource()
Try
Dim cmd As New MySqlCommand("SELECT * FROM upload", db_con)
With Me.DataGridView1
.AutoGenerateColumns = True
bindingSource1.DataSource = cmd
.DataSource = bindingSource1
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
.BorderStyle = BorderStyle.Fixed3D
.EditMode = DataGridViewEditMode.EditOnEnter
End With
DataGridView1.DataSource = bindingSource1
Catch ex As Exception
MessageBox.Show("something went wrong")
End Try
You cannot simply pass your MySqlCommand to the BindingSource.
You should use a object of type MySqlDataAdapter to get the command and fill a DataSet/DataTable.
That DataTable/DataSet should be used as DataSource for your bindingSource1
Dim cmd As New MySqlCommand("SELECT * FROM upload", db_con)
Dim da = New MySqlDataAdapter (cmd)
Dim dt = New DataTable()
da.Fill(dt)
bindingSource1.DataSource = dt ' here assign the DataTable'
With Me.DataGridView1
.AutoGenerateColumns = True
.DataSource = bindingSource1
.....
End With
Public Sub dataview()
DataGridView1.DataSource = ""
Dim bindingSource1 As New BindingSource()
Try
Dim cmd As New MySqlCommand("SELECT * FROM upload", db_con)
With Me.DataGridView1
.AutoGenerateColumns = True
bindingSource1.DataSource = cmd
.DataSource = bindingSource1
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
.BorderStyle = BorderStyle.Fixed3D
.EditMode = DataGridViewEditMode.EditOnEnter
End With
DataGridView1.DataSource = bindingSource1
Catch ex As Exception
MessageBox.Show("something went wrong")
End Try
try this one^^