I have a detailsview which is used for inserting new records to the GRIDVIEW. DetailsView has 3 bound fields and one template field. The template field has a dropdownlist control.
When I click insert, all the fields should be entered into the database. Bound fields get inserted, but I have problem with the dropdownlist control. I tried writing this code especially for dropdownlist in "INSERT" click event
protected void btnInsert_Click(object sender, EventArgs e)
{
DropDownList ddl = null;
ddl = new DropDownList();
RateCenters rate = null;
rate = new RateCenters();
rate.statename = ddl.Text;
OleDbConnection con = new OleDbConnection(conStr);
OleDbCommand cmd = new OleDbCommand(sql, con);
cmd.Parameters.Add("#State/Province_name", OleDbType.VarChar).Value = statename;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
I am getting this error : Parameter [State/Province_name] has no default value.
Kindly help me to solve this issue
regards,
Arjun
shouldn't you be using rate.statename
cmd.Parameters.Add("#State/Province_name", OleDbType.VarChar).Value = rate.statename;
Related
I am new to Model view Controller so i wanna know what's the exact meaning of data Adapter. I used to create procedure but after that in my controller i use object of sql command and then by using adapter i fetch data's of tables that exists in database.
public void ExecuteSelectQueryWithDataTable(String procedureName, SqlParameter[] sqlParameter, out DataTable dataTable)
{
dataTable = new DataTable();
Random Rnd = new Random();
try
{
using (SqlConnection sqlConnection = new SqlConnection(connectionstring))
{
using (SqlCommand sqlCommand = new SqlCommand("", sqlConnection))
{
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
sqlCommand.CommandText = procedureName;
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddRange(sqlParameter);
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.Fill(dataTable);
}
}
}
}
Thanks in advance!
SQLDataAdaptor is one ado.net class that fill datatable by database table or result set of stored procedure.
I suggest to connect to DB by entity framework instead of legacy development.
BR
I'm creating a form where I can do the following:
please see the image
As you can see, I have a txt_id_up and txt_id_dw
in the database I want to make the following query.
SELECT * FROM Tabla1
WHERE ID BETWEEN 3 AND 7;
where txt_id_up = 3, and txt_id_dw = 7;
Dim connection As OleDbConnection
Dim command As OleDbCommand
Dim data_reader As OleDbDataReader
'------------------------------
'connect to ms.access database
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source= data\base.accdb;Persist Security Info=False")
connection.Open()
'reading data from Tabla1 table
command = New OleDbCommand("SELECT * FROM Tabla1", connection)
data_reader = command.ExecuteReader
'----------------------------------
'here the code to show in listview1 is missing
'----------------------------------
and in passing I would like to ask another question, can only the following columns be shown in listview?
Name
Account
I clarify that I use the datagridview to see it in general and the listview for queries
I don't know if I get your question but if you want to display Name and Account from your database I suggest you to use DataGridView.
Add a DataGridView control to your form and add this code:
Dim connection As OleDbConnection
Dim command As OleDbCommand
Dim data_adapter As OleDbDataAdapter
'------------------------------
'connect to ms.access database
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source= data\base.accdb;Persist Security Info=False")
connection.Open()
'reading data from Tabla1 table
command = New OleDbCommand("SELECT Name, Account FROM Tabla1 WHERE ID BETWEEN 3 AND 7", connection)
data_adapter = New OleDbDataAdapter(command)
'add results to DataGridView1
Dim datatable as New DataTable("Table")
data_adapter.Fill(datatable)
DataGridView1.DataSource = datatable
I may have the 2 text boxes backwards.
Public Class Form3
Private Sub FillListView()
ListView1.BeginUpdate() 'keeps the control from repainting on each addition
Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source= data\base.accdb;Persist Security Info=False")
Dim command As New OleDbCommand("SELECT ID, Name FROM Tabla1 Where ID Between ? And ?;", connection)
command.Parameters.Add("FirstID", OleDbType.Integer).Value = CInt(txt_id_up.Text)
command.Parameters.Add("SecondID", OleDbType.Integer).Value = CInt(txt_id_dw.Text)
connection.Open() 'Open the connection at the last possible minute
Using data_reader = command.ExecuteReader
While data_reader.Read()
Dim li As New ListViewItem()
li.Text = CStr(data_reader.GetValue(0)) 'ID
li.SubItems.Add(CStr(data_reader.GetValue(1))) 'Name
ListView1.Items.Add(li)
Loop
End Using
End Using
ListView1.EndUpdate()
End Sub
End Class
Edit
The BeginUpdate of the ListView control prevents the screen from
repainting every time you add an item. This speeds up the addition
of items.
The Using...End Using block makes sure your connection is closed and disposed
event if there is an error.
I added question marks in the Where clause of your SQL statement.
These are placeholders for parameters in the Access/OleDb provider.
I added the 2 parameters to the Parameters collection of the Command
object. The Add method has a number of overloads. The one used here
takes a name and data type. Then the Value property of the parameter
is set to the numbers in the text boxes. These values are in a text
property so they are strings and need the CInt() to convert to
Integers.
I moved the Open method to directly before the command is executed.
Again the Using...End Using block as explained above.
OOPS! I forgot to add the loop (red face) - Code is now corrected
Inside the loop, create a new ListViewItem. A new item for each
iteration of the loop.
Set the Text property of the ListViewItem to the first column in the reader. Convert it to a string. This should show up in the first column.
Set the first SubItem of the ListViewItem to the second column of
the reader. Again convert to a string. This should show up in the
second column.
Add the ListViewItem to the ListView.
The first End Using will close and dispose your data_reader. The
second End Using will close and dispose your connection.
Very Important! ListView1.EndUpdate() Nothing shows up in the
ListView without this line.
Hope this helps.
I have just joined stackoverflow as a relatively novice user of visual studio, with the hope that someone might be able to help answer a question to a problem I'm having.
I'm sure it's quite simple, but at the moment I have a Gridview which is bound to a table from our SQL Server.
My issue I'm having is with the gridview, which was generated with the help of the in-built wizard. My update SQL statement is a custom one, which is quite simply:
UPDATE SMCsummove
SET SubmitQty = #Textbox1 * - 1, HasBeenEdited = 'Y'
WHERE (SMCPOinteger = #SMCPOinteger) AND (SMCproduct = #PartNo)
Now being a novice, I am not quite sure if I am doing this right, but #Textbox1 is the value that the user enters as a new value to update the table with, after they have clicked the autogenerated 'edit' button. Quite simply once they have clicked the 'update' button, the gridview reverts back to the original value that was there in the SubmitQty column.
If anyone can shed some light or point out any massive oversights, that would be most appreciated!
Need to see more of the code from the gridview to know for sure, but it looks like a postback is occurring and this is causing your gridview to bind again and be reset.
I'm pretty new to this as well and what I have found works for gridviews is to have a pageload event like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindGridView();
}
}
Then have a separate statement for the binding (this is one that I uses:
private void bindGridView()
{
string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
string selectSQL = String.Format("SQL Statement here");
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds, "Review");
GridView1.DataSource = ds;
GridView1.DataBind();
}
You may find you have to call the gridbind as well after updating records to repopulate the gridview with the updated values.
I have a GridControl on my windowsform. On a column, I want to show images+text conditionally.
On page load
string command= "select cond, info from Table";
SqlConnection conn = new SqlConnection("Data Source=10.10.10.10;Initial Catalog=zxcv;Persist Security Info=True;User ID=qw;Password=wq");
conn.Open();
SqlDataAdapter adap = new SqlDataAdapter(command, conn);
DataTable dt = new DataTable();
adap.Fill(dt);
gridControl1.DataSource = dt;
conn.Close();
How can I add images on the "cond" column conditionally(ex. if "cond" column is 1 i want to show 1.png+"Condition 1" on the cell).
This is usually handled using a RepositoryItemImageComboBoxEdit. Create one of these and then edit the items so that each of your conditions is represented by one of the Items in the collection. Set your value to match the cond in your datasource and set an image to match the unique cond.
Then set the editor for your cond column to the RepositoryItemImageComboBoxEdit you created. And set the fieldName to the proper field "cond" to complete the binding.
I've a grid in my winforms application and i bind a huge dataset to the grid. Will the dataset be stored in the memory by the grid after calling DataBind(). How does it operate on the data binded to the grid?
Update
I wrote the following code
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Server=server;Initial Catalog=db;User ID=testv;Pwd=pass"))
{
con.Open();
using (SqlCommand com = new SqlCommand("select * from tbl_Sample", con))
{
using (SqlDataAdapter ada = new SqlDataAdapter(com))
{
ada.Fill(dt);
dgvMain.DataSource = dt;
dt.Dispose();
}
}
}
After assigning the datatable as the datasource i'm able to dispose it. So does it make a copy in the memory?
Thanks
NLV
It doesn't make a copy, it makes a reference to the original datasource object.
P.S. Making a huge dataset is not a good idea anyway. If you need to display lots of rows, make some kind of paging or filters and restrict number of rows to load and display.