亲爱的所有我在gridview中显示图像,此图像以varbinary格式保存在数据库中,包含其内容类型和图像名称。而且我在gridview中的图像显示完美,现在我想从gridview将相同的图像从gridview中的按钮点击插入到另一个表中,如何实现这一点,任何人都可以指导我吗?我尝试通过从gridview接收这些图像数据来实现它,例如Varbinary数据 - 这是数据库中的图像,内容类型和imagename到文本框但是它会导致错误"从数据类型nvarchar到varbinary(max)的隐式转换是不允许。使用CONVERT函数运行此查询"
<asp:TemplateField HeaderText="" ItemStyle-Width="" Visible="true">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" class="preview" ToolTip='<%#Bind("StaffName") %>'
NavigateUrl='' runat="server">
<asp:ImageButton runat="server" ID="Image2" class="img2" ImageUrl='<%# Eval("ImageName") %>'
CommandName='<%# Eval("Id") %>' CommandArgument='<%# Eval("ImageName") %>' />
</asp:HyperLink>
<asp:TextBox ID="txtFileType" runat="server" Text='<%# Eval("FileType") %>' Visible="true"></asp:TextBox>
<asp:TextBox ID="txtBData" runat="server" Text='<%# Eval("BData") %>' Visible="true"></asp:TextBox>
<asp:TextBox ID="txtImageName" runat="server" Text='<%# Eval("ImageName") %>' Visible="true"></asp:TextBox>
<br />
<br />
</ItemTemplate>
<ControlStyle Width="100%" />
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="20%" />
</asp:TemplateField>
foreach (GridViewRow row1 in gvImage.Rows)
{
if (row1.RowType == DataControlRowType.DataRow)
{
// txtFileType
// txtBData
// txtImageName
TextBox txtFileType, txtBData, txtImageName;
txtFileType = (row1.Cells[1].FindControl("txtFileType") as TextBox);
txtBData = (row1.Cells[1].FindControl("txtBData") as TextBox);
txtImageName = (row1.Cells[1].FindControl("txtImageName") as TextBox);
string constr = ConfigurationManager.ConnectionStrings["CONNECTION"].ConnectionString;
using (SqlConnection con8 = new SqlConnection(constr))
{
string query = "insert into SShare (FId,UDetails,ShareBy,ShareByUserId,BData,FileType,ImageName) values(@FId,@UDetails,@ShareBy,@ShareByUserId,@BData,@FileType,@ImageName)";
using (SqlCommand cmd8 = new SqlCommand(query))
{
cmd8.Parameters.AddWithValue("@FId", txt_Tester.Text);
cmd8.Parameters.AddWithValue("@UDetails", TextBox1.Text);
cmd8.Parameters.AddWithValue("@ShareBy", txt_StaffId.Text);
cmd8.Parameters.AddWithValue("@ShareByUserId", txt_Employee.Text);
cmd8.Parameters.AddWithValue("@BData", txtBData.Text);
cmd8.Parameters.AddWithValue("@FileType", txtFileType.Text);
cmd8.Parameters.AddWithValue("@ImageName", txtImageName.Text);
con8.Open();
// cmd8.ExecuteNonQuery();
this.ExecuteQuery(cmd8, "SELECT");
con8.Close();
}
}
}
}
答案 0 :(得分:2)
这是我的建议。您可以从网格中获取所有数据,但您也可以在SQL中执行此操作。
注意public class MyBookListAdapter extends BaseAdapter {
public MyBookListAdapter( ListOfMyBookActivity listMyBooks, List<Book> feedItemsBooks) {
this.activity = listMyBooks;
this.feedItemsBooks = feedItemsBooks;
}
@Override
public int getCount() {
return feedItemsBooks.size();
}
@Override
public Object getItem(int location) {
return feedItemsBooks.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
final int pos = position;
if (inflater == null) {
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null) {
convertView = inflater.inflate(R.layout.feed_list_item, null);
}
storageDisplayImg = FirebaseStorage.getInstance();
auth = FirebaseAuth.getInstance();
FirebaseUser userConnect = auth.getCurrentUser();
String id_user = userConnect.getUid();
mFirebaseInstance = FirebaseDatabase.getInstance();
mFirebaseDatabase = mFirebaseInstance.getReference("books");
final ImageView profilePic = (ImageView) convertView.findViewById(R.id.book_picture_display_myBook);
name = (TextView) convertView.findViewById(R.id.name_book_display_myBook);
TextView timestamp = (TextView) convertView.findViewById(R.id.timestamp_myBook);
statusMsg = (TextView) convertView.findViewById(R.id.desc_display_myBook);
prix = (TextView) convertView.findViewById(R.id.prix_display_myBook);
TextView ville = (TextView) convertView.findViewById(R.id.ville_display_myBook);
TextView typeCat = (TextView) convertView.findViewById(R.id.type_ann_to_display_myBook);
Book item = feedItemsBooks.get(position);
idLivreListAdapter=item.getId_book();
name.setText(item.getNom_livre());
if (!TextUtils.isEmpty(item.getDate_creation())) {
timestamp.setText(item.getDate_creation());
timestamp.setVisibility(View.VISIBLE);
} else {
timestamp.setVisibility(View.GONE);
}
if (!TextUtils.isEmpty(item.getDesc_livre())) {
statusMsg.setText(item.getDesc_livre());
statusMsg.setVisibility(View.VISIBLE);
} else {
statusMsg.setVisibility(View.GONE);
}
if (!TextUtils.isEmpty(item.getType_annonce_selected())) {
typeCat.setText(item.getType_annonce_selected());
typeCat.setVisibility(View.VISIBLE);
} else {
typeCat.setVisibility(View.GONE);
}
if (item.getPrix_livre() != null) {
prix.setText(item.getPrix_livre() + " DH");
prix.setVisibility(View.VISIBLE);
} else {
prix.setVisibility(View.GONE);
}
if (item.getVille_livre() != null) {
ville.setText(item.getVille_livre());
ville.setVisibility(View.VISIBLE);
} else {
ville.setVisibility(View.GONE);
}
storageRef = storageDisplayImg.getReferenceFromUrl(item.getChemin_image());
Log.e(" ", " storageRef " + storageRef.toString());
if (item.getChemin_image() != null && id_user != null) {
Glide.with(convertView.getContext())
.using(new FirebaseImageLoader())
.load(storageRef)
.into(profilePic);
profilePic.setVisibility(View.VISIBLE);
} else {
profilePic.setVisibility(View.GONE);
}
return convertView;
}
}
不一个BDAta
,它是从SqlParameter
表中提取的:
Employee
答案 1 :(得分:1)
在到处打败头部之后,我想出来,如果有人可以参考,我会发布它。感谢@Crowcoder提供了一个实现它的逻辑。
foreach (GridViewRow row1 in gvImage.Rows)
{
if (row1.RowType == DataControlRowType.DataRow)
{
string Id = gvImage.DataKeys[row1.RowIndex].Value.ToString();
ImageButton imgbtn = (ImageButton)gvImage.Rows[row1.RowIndex].FindControl("Image2");
string filename = imgbtn.ImageUrl;
TextBox ftype = (row1.FindControl("txtFileType") as TextBox);
byte[] bytes = (byte[])GetData("SELECT BData FROM Employee WHERE Id =" + txt_StaffId.Text).Rows[0]["BData"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
imgbtn.ImageUrl = "data:image/png;base64," + base64String;
{
string constr = ConfigurationManager.ConnectionStrings["CONNECTION"].ConnectionString;
using (SqlConnection con8 = new SqlConnection(constr))
{
string query = "insert into SShare (FId,UDetails,ShareBy,ShareByUserId,BData,ImageName, FileType) values(@FId,@UDetails,@ShareBy,@ShareByUserId,@BData,@ImageName,@FileType)";
using (SqlCommand cmd8 = new SqlCommand(query))
{
cmd8.Parameters.AddWithValue("@FId", txt_Tester.Text);
cmd8.Parameters.AddWithValue("@UDetails", TextBox1.Text);
cmd8.Parameters.AddWithValue("@ShareBy", txt_StaffId.Text);
cmd8.Parameters.AddWithValue("@ShareByUserId", txt_Employee.Text);
cmd8.Parameters.AddWithValue("@BData", bytes);
cmd8.Parameters.AddWithValue("@ImageName", filename);
cmd8.Parameters.AddWithValue("@FileType", ftype.Text);
con8.Open();
// cmd8.ExecuteNonQuery();
this.ExecuteQuery(cmd8, "SELECT");
con8.Close();
}
}
}
}
}